您的位置:首页 > 其它

hdu 5479(括号问题)

2015-10-01 17:00 232 查看
题意:类似"()","(())","()()" 是匹配的, 而 "((", ")(", "((()"不行.

思路:总感觉题目和自己想的不一样,但是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
;

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;


  

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: