1023C - Bracket Subsequence - CodeForces Solution


greedy *1200

Please click on ads to support us..

Python Code:

n,k=map(int,input().split())
s=input()
c=0
y=0
y1=0
c1=0
l=[]
'''for x in s:
    if x=="(":
        c=c+1
    else:
        c1=c1+1'''
y=k//2
y1=k//2
a=0
b=0
for x in s:
    if x == "(" and a<y:
        l.append(x)
        a=a+1
    if x==")" and b<y1:
        l.append(x)
        b=b+1
for x in l:
    print(x,end="")
        
        

C++ Code:

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)

void solve(){
	int n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	string ans;
	ans="";
	int imb=0;
	rep(i,0,n){
		if(s[i]==')' && imb>0){
			ans+=s[i];
			imb--;
		}else{
			if((int)ans.size()+1+imb+1<=k){
				ans+=s[i];
				imb++;
			}
		}
	}
	cout<<ans<<"\n";
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	//cin>>tt;
	while(tt--){
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words
1681A - Game with Cards
151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC