CF1452 C. Two Brackets

链接 https://codeforces.com/contest/1452/problem/C

You are given a string s, consisting of brackets of two types: ‘(’, ‘)’, ‘[’ and ‘]’.

A string is called a regular bracket sequence (RBS) if it’s of one of the following types:

empty string;
‘(’ + RBS + ‘)’;
‘[’ + RBS + ‘]’;
RBS + RBS.
where plus is a concatenation of two strings.

In one move you can choose a non-empty subsequence of the string s (not necessarily consecutive) that is an RBS, remove it from the string and concatenate the remaining parts without changing the order.

What is the maximum number of moves you can perform?

Input
The first line contains a single integer t (1≤t≤1000) — the number of testcases.

Each of the next t lines contains a non-empty string, consisting only of characters ‘(’, ‘)’, ‘[’ and ‘]’. The total length of the strings over all testcases doesn’t exceed 2⋅105.

Output
For each testcase print a single integer — the maximum number of moves you can perform on a given string s.

Example
input
5
()

([)]
)]([
)[(]
output
1
2
2
0
1
Note
In the first example you can just erase the whole string.

In the second example you can first erase the brackets on positions 1 and 2: “”, then “()” is left. After that you can erase it whole. You could erase the whole string from the beginning but you would get one move instead of two.

In the third example you can first erase the brackets on positions 1 and 3: “([)]”. They form an RBS “()”. Then “[]” is left, so you can erase it whole.

In the fourth example there is no subsequence that is an RBS, so you can’t perform a move at all.

In the fifth example you can erase the brackets on positions 2 and 4: “)[(]” and get “)(” as a result. You can erase nothing from it.

题意
刚才有个朋友问我,马老师发生甚么事,我说甚么回事?他给我发了几张截图,我一看,嗷,原来是昨天晚上的cf的c题
单纯的括号配对,我一看过了这么多人,看了样例,就懂意思。
这cf不讲武德。

思路
可以用栈,队列,数组来模拟,配对成功,自加。
简单题,拼手速!

代码

#include <bits/stdc++.h>
typedef long long ll;

const ll mod =1e9 + 7;

using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;

const int N = 2e5+5;
int Case,x,y;
char a[N];
void solve(){
	int ans1 = 0,ans2 = 0,sum = 0;
	scanf("%s",a);
	int len = strlen(a);
	for(int i=0;i<len;i++){
		if(a[i]=='(') ans1++;
		if(a[i]=='[') ans2++;
		if(a[i]==')'&&ans1>0){
			ans1--;
			sum++;
		}
		if(a[i]==']'&&ans2>0){
			ans2--;
			sum++;
		}
	}
	printf("%d\n",sum);
}

int main(){
	Case=1;
	input(Case);
	while(Case--){
		solve();
	}
	return 0;
}
/*
5
5 5
3 4
7 1
0 0
2 0
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值