C. Messy

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.

Example

input

4

8 2

()(())()

10 3

))()()()((

2 1

()

2 1

)(

output

4

3 4

1 1

5 8

2 2

3

4 10

1 4

6 7

0

1

1 2

Note

In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change s).

题意

给你一个长度为n的括号序列,恰好n/2个(,n/2个),你需要通过最多n次翻转操作,使得能够得到恰好k个合法括号前缀。

题解

首先因为能够翻转n次,所以任何序列我都能得到。

然后我只要构造出来就行。

假设要k个合法前缀,那么我前k-1个括号前缀通过()()()()()()构造,最后的一个为剩下的括号((((.....))))这样构造就可以了

代码

#include<bits/stdc++.h>
using namespace std;
int n,k;
string s;
void solve_swap(int x,int y){
while(x<y){
swap(s[x],s[y]);
x++,y--;
}
}
string get_str(int n,int k){
string tmp="";
for(int i=0;i<k-1;i++){
tmp+="(";
tmp+=")";
}
int len = n-tmp.size();
for(int i=0;i<len/2;i++){
tmp+="(";
}
for(int i=0;i<len/2;i++){
tmp+=")";
}
return tmp;
}
void solve(){
cin>>n>>k;
cin>>s;
vector<pair<int,int>>ans;
string final_str = get_str(n,k);
for(int i=0;i<s.size();i++){
if(s[i]!=final_str[i]){
for(int j=i+1;j<s.size();j++){
if(s[j]==final_str[i]){
solve_swap(i,j);
ans.push_back(make_pair(i+1,j+1));
break;
}
}
}
}
//cout<<s<<endl;
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
}

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造的更多相关文章

  1. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

    A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...

  2. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学

    F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong ...

  3. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和

    E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...

  4. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

    D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...

  5. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心

    B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) pos ...

  6. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题

    A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...

  7. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy

    //因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...

  8. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box

    #include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...

  9. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem

    //只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int ...

随机推荐

  1. vue render函数解析

    一.render 函数的作用: 写一些vue.js的template太繁琐,利用render,可以使用js来生成模板,更加灵活和简便. 二.使用render前提: 官网也说了.在深入渲染函数之前推荐阅 ...

  2. 理解ES6的新数据类型:Symbol

    ES6之前的数组类型 在ES6之前JS只有6种数据类型,分别是:Undefined.Null.布尔值(Boolean).字符串(String).数值(Number).对象(Object). ES6引入 ...

  3. JSP的介绍

    JSP概念 JSP全称java server page,中文含义为java服务端页面.对于jsp的理解需要和另外几个相似的概念连接起来:Html和Servlet.常规的html作为一个静态文本传输,具 ...

  4. jenkins实现git自动拉取代码时替换配置文件

    jenkins实现从git上自动拉取源代码——>自动编译——>发布到测试服务器——>验证测试,这个大家应该都知道,但是关于源代码里的配置文件,可能就会有点头疼了, 一般测试都会自己的 ...

  5. python大作业二

    一.存入csv 上次爬取到了所需要的内容,但是没有存入到csv中,这次存入了csv文件中,代码如下: import requests from bs4 import BeautifulSoup imp ...

  6. 调试seanbell/intrinsic遇到的坑

    那些遗忘过去的人注定要重蹈覆辙.——乔治•桑塔亚纳  Authorized error 刚开始按作者 GitHub 上的指示,当运行环境配置好,并且 make 之后,因为生成的 decompose.p ...

  7. laravel实现多模块

    一.这里使用Caffienate Modules 网址:modules maintained by caffeinated 二.根据自己的版本选择包的版本 三.在项目composer.json文件中加 ...

  8. EF中存储过程的使用

    存储过程即用来完成一个特定功能的一段代码.它的优缺点 优点 存储过程可封装,并隐藏复杂的商业逻辑. 存储过程可以回传值,并可以接受参数. 存储过程无法使用 SELECT 指令来运行,因为它是子程序,与 ...

  9. php代码如何加域名授权?开源php项目如何保护版权 商业授权?

    php在web开发领域是最热门的语言,也是开发项目的不二选择,许多PHP开发者说它是当今世界上最好的开发语言,php开发项目效率高,是因为开源项目太多了,不管是国内,还是国外,开源的框架,开源的CMS ...

  10. XSS原理及其相应工具使用

    XSS(厉害程度:只要js能实现什么功能,xss就能对client造成什么伤害):   原理:通过web站点漏洞,向客户端交付恶意脚本代码,实现对客户端的攻击目的 主要攻击目的(网页挂马:通过XSS向 ...