# 【CodeForces 1262C --- Messy】

ACM 专栏收录该内容
344 篇文章 0 订阅

### 【CodeForces 1262C --- Messy】

Description

You are fed up with your messy room, so you decided to clean it up.

Your room is a bracket sequence s=s1s2…sn of length n. Each character of this string is either an opening bracket ‘(’ or a closing bracket ‘)’.

In one operation you can choose any consecutive substring of s and reverse it. In other words, you can choose any substring s[l…r]=sl,sl+1,…,sr and change the order of elements in it into sr,sr−1,…,sl.

For example, if you will decide to reverse substring s[2…4] of string s="((()))" it will be equal to s="()(())".

A regular (aka balanced) 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 “()()”, “(())” are regular (the resulting expressions are: “(1)+(1)”, “((1+1)+1)”), and “)(” and “(” are not.

A prefix of a string s is a substring that starts at position 1. For example, for s="(())()" there are 6 prefixes: “(”, “((”, “(()”, “(())”, “(())(” and “(())()”.

In your opinion, a neat and clean room s is a bracket sequence that:

the whole string s is a regular bracket sequence;
and there are exactly k prefixes of this sequence which are regular (including whole s itself).
For example, if k=2, then “(())()” is a neat and clean room.

You want to use at most n operations to make your room neat and clean. Operations are applied one after another sequentially.

It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in n or less operations.

Input

The first line contains integer number t (1≤t≤100) — the number of test cases in the input. Then t test cases follow.

The first line of a test case contains two integers n and k (1≤k≤n2,2≤n≤2000, n is even) — length of s and required number of regular prefixes.

The second line of a test case contains s of length n — the given bracket sequence. It contains only ‘(’ and ‘)’.

It is guaranteed that there are exactly n2 characters ‘(’ and exactly n2 characters ‘)’ in the given string.

The sum of all values n over all the test cases in the input doesn’t exceed 2000.

Output

For each test case print an answer.

In the first line print integer m (0≤m≤n) — the number of operations. You do not need to minimize m, any value is suitable.

In the following m lines print description of the operations, each line should contain two integers l,r (1≤l≤r≤n), representing single reverse operation of s[l…r]=slsl+1…sr. Operations are applied one after another sequentially.

The final s after all operations should be a regular, also it should be exactly k prefixes (including s) which are regular.

It is guaranteed that the answer exists. If there are several possible answers you can print any.

Sample Input

4
8 2
()(())()
10 3
))()()()((
2 1
()
2 1
)(

Sample Output

4
3 4
1 1
5 8
2 2
3
4 10
1 4
6 7
0
1
1 2

AC代码：

#include <bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'

int main()
{
SIS;
int T,n,k;
cin >> T;
while(T--)
{
cin >> n >> k;
k--;
string s,ss;
cin >> s;
for(int i=0;i<k;i++) ss+="()";
for(int i=0;i<(n-2*k)/2;i++) ss+="(";
for(int i=0;i<(n-2*k)/2;i++) ss+=")";
cout << n << endl;
for(int i=0;i<n;i++)
{
int x=i;
while(ss[i]!=s[x]) x++;
cout << i+1 << " " << x+1 << endl;
for(int j=0;j<(x-i+1)/2;j++) swap(s[j+i],s[x-j]);
}
}
return 0;
}

• 0
点赞
• 0
评论
• 1
收藏
• 一键三连
• 扫一扫，分享海报

04-26 90
05-28 711

04-15 185
11-25 210
03-07 427
05-05 47
11-24 4470
07-02 684
08-09 31

¥2 ¥4 ¥6 ¥10 ¥20

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