1023 C. Bracket Subsequence

传送门

[http://codeforces.com/contest/1023/problem/C]

题意

n字符串长度,k要求的字符串的长度,字符串只包含'('和')',而且这两种的数量相等,要求的字符串是原始的子串

分析

很简单,'('和')'一对对消去即可,记录'('的位置

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,k;
	string s;
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	while(cin>>n>>k){
		cin>>s;
		if(n==k)
		{
			cout<<s<<endl;
			continue;
		}
		 ll sum=(n-k)/2;
		 ll j,pos=0;

		 	for(int i=0;i<n;i++){
		 	   	while(s[pos]!='(') 
		        {
		        	pos++;
				}
		 	if(s[i]==')') {
		 		sum--; 
		 		s[pos]=s[i]='1';
		 		if(sum==0) break;
			 } 
		 }
		for(int y=0;y<n;y++)
		{
			if(s[y]!='1') cout<<s[y];
		}
		cout<<endl;
	}
	return 0;
} 
posted @ 2018-08-18 16:33  ChunhaoMo  阅读(194)  评论(0)    收藏  举报