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

Posted qingyuyyyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy相关的知识,希望对你有一定的参考价值。

//因为可以反转n次 所以可以得到任何可以构成的序列
#include<iostream>
#include<string>
#include<vector>
using namespace std ;
typedef pair<int,int>PII;
int n,k;
string s;
string get_str(int n,int k) {//先构建前k-1个 
    string res="";
    for(int i=0; i<k-1; i++) {
        res+="(";
        res+=")";
    }
    int len=n-res.size();
    for(int i=0; i<len/2; i++)
        res+="(";//构建最后一个 
    for(int i=0; i<len/2; i++)
        res+=")";
    return res;
}
void solve_swap(int x,int y) {
    while(x<y) {
        swap(s[x],s[y]);
        x++,y--;
    }
}
void solve() {
    cin>>n>>k;
    cin>>s;
    vector<PII>res;
    string final_str=get_str(n,k);
    for(int i=0; i<n; i++) {
        if(s[i]!=final_str[i]) {
            for(int j=i+1; j<n; j++) {
                if(s[j]==final_str[i]) {
                    solve_swap(i,j);
                    res.push_back({i+1,j+1});
                    break;
                }
            }
        }
    }
    cout<<res.size()<<endl;
    for(int i=0; i<res.size(); i++)
        cout<<res[i].first<<" "<<res[i].second<<endl;
}
int main() {
    int t;
    cin>>t;
    while(t--) solve();
    return 0;
}

 

 

以上是关于Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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