0
点赞
收藏
分享

微信扫一扫

Educational Codeforces Round 125 (Rated for Div. 2) C. Bracket Sequence Deletion

q松_松q 2022-03-23 阅读 91
算法

题目跳转至:https://codeforces.com/contest/1657/problem/C

思路

题目要求删除满足要求的最短前缀:
1.当第一个字符为 ( ( (时,不管第二个字符为什么,都满足合法字符串条件,那么就操作数+1,进行下一轮操作。
2.两个最近 ( ( (的字符加上他们之间的字符也一定是满足合法的字符串,所以再次操作数+1,进行下一轮操作。

代码

#include<bits/stdc++.h>
#include<algorithm>
#define ll long long
#define pii pair<int,int>
#define inf 1e9
using namespace std;
const int N = 5e5+10;
int t, n, m, k;
void solve(){
	int n;
	std::cin>>n;
	std::string s;
	std::cin>>s;
	int ans = 0;
	int i = 0;
	while(i < n-1){
		if(s[i] == '('){
			i+=2;
			ans++;
		}
		else {
			int j = i+1;
			while(j < n&&s[j]=='('){
				j++;
			}
			if(j == n)break;
			i = j+1;
			ans++;
		}
	}
	std::cout<<ans<<" "<<n-i<<"\n";
}
int main(){
	std::cin>>t;
	while(t--)
		solve();
	return 0;
}
举报

相关推荐

0 条评论