hdoj--5479--Scaena Felix(stack水题)

来源:互联网 发布:it企业级 编辑:程序博客网 时间:2024/05/10 14:55

Scaena Felix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 768    Accepted Submission(s): 327


Problem Description
Given a parentheses sequence consist of '(' and ')', a modify can filp a parentheses, changing '(' to ')' or ')' to '('.

If we want every not empty <b>substring</b> of this parentheses sequence not to be "paren-matching", how many times at least to modify this parentheses sequence?

For example, "()","(())","()()" are "paren-matching" strings, but "((", ")(", "((()" are not.
 

Input
The first line of the input is a integer T, meaning that there are T test cases.

Every test cases contains a parentheses sequence S only consists of '(' and ')'.

1|S|1,000.
 

Output
For every test case output the least number of modification.
 

Sample Input
3()(((((())
 

Sample Output
102
 

Source
BestCoder Round #57 (div.2)

括号配对水题
#include<cstdio>#include<stack>#include<cstring>#include<algorithm>using namespace std;char s[20000];int main(){int t;scanf("%d",&t);while(t--){memset(s,0,sizeof(s));stack<char>q;scanf("%s",s);q.push(s[0]);int ans=0;int i;for(i=1;i<strlen(s);i++){if(s[i]==')')q.pop(),ans++;elseq.push(s[i]);}printf("%d\n",ans);}return 0;}


0 0