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

猜你喜欢

转载自www.cnblogs.com/QingyuYYYYY/p/11968013.html