Codeforces Round #626 C. Unusual Competitions(括号匹配)

传送门

题意:

给一个长度为n的字符串,只有 ( ) '('和')' 组成
我们可以选取任意一段长度为len的子串,把它随便排序,需要花费的时间为len秒,问最小需要多长时间,原字符串完全匹配,不可能的话输出-1

思路:

不可能的情况就是 ( ) (和) 的数量不相等
为了尽可能短,那我们每次一遇到可能匹配的(就是‘( ’和 ‘)’的个数相等的时候)就判断一下
做法:
遍历一遍,记录( 和 )的个数,当他们两个数量相等时,判断一下该段是否已经匹配,若没有匹配需要花费时间排序,加上该段的长度即可

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
typedef long long ll;
using namespace std;
const int MAXN=1e6+50;
const int inf=0x3f3f3f3f;
const int M=5000*4;
char ch[MAXN];
int main()
{
    int n;
    cin>>n;
    cin>>(ch+1);
    int f=0;
    for(int i=1;i<=n;i++){
        if(ch[i]=='(')f++;
        if(ch[i]==')')f--;
    }
    if(f!=0){
        cout<<-1<<endl;
        return 0;
    }
    int ans=0,cnt=0,l,r;
    l=1;
    if(ch[1]=='(')f++;
    else f--;
    for(int i=2;i<=n;i++){
        if(ch[i]=='(')f++;
        else f--;
        if(f==0){//判断l到i这一段是否已经匹配
            stack<char>s;
            char chc;
        for(int j=l;j<=i;j++)
        {
        if(s.empty())
            s.push(ch[j]);
        else{
            chc=s.top();
            if(chc=='('&&ch[j]==')')
                s.pop();
            else
                s.push(ch[j]);
            }
        }
        if(!s.empty())ans+=(i-l+1);//说明没有匹配
        l=i+1;


        }
    }
    cout<<ans<<endl;
    return 0;
}
/*

*/
发布了158 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44091178/article/details/104723409