hduoj5479 Scaena Felix
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 substring 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
1
0
2
思路:括号匹配问题,用栈简单模拟 |
#include<iostream>
#include<cstring>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int MAXN = 1e5 + 50;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int INF=0x3f3f3f3f;
#define PI acos(-1)
#define IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,x) for(int i=0;i<x;++i)
#define UP(i,x,y) for(int i=x;i<=y;i++)
#define DOWN(i,x,y) for(int i=x;i>=y;i--)
inline int in() {
char ch = getchar();
int f = 1, x = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
return f * x;
}
string s; int T;
int main(){
IO;
cin>>T;
while(T--){
cin>>s;
stack<char> st;
int cnt = 0,len = s.size();
for(int i = 0; i < len; i++){
if(s[i]=='(') {
st.push(s[i]);
}
else{
if(!st.empty()&&st.top()=='('){
cnt++; st.pop();
}
}
}
cout<<cnt<<endl;
}
return 0;
}