C. Swap Letters

Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters “a” and “b”.

Monocarp wants to make these two strings s and t equal to each other. He can do the following operation any number of times: choose an index pos1 in the string s, choose an index pos2 in the string t, and swap spos1 with tpos2.

You have to determine the minimum number of operations Monocarp has to perform to make s and t equal, and print any optimal sequence of operations — or say that it is impossible to make these strings equal.

Input
The first line contains one integer n (1≤n≤2⋅105) — the length of s and t.

The second line contains one string s consisting of n characters “a” and “b”.

The third line contains one string t consisting of n characters “a” and “b”.

Output
If it is impossible to make these strings equal, print −1.

Otherwise, in the first line print k — the minimum number of operations required to make the strings equal. In each of the next k lines print two integers — the index in the string s and the index in the string t that should be used in the corresponding swap operation.

Monocarp有两个长度相等的弦S和T。两个字符串都由小写拉丁字母“a”和“b”组成。
Monocarp想要使这两条弦S和T相等。他可以多次执行以下操作:在字符串s中选择一个索引pos1,在字符串t中选择一个索引pos2,并用tpos2交换spos1。
您必须确定Monocarp必须执行的使s和t相等的最小操作数,并打印任何最佳操作序列,或者说不可能使这些字符串相等。
输入
第一行包含一个整数n(1≤n≤2 105)-s和t的长度。
第二行包含一个由n个字符“a”和“b”组成的字符串。
第三行包含一个由n个字符“a”和“b”组成的字符串t。
输出
如果无法使这些字符串相等,请打印-1。
否则,在第一行print k-使字符串相等所需的最小操作数。在接下来的k行中,每行打印两个整数-字符串s中的索引和字符串t中的索引,这两个索引应该在相应的交换操作中使用。

Examples
input

4
abab
aabb

output

2
3 3
3 2

input

1
a
b

output

-1

input

8
babbaabb
abababaa

output

3
2 6
1 3
7 8

Note
In the first example two operations are enough. For example, you can swap the third letter in s with the third letter in t. Then s= “abbb”, t= “aaab”. Then swap the third letter in s and the second letter in t. Then both s and t are equal to “abab”.

In the second example it’s impossible to make two strings equal.

在第一个例子中,两个操作就足够了。例如,您可以将s中的第三个字母替换为t中的第三个字母,然后s=“abbb”,t=“aaab”。然后交换s中的第三个字母和t中的第二个字母,然后s和t都等于“abab”。
在第二个例子中,不可能使两个字符串相等。

分析:
如果两个字符串相等,则a、b的个数应该是偶数,如果是奇数,无论怎样换都不可能相等,输出-1 。
将第一个字符串从头遍历,如果遇到和第二个字符串中的字符不相同的,则从这个位置以后遍历第二个字符串,
直到遇到一个当前位置两个字符串中的字符不相等而且将第二个字符串的字符与第一个字符串原先位置互换后,
原先位置两个字符串中的字符相等,则将两个字符互换。
如果将第二个字符串遍历完之后没有找到这样的字符,则将当前位置两个字符串中的字符互换,
再遍历第二个字符,重复刚才操作。

code:

#include <iostream>
#define maxn 200005
using namespace std;
char a[maxn],b[maxn];
struct step
{
    int x,y;
};
step c[maxn];
int main()
{
    int n,i,j;
    while(cin>>n)
    {
        int sum=0,e=0;
        for(i=1; i<=n; i++)
        {
            cin>>a[i];
            if(a[i]=='a')
                sum++;
            else
                e++;
        }
        for(i=1; i<=n; i++)
         {
            cin>>b[i];
            if(b[i]=='a')
                sum++;
            else
                e++;
        }
        if(sum%2!=0||e%2!=0)
        {
            cout<<"-1"<<endl;
            continue;
        }
        sum=e=0;
        char ch;
        for(i=1; i<=n; i++)
        {
            if(a[i]!=b[i])
            {
                sum++;
                for(j=i+1; j<=n; j++)
                {
                    if(a[j]!=b[j]&&b[j]==b[i])
                    {
                        ch=b[j];
                        b[j]=a[i];
                        a[i]=ch;
                        c[e].x=i;
                        c[e].y=j;
                        e++;
                        break;
                    }
                }
                if(j==(n+1))
                {
                    ch=a[i];
                    a[i]=b[i];
                    b[i]=ch;
                    c[e].x=i;
                    c[e].y=i;
                    e++;
                    i--;
                }
            }
        }
        cout<<sum<<endl;
        for(i=0;i<e;i++)
            cout<<c[i].x<<" "<<c[i].y<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44350170/article/details/100940780