// 思维 C. Swap Letters Codeforces Round #585 (Div. 2)

C. Swap Letters
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters “a” and “b”.

Monocarp wants to make these two strings s and t equal to each other. He can do the following operation any number of times: choose an index pos1 in the string s, choose an index pos2 in the string t, and swap spos1 with tpos2.

You have to determine the minimum number of operations Monocarp has to perform to make s and t 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 n (1≤n≤2⋅105) — the length of s and t.

The second line contains one string s consisting of n characters “a” and “b”.

The third line contains one string t consisting of n characters “a” and “b”.

Output
If it is impossible to make these strings equal, print −1.

Otherwise, in the first line print k — the minimum number of operations required to make the strings equal. In each of the next k lines print two integers — the index in the string s and the index in the string t that should be used in the corresponding swap operation.

Examples
inputCopy
4
abab
aabb
outputCopy
2
3 3
3 2
inputCopy
1
a
b
outputCopy
-1
inputCopy
8
babbaabb
abababaa
outputCopy
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 s with the third letter in t. Then s= “abbb”, t= “aaab”. Then swap the third letter in s and the second letter in t. Then both s and t are equal to “abab”.

In the second example it’s impossible to make two strings equal.

  • 设S序列与T序列不同时 aa表示这种情况下si为 a的情况,bb 表示这种情况下si为b的情况
  • 首先如果(aa+bb)%2==1也就是S中a ,b的元素个数是奇数 也就是不可能分成两个一致的
  • 想想看最合适的情况就是 aa %2==0 那么就直接考虑 考虑这个 不合理的si里面的a和si+1对应的ti+1的b 对调这样就可以使两两一致了
  • 然而以上是针对于aa 和bb都是偶数的情况 如果aa 与bb 是奇数那么最后肯定剩下是这样的情况
  • 设S 中a的位置为pa ;b的位置为pb
    S:a - b
    T:b - a
    考虑 S中最后一个没被分配的a 和这个a对应的T 中的b 换也就是pa->pa
    S:b- b
    T:a- a
    之后考虑 pa->pb
    也就是
    S:a- b
    T:a- b
#include<bits/stdc++.h>
using namespace std;
char s[200010],t[200010];
int main()
{
	int n;scanf("%d",&n);
	scanf("%s",s+1);
	scanf("%s",t+1);
	vector<int>a,b;
	for(int i=1;i<=n;i++)
	{
		if(s[i]!=t[i])
		{
			if(s[i]=='a') a.push_back(i);
			else b.push_back(i);
		}
	}
	if((a.size()+b.size())%2)
	{
		printf("-1\n");return 0;
	}
	printf("%d\n",(a.size()+b.size())/2+a.size()%2);
	for(int i=0;i+1<a.size();i+=2)
	{
		printf("%d %d\n",a[i],a[i+1]);
	}
	for(int i=0;i+1<b.size();i+=2)
	{
		printf("%d %d\n",b[i],b[i+1]);
	}
	if(a.size()%2)// 
	{
		printf("%d %d\n",a.back(),a.back());
		printf("%d %d\n",a.back(),b.back());
	}
}

UP UP PANDA

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值