1023CBracket Subsequence

C. Bracket Subsequence

http://codeforces.com/problemset/problem/1023/C

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 ss and an integer number kk. Your task is to find a regular bracket sequence of length exactly kk such that it is also a subsequence of ss.

It is guaranteed that such sequence always exists.

Input

The first line contains two integers nn and kk (2≤k≤n≤2⋅1052≤k≤n≤2⋅105, both nn and kk are even) — the length of ss and the length of the sequence you are asked to find.

The second line is a string ss — regular bracket sequence of length nn.

Output

Print a single string — a regular bracket sequence of length exactly kk such that it is also a subsequence of ss.

It is guaranteed that such sequence always exists.

Examples

input

6 4
()(())

output

()()

input

8 8
(()(()))

output

(()(()))

 

题意:给有n个字符的括号序列(符合规律),输出含k个字符的括号序列(符合规律)。k位偶数

思路:水题。输出第k/2个"("前的序列,然后补齐")"即可。

 

代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#define inf 0x3f3f3f3f
using namespace std;

char s[200005];

int main()
{
    int n,k;
    cin>>n>>k;
    cin>>s;
    int m=k/2;
    int num1=0;
    int sum=0;
    for(int i=0;i<n;i++)
    {
        sum++;
        if(s[i]=='(')
        {
            cout<<"(";
            num1++;
            if(num1==m)
                break;
        }
        else
        {
            cout<<")";
        }
    }
    for(int i=sum+1;i<=k;i++)
        cout<<")";
    cout<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值