Swap Letters CodeForces - 1215C(贪心)

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.
题意:互相交换两串字符串中的若干字符,使得这两串字符串变成相等的字符串。问最小的操作步数是多少,输出操作步骤。
思路:如果某个字符个数是奇数的话,就不可能成功。
剩下的就是可以交换成功的情况了。如果两个位置的字符相同的话就不用管它了。如果不相同的话,如果这一位置是ab,那么我们最优先找的就是另一个位置也是ab的。因为这样的话我们交换一次就可以使这两个位置都是一样的字符了。如果找不到的话,就只能和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)//如果存在这种情况的话,最多就一个
	{
		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)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值