zoukankan      html  css  js  c++  java
  • HDOJ:Balanced Sequence

    • 解题思路
    1. 输入多组扩号字符串,自由排列,尽可能的让匹配的对数多。
    2. 先对单个字符串处理,把单一字符串中能匹配的括号配对出来。
    3. 队列按单边括号最少数排列(字符串连接配对的括号对数多)
    4. 把处理好的字符串入队
    5. 取出前两个字符串,按各自的左,右括号数确定,各自放在左还是右
    6. 再把5中的处理出的新字符串入队(没有就忽略)
    7. 直到无法再匹配,结束输出答案
      代码:
    #include<ostream>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<stack>
    using namespace std;
    char s[100005];
    struct node
    {
        int st;
        int et;
        bool operator <(const node a)const
        {
            return min(st,et)<min(a.st,a.et);
        }
    };
    priority_queue<node> q;
    stack<char> c;
    int main()
    {
        int t,n,a,b,st,et,sum;
        scanf("%d",&t);
        while(t--)
        {
            while(!q.empty()) q.pop();
            sum=0;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                a=0;b=0;
                scanf("%s",&s);
                int len=strlen(s);
                for(int j=0;j<len;j++)
                {
                    if(s[j]==')')
                    {
                        if(!c.empty()&&c.top()=='(')
                            c.pop();
                        else
                            c.push(s[j]);
                    }
                    else
                        c.push(s[j]);
     
                }
                while(!c.empty())
                {
                    if(c.top()=='(')
                        a++;
                    else
                        b++;
                    c.pop();
                }
                sum+=(len-a-b)/2;
                node now;
                now.et=a;
                now.st=b;
                q.push(now);
            }
            while(!q.empty())
            {
                node now1,now2;
                now1=q.top();
                q.pop();
                if(q.empty()) break;
                now2=q.top();
                q.pop();
                if(min(now1.st,now2.et)>min(now1.et,now2.st))
                {
                    swap(now1.st,now2.st);
                    swap(now1.et,now2.et);
                }
                sum+=min(now1.et,now2.st);
                node ti;
                if(now1.et>now2.st)
                {
                    ti.et=now1.et-now2.st+now2.et;
                    ti.st=now1.st;
                }
                else
                {
                    ti.st=now2.st-now1.et+now1.st;
                    ti.et=now2.et;
                }
                if(ti.et||ti.st)
                    q.push(ti);
            }
            printf("%d\n",sum*2);
        }
    }
    
  • 相关阅读:
    Springboot中使用ibatis输出日志
    openssl生成iis需要的pfx格式的证书
    SpringBoot 默认日志
    Spring @Value 默认值
    SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成增删改查逻辑
    geomesa hbase geoserver
    在hyper-v中安装centos后配置网络
    linux命令
    SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成ResultMap
    Java 获取指定包下的所有类
  • 原文地址:https://www.cnblogs.com/freeyouth/p/15753582.html
Copyright © 2011-2022 走看看