C. Bracket Subsequence

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;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值