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
*/
本文探讨了在给定长度相等的两个字符串s和t的情况下,如何通过交换操作使两字符串相等的问题。提出了一个有效的算法,首先统计s和t中字符不匹配的情况,再通过特定的交换策略实现最小化操作次数。文章详细介绍了算法思路和实现代码,适用于需要优化字符串匹配场景。
576

被折叠的 条评论
为什么被折叠?



