Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

Codeforces Round #529 (Div. 3)

题目传送门

题意:

给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列

思路:

这么考虑:

如果是左括号

1)整个序列左括号个数比右括号多 2

2)在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数

3)在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数减去前缀右括号个数大于2

(这里这么想,把左变成右,左-1,右+1)

右括号也是这样

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],pre[N],post[N];
char s[N];
int n;
int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%s",s+1);
        int x=0;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='(') x++;
            else x--;
            a[i]=x;
        }
       pre[0]=N,post[n]=N;
       for(int i=1;i<n;i++) pre[i]=min(pre[i-1],a[i]);
       for(int i=n-1;i>=0;i--) post[i]=min(post[i+1],a[i]);
       int ans=0;
       if(x!=-2&&x!=2)
       {
           printf("0
");
       }
       else{
       for(int i=1;i<=n;i++)
       {
           if(s[i]=='(')
           {
               if(pre[i-1]>=0&&post[i]>=2&&x==2) ans++;
           }
           else if(pre[i-1]>=0&&post[i]>=-2&&x==-2) ans++;
       }
        printf("%d
",ans);
       }
    }
}
View Code
原文地址:https://www.cnblogs.com/zhgyki/p/10336488.html