D. Makoto and a Blackboard

http://codeforces.com/contest/1097/problem/D
对于题中一个括号序列,将其中合法的括号序列删去,显然无影响(如将"()())(((“变为”)((("),那么我们首先这样处理题目给的n个序列,得到新的n个序列。
新的n个序列中,主要有四种典型序列,空串,“(”,“)”,或者“)(”,显然第四种是无论如何都拼不出合法序列的。
现在,我们将所有序列用map<string,int>ma计个数。
然后,对每一个串,按正括号等于反括号,反为正建一个反串,在map里找这个反串,若有,得到一对,答案加1。
最终答案再加上空串的数量除二向下取整,输出。
上述几步并行执行,节约时间。

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int N,ans,cou;
char s[500005];
string str,dstr;
stack<char>sta;
map<string,int>ma;
int main()
{
    cin>>N;
    while(N--)
    {
        scanf("%s",s);
        str.clear();
        dstr.clear();
        for(int i=0;s[i];i++)
        {
            if(str.length())
            {
                if(str[str.length()-1]=='('&&s[i]==')')str.erase(str.end()-1),dstr.erase(dstr.end()-1);
                else
                {
                    str+=s[i];
                    if(s[i]=='(')dstr+=")";
                    else dstr+="(";
                }
            }
            else
            {
                str+=s[i];
                if(s[i]=='(')dstr+=")";
                else dstr+="(";
            }
        }
        if(str!="")
        {
            if(ma[dstr])
            {
                ans++;
                ma[dstr]--;
            }
            else ma[str]++;
        }
        else cou++;
    }
    cout<<ans+(cou>>1);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值