zoukankan      html  css  js  c++  java
  • Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)

    This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In addition, k1000,n50k≤1000,n≤50 and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.

    After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.

    Ujan has two distinct strings ss and tt of length nn consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation at most 2n2n times: he takes two positions ii and jj (1i,jn1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj.

    Ujan's goal is to make the strings ss and tt equal. He does not need to minimize the number of performed operations: any sequence of operations of length 2n2n or shorter is suitable.

    Input

    The first line contains a single integer kk (1k10001≤k≤1000), the number of test cases.

    For each of the test cases, the first line contains a single integer nn (2n502≤n≤50), the length of the strings ss and tt.

    Each of the next two lines contains the strings ss and tt, each having length exactly nn. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

    Output

    For each test case, output "Yes" if Ujan can make the two strings equal with at most 2n2n operations and "No" otherwise. You can print each letter in any case (upper or lower).

    In the case of "Yes" print mm (1m2n1≤m≤2n) on the next line, where mm is the number of swap operations to make the strings equal. Then print mm lines, each line should contain two integers i,ji,j (1i,jn1≤i,j≤n) meaning that Ujan swaps sisi and tjtj during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than 2n2n is suitable.

    Example
    input
    Copy
    4
    5
    souse
    houhe
    3
    cat
    dog
    2
    aa
    az
    3
    abc
    bca
    
    output
    Copy
    Yes
    1
    1 4
    No
    No
    Yes
    3
    1 2
    3 1
    2 3



    #include<bits/stdc++.h>
    using namespace std;
    int n;
    string s,t;
    vector<pair<int,int> >op;
    void solve() {
        op.clear();
        cin>>n;
        cin>>s>>t;
        for(int i=0; i<s.size(); i++) {
            if(s[i]!=t[i]) {
                int flag = 0;
                for(int j=i+1; j<t.size(); j++) {
                    if(t[j]==t[i]) {
                        flag = 1;
                        op.push_back(make_pair(i+1,j+1));
                        swap(s[i],t[j]);
                        break;
                    }
                }
                if(flag==0) {
                    for(int j=i+1; j<s.size(); j++) {
                        if(s[j]==t[i]) {
                            flag = 1;
                            op.push_back(make_pair(j+1,t.size()));
                            swap(s[j],t[t.size()-1]);
                            op.push_back(make_pair(i+1,t.size()));
                            swap(s[i],t[t.size()-1]);
                            break;
                        }
                    }
                }
                if(flag==0) {
                    puts("NO");
                    return;
                }
            }
        }
        puts("YES");
        cout<<op.size()<<endl;
        for(int i=0; i<op.size(); i++) {
            cout<<op[i].first<<" "<<op[i].second<<endl;
        }
        return;
    }
    int main() {
        int t;
        scanf("%d",&t);
        while(t--)
            solve();
    }
    /* 考虑贪心,假设我们已经考虑到了i位置,[0,i)区间的都已经相同了。
    如果s[i]!=t[i]的情况,我们考虑首先交换s[i]和t[j],即能否在t里面找到和t[i]相同的;
    如果没有,我们再从s里面去找即可。
    假设s[j]==t[i],那么我们交换s[j]和t[t.size()-1],再交换s[i]和t[t.size()-1],
    只需要两次操作就可以使得s[i]变成s[j]了,那么我们这样最多操作2n次,就可以使得s==t了。 */ 
  • 相关阅读:
    数组相关操作
    控制结构和函数
    调用函数和方法
    常用类型、算术和操作符重载
    函数式 CSS (FCSS)
    Javascript 封装问题
    认识javascript中的作用域和上下文
    Javascript 继承-原型的陷阱
    解耦你的HTML,CSS和JAVASRIPT
    网格如此简单
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11829332.html
Copyright © 2011-2022 走看看