日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Swap Letters CodeForces - 1215C(贪心)

發(fā)布時(shí)間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Swap Letters CodeForces - 1215C(贪心) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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
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 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.
題意:互相交換兩串字符串中的若干字符,使得這兩串字符串變成相等的字符串。問最小的操作步數(shù)是多少,輸出操作步驟。
思路:如果某個(gè)字符個(gè)數(shù)是奇數(shù)的話,就不可能成功。
剩下的就是可以交換成功的情況了。如果兩個(gè)位置的字符相同的話就不用管它了。如果不相同的話,如果這一位置是ab,那么我們最優(yōu)先找的就是另一個(gè)位置也是ab的。因?yàn)檫@樣的話我們交換一次就可以使這兩個(gè)位置都是一樣的字符了。如果找不到的話,就只能和ba的交換了,這樣的話我們就交換兩次了。具體看代碼:
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=2e5+100; string s1,s2; vector<int> p[3]; struct node{int x,y; }ans[maxx]; int n;int main() {scanf("%d",&n);cin>>s1>>s2;int sum1=0,sum2=0;for(int i=0;i<n;i++) {if(s1[i]=='a') sum1++;if(s1[i]=='b') sum2++;if(s2[i]=='a') sum1++;if(s2[i]=='b') sum2++;}if((sum1&1)||(sum2&1)) {puts("-1");return 0;}for(int i=0;i<n;i++){if(s1[i]==s2[i]) continue;else if(s1[i]=='a'&&s2[i]=='b') p[1].push_back(i+1);else p[2].push_back(i+1);}int cnt=0,i,j;int Ans=0;for(i=1;i<p[1].size();i+=2){ans[++cnt].x=p[1][i];ans[cnt].y=p[1][i-1];Ans++;}for(j=1;j<p[2].size();j+=2){ans[++cnt].x=p[2][j];ans[cnt].y=p[2][j-1];Ans++;}if(p[1].size()%2&&p[2].size()%2)//如果存在這種情況的話,最多就一個(gè){Ans+=2;int len1=p[1].size()-1;int len2=p[2].size()-1;ans[++cnt].x=p[1][len1];ans[cnt].y=p[1][len1];ans[++cnt].x=p[1][len1];ans[cnt].y=p[2][len2];}cout<<Ans<<endl;for(int i=1;i<=cnt;i++) cout<<ans[i].x<<" "<<ans[i].y<<endl; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Swap Letters CodeForces - 1215C(贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。