题意:类似"()","(())","()()" 是匹配的, 而 "((", ")(", "((()"不行.
思路:总感觉题目和自己想的不一样,但是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 ",ans); } return 0;
}