1215C - Swap Letters

烈酒焚心 提交于 2019-11-30 02:09:01

C. Swap Letters

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Monocarp has got two strings ss and tt having equal length. Both strings consist of lowercase Latin letters "a" and "b".

Monocarp wants to make these two strings ss and tt equal to each other. He can do the following operation any number of times: choose an index pos1pos1 in the string ss, choose an index pos2pos2 in the string tt, and swap spos1spos1 with tpos2tpos2.

You have to determine the minimum number of operations Monocarp has to perform to make ss and tt 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 nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the length of ss and tt.

The second line contains one string ss consisting of nn characters "a" and "b".

The third line contains one string tt consisting of nn characters "a" and "b".

Output

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

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

Examples

input

Copy

4
abab
aabb

output

Copy

2
3 3
3 2

input

Copy

1
a
b

output

Copy

-1

input

Copy

8
babbaabb
abababaa

output

Copy

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 ss with the third letter in tt. Then s=s= "abbb", t=t= "aaab". Then swap the third letter in ss and the second letter in tt. Then both ss and tt are equal to "abab".

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

思路:

记录s中为a,t中为b的情况为ab;s中为b,t中为a的情况为ba。当两种情况加起来除2余0时有解,否则输出-1。

然后用abpir记录ab情况的位置,bapir记录ba情况的位置。当ab情况为偶数时,则ba情况也为偶数,此时可以对每两种ab的位置进行交换,每两种ba的位置进行交换。若ab情况为奇数,则ba情况也为奇数,此时只要将一个ab情况换一下,或者将一个ba情况换一下,然后继续按上述方法置换即可。

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int maxn = 1e6+50;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+7;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
vector<int> abpir;
vector<int> bapir;
string s1, s2;
vector<pair<int, int> > ans;
int main()
{
    ll n;
    cin >> n;
    cin >> s1;
    cin >> s2;
    int ab = 0;
    int ba = 0;
    for(int i = 0; i < s1.size(); ++i){
        if(s1[i] != s2[i]){
            if(s1[i] == 'a'){
                ab++;
                abpir.push_back(i+1);
            }
            else{
                ba++;
                bapir.push_back(i+1);
            }
        }
    }
    if((ab+ba)%2==1){
        puts("-1");
    }else{
        if(ab%2 == 0){
            cout << (ab+ba)/2 << endl;
            for(int i = 0; i < abpir.size(); i+=2){
                cout << abpir[i] << " " << abpir[i+1] << endl;
            }
            for(int i = 0; i < bapir.size(); i+=2){
                cout << bapir[i] << " " << bapir[i+1] << endl;
            }
        }else{
            cout << (ab+ba)/2+1 << endl;
            if(abpir.size()){
                cout << abpir[abpir.size()-1] << " " << abpir[abpir.size()-1] << endl;
                bapir.push_back(abpir[abpir.size()-1]);
                abpir.pop_back();
            }else if(bapir.size()){
                cout << bapir[bapir.size()-1] << " " << bapir[bapir.size()-1] << endl;
                abpir.push_back(bapir[bapir.size()-1]);
                bapir.pop_back();
            }
            for(int i = 0; i < abpir.size(); i+=2){
                cout << abpir[i] << " " << abpir[i+1] << endl;
            }
            for(int i = 0; i < bapir.size(); i+=2){
                cout << bapir[i] << " " << bapir[i+1] << endl;
            }
        }
    }
    return 0;
}
/*

baba
abab


*/

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!