A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
You are given a regular bracket sequence s
and an integer number k. Your task is to find a regular bracket sequence of length exactly k such that it is also a subsequence of s
.
It is guaranteed that such sequence always exists.
Input
The first line contains two integers n
and k (2≤k≤n≤2⋅105, both n and k are even) — the length of s
and the length of the sequence you are asked to find.
The second line is a string s
— regular bracket sequence of length n
.
Output
Print a single string — a regular bracket sequence of length exactly k
such that it is also a subsequence of s
.
It is guaranteed that such sequence always exists.
Examples
Input
Copy
6 4 ()(())
Output
Copy
()()
Input
Copy
8 8 (()(()))
Output
Copy
(()(()))
题意:
给你2个长度,一个字符串,一个是字符串的长度,一个是要你构造的长度,一个字符串合法当且仅当每一位的左括号数大于等于右括号数,且最后一位的左括号数等于右括号数。
思路:
观察:首先我读懂了题意 提出假设:贪心就行
code:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<string.h>
# include <cstdio>
# include <iostream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n,m,tl,tr,book[200100];
char s[200100];
int main()
{
memset(book,0,sizeof(book));
cin>>n>>m;
scanf("%s",s+1);
tl=tr=(n-m)/2;
for(int i=1;i<=n;i++)
{
if(tr>0)
{
if(s[i]==')')
{
book[i]=1;
tr--;
}
}
else
break;
}
for(int i=1;i<=n;i++)
{
if(tl>0)
{
if(s[i]=='(')
{
book[i]=1;
tl--;
}
}
else
break;
}
for(int i=1;i<=n;i++)
{
if(!book[i])
cout<<s[i];
}
cout<<endl;
return 0;
}