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)/~

Swap Letters CodeForces - 1215C(贪心)相关推荐

  1. CodeForces - 1215C Swap Letters(暴力+思维+模拟)

    题目链接:点击查看 题目大意:给出两个只由字母a和字母b所组成的字符串,我们记为s和t,现在我们可以交换两个字符串任意位置的字母(只能在两个串之间交换,不能在自己串中交换),现在问能否通过一定次数的交 ...

  2. 贪心 ---- Codeforces Global Round 8,B. Codeforces Subsequences[贪心,贪的乘法原理]

    题目链接 给出字符串,统计子串(子串字母可以跳跃)是codeforces的数量. 本题要求,给出子串最少数量k,构造字符串s,要求字符串s包含的字母数量最少,输出这个最少的字符串s. 题目要求是至少有 ...

  3. Minimize the Permutation CodeForces - 1256(贪心)

    题意: q次询问,每次询问给你长度为n的排列,然后你每次可以选择一个位置i和i+1的数字进行交换.但是每个位置只能交换一次,问你反转若干次后,这个排列最小是多少? 题目: You are given ...

  4. CodeForces - 93B(贪心+vectorpairint,double +double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

  5. Codeforces 985C (贪心)

    传送门 题面: C. Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. Minimizing Difference CodeForces - 1244E(贪心题)

    题目 题意 官方题解: 百度翻译 思路 ac代码 题意 给出一列数,至多n个操作使其中的数+1或-1,要求得到最小的差值(最大值-最小值): You are given a sequence a1_{ ...

  7. Serval and Parenthesis Sequence CodeForces - 1153C 贪心

    题意:给出一个由"(",")","?"三种字符构成的序列,让我们把其中的问号替换成左右括号,使得整个序列变成一个完整地括号序列,也就是括号匹 ...

  8. CodeForces - 1089L 贪心

    The kingdom of Lazyland is the home to nn idlers. These idlers are incredibly lazy and create many p ...

  9. Too Many Segments (easy version) CodeForces - 1249D1(贪心+差分)

    题意 给多组线段,而每一个点的覆盖次数不超过K,每次可去除一个线段,问最少去多少线段以及线段的位置. The only difference between easy and hard version ...

最新文章

  1. Caught exception java.lang.interruptedException(在集群上进行多个文件合并压缩时出错)
  2. C++中的以任意字符分割字符串
  3. UVA11520填充正方形
  4. ping 问题网络翻滚问题小结
  5. linux rdate
  6. Windows进程管理类封装
  7. 用递归的方式处理数组 把递归方法方法定义到数组的原型上 (这是一次脑洞大开的神奇尝试)...
  8. 苹果 CEO 库克“喜当爹”,被女子索赔31.6亿分手费!
  9. BZOJ4141 THUSC2013 魔塔 贪心
  10. java怎么连高斯数据库_Gauss DB 数据库使用(二) Data Studio
  11. Ubuntu配置maven环境变量
  12. YarnAllocator:Container killed by YARN for exceeding memory limits. spark.yarn.executor.memoryOverhe
  13. 怎么获取公众号二维码?
  14. 计算机文档字体替换,在word中巧妙使用字体替换
  15. python中文件最重要的功能_重点汇总-python-gitbook-重要点学习-1
  16. 【JAVA】LeetCode力扣 第199场周赛 题解+代码
  17. C++调用Armadillo计算库
  18. 连接IBM MQ原因码报2537的错误解决记录
  19. formatDate方法
  20. 十分钟读懂游戏研发、发行、渠道那些事儿

热门文章

  1. python能干啥、实际生活-学习Python可以做什么?从事哪些岗位?
  2. 剑指offer(05)用两个栈实现队列
  3. 低功耗STM32F411开发板(原理图+PCB源文件+官方例程+驱动等)
  4. 17年三月计算机二级,2017年3月计算机二级考试攻略
  5. python文件可以包括任何数据内容_python 文件数据合并(数据行不对应)?
  6. python模块下载连接清华镜像的具体步骤_如何下载Pycharm开源版以及设置国内镜像源-百度经验...
  7. 多多自走棋改动_多多自走棋腾讯版
  8. MySql中有哪些存储引擎
  9. Promise async/await的理解和用法
  10. 病毒行为分析初探(三)