hdu 5479(括号问题)

来源:互联网 发布:linux默认用户名密码 编辑:程序博客网 时间:2024/06/06 15:42

题意:类似"()","(())","()()" 是匹配的, 而 "((", ")(", "((()"不行.

思路:总感觉题目和自己想的不一样,但是AC了,这是什么鬼Orz

因为要所有子串都不匹配,所以最终是连续的'(' or ')',  即 ))) , (((  ,))((

所以找 '('和 ‘)’匹配的最小个数,匹配的个数即是要改变的   

例:  ( ( ) ) )  ---> ) ) ) ) )      ( ( ( ) ) ---> ( ( ( ( (


#include <cstdio>#include <cstring>#include <algorithm>#include <functional>#include <vector>#include <queue>#define MAXN 100010typedef long long ll;using namespace std;const int N = 1e5 + 5;char s[N];int main(){    int t,cas = 1;    int n,len;    scanf("%d",&t);    while(t--)    {        getchar();        scanf("%s",s);        int a1 = 0;        int ans = 0;        int len = strlen(s);        for(int i = 0;i < len;i++)        {            if(s[i] == '(')                a1++;            if(s[i] == ')' && a1 > 0)            {                ans ++;                a1--;            }        }        printf("%d\n",ans);    }    return 0;}




0 0
原创粉丝点击