D. Makoto and a Blackboard

本文深入探讨了括号序列的有效匹配算法,通过去除合法括号序列简化问题,并使用map记录不同序列出现次数,实现了快速配对。算法核心在于构建反转序列进行匹配,最终输出最大合法括号序列对数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

Mersenne Twister The C extension underlying the random module includes code based on a download from http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html. The following are the verbatim comments from the original code:_random A C-program for MT19937, with initialization improved 2002/1/26. Coded by Takuji Nishimura and Makoto Matsumoto. Before using, initialize the state by using init_genrand(seed) or init_by_array(init_key, key_length). Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Any feedback is very welcome. 翻译下说的什么
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值