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.
=========================================================================
首先不匹配有两种情况
a b
b a
如果两种情况都是偶数,那么一定能够置换成功,且只需要对单个情况每两个进行两两组合即可
如果两个都是奇数,那么两个各剩下一个,单独耗费两次(参考样例1)
如果一奇一偶,那么显然是不可以的
- # include
- #include
- # include
- # include
- # include
- # include
- using namespace std;
- typedef long long int ll;
-
- vector<int>v1,v2;
-
- int main()
- {
-
-
- int n;
-
- cin>>n;
-
- string a,b;
- cin>>a>>b;
-
- for(int i=0; i
- {
- if(a[i]=='a'&&b[i]=='b')
- v1.push_back(i+1);
- else if(a[i]=='b'&&b[i]=='a')
- v2.push_back(i+1);
- }
-
- if((v1.size()+v2.size())%2)
- {
- cout<<-1;
- }
- else
- {
-
- if(v1.size()%2==0&&v2.size()%2==0)
- {
-
-
- cout<<(v1.size()+v2.size())/2<
-
- if(v1.size())
- {
- for(int i=0; i
size(); i+=2) - {
- cout<
" "<1]< - }
-
-
- }
-
-
- if(v2.size())
- {
- for(int i=0; i
size(); i+=2) - {
- cout<
" "<1]< - }
-
-
- }
- }
- else if(v1.size()%2&&v2.size()%2)
- {
-
-
- cout<<(v1.size()+v2.size()-2)/2+2<
-
- if(v1.size())
- {
- for(int i=0; i+1
size(); i+=2) - {
- cout<
" "<1]< - }
-
-
- }
-
-
- if(v2.size())
- {
- for(int i=0; i+1
size(); i+=2) - {
- cout<
" "<1]< - }
-
-
- }
-
- cout<
size()-1]<<" "<size()-1]< -
- cout<
size()-1]<<" "<size()-1]< -
- }
- }
-
- return 0;
- }
-
相关阅读:
ThinkingPython | 关于Programing 和 Debugging的认识
Android14 WMS-窗口添加流程(一)-Client端
博流BL602开发一 编译与实例
听GPT 讲Istio源代码--pkg(4)
【网络通信 -- WebRTC】项目实战记录 -- mediasoup android 适配 webrtc m94
计算机视觉实战项目(图像分类+目标检测+目标跟踪+姿态识别+车道线识别+车牌识别)
SpringCloud之OpenFeign调用解读
【深度学习】图像超分实验:SRCNN/FSRCNN
数据结构28TI
阿里云韩国服务器测试IP地址及公网带宽收费价格表
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126323507