hduoj5479 Scaena Felix

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值