hdu 5479(括号问题)

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

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

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

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

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


 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#define MAXN 100010
typedef 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;

  

}


 



 

posted @ 2015-10-01 17:00  Przz  阅读(138)  评论(0编辑  收藏  举报