CodeForces-1215C-Swap Letters-思维
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.
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".
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.
4
abab
aabb
2
3 3
3 2
1
a
b
-1
8
babbaabb
abababaa
3
2 6
1 3
7 8
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和t两个长度相等的字符串,问需要交换多少次s和t才能相等
输出次数和需要交换的下标
思路:
分两种情况进行讨论
ababab
babbab
ab=2,ba=1
所以ab+ba=3
即偶+奇=奇(直接输出-1即可)
可以交换成功的,进行分类讨论:
只有ab+ba的和为偶数才可以交换成功,
而出现偶数的可能只有偶+偶=偶或者奇+奇=偶
故此时可以进行下一步判断哪个奇数哪个偶数
abababb
babbaba
ab=2,ba=2
所以ab+ba=4
即偶+偶=偶
所以ab和ab之间交换,ba和ba之间进行交换
abababbab
babbababa
ab=3,ba=3
所以ab+ba=6
即奇+奇=偶
把多余的那一项拿出来单独进行交换即可

1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 typedef long long ll; 7 const int N=2e5+20; 8 char s[N],t[N]; 9 int ab[N],ba[N];//记录下标 10 11 int main() 12 { 13 int n; 14 while(~scanf("%d",&n)) 15 { 16 scanf("%s%s",s,t); 17 // int ab=0,ba=0; 18 // for(int i=0;i<n;i++) 19 // { 20 // if(s[i]=='a'&&t[i]=='b') 21 // ab++; 22 // else if(s[i]=='b'&&t[i]=='a') 23 // ba++; 24 // else 25 // continue; 26 // } 27 // if(ab!=ba) 28 // printf("-1\n");//s=ab、t=ab这样子不对了就 29 int p=1,q=1; 30 for(int i=0; i<n; i++) 31 { 32 if(s[i]=='a'&&t[i]=='b') 33 ab[p++]=i+1; 34 else if(s[i]=='b'&&t[i]=='a') 35 ba[q++]=i+1; 36 else 37 continue; 38 } 39 p--,q--; 40 int sum=p+q; 41 if(sum%2)//如果和是奇数 42 { 43 printf("-1\n"); 44 continue; 45 } 46 if(p%2&&q%2) 47 //即奇+奇=偶 48 //把多余的那一项拿出来单独进行交换即可 49 { 50 int kk=(p-1)/2+(q-1)/2+1; 51 printf("%d\n",kk+1); 52 for(int i=1;i<=p-1;i+=2) 53 printf("%d %d\n",ab[i],ab[i+1]); 54 for(int i=1;i<=q-1;i+=2) 55 printf("%d %d\n",ba[i],ba[i+1]); 56 printf("%d %d\n%d %d\n",ba[q],ba[q],ba[q],ab[p]); 57 } 58 else if(p%2==0&&q%2==0) 59 //即偶+偶=偶 60 //所以ab和ab之间交换,ba和ba之间进行交换 61 { 62 printf("%d\n",(p+q)/2); 63 for(int i=1;i<=p;i+=2) 64 printf("%d %d\n",ab[i],ab[i+1]); 65 for(int i=1;i<=q;i+=2) 66 printf("%d %d\n",ba[i],ba[i+1]); 67 } 68 } 69 return 0; 70 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」