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