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

Copy
4
abab
aabb
output

Copy
2
3 3
3 2
input

Copy
1
a
b
output

Copy
-1
input

Copy
8
babbaabb
abababaa
output

Copy
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.

题意:

给出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
即奇+奇=偶
把多余的那一项拿出来单独进行交换即可

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=2e5+;
char s[N],t[N];
int ab[N],ba[N];//记录下标 int main()
{
int n;
while(~scanf("%d",&n))
{
scanf("%s%s",s,t);
// int ab=0,ba=0;
// for(int i=0;i<n;i++)
// {
// if(s[i]=='a'&&t[i]=='b')
// ab++;
// else if(s[i]=='b'&&t[i]=='a')
// ba++;
// else
// continue;
// }
// if(ab!=ba)
// printf("-1\n");//s=ab、t=ab这样子不对了就
int p=,q=;
for(int i=; i<n; i++)
{
if(s[i]=='a'&&t[i]=='b')
ab[p++]=i+;
else if(s[i]=='b'&&t[i]=='a')
ba[q++]=i+;
else
continue;
}
p--,q--;
int sum=p+q;
if(sum%)//如果和是奇数
{
printf("-1\n");
continue;
}
if(p%&&q%)
//即奇+奇=偶
//把多余的那一项拿出来单独进行交换即可
{
int kk=(p-)/+(q-)/+;
printf("%d\n",kk+);
for(int i=;i<=p-;i+=)
printf("%d %d\n",ab[i],ab[i+]);
for(int i=;i<=q-;i+=)
printf("%d %d\n",ba[i],ba[i+]);
printf("%d %d\n%d %d\n",ba[q],ba[q],ba[q],ab[p]);
}
else if(p%==&&q%==)
//即偶+偶=偶
//所以ab和ab之间交换,ba和ba之间进行交换
{
printf("%d\n",(p+q)/);
for(int i=;i<=p;i+=)
printf("%d %d\n",ab[i],ab[i+]);
for(int i=;i<=q;i+=)
printf("%d %d\n",ba[i],ba[i+]);
}
}
return ;
}

CodeForces-1215C-Swap Letters-思维的更多相关文章

  1. Codeforces 1215C. Swap Letters

    传送门 好像是个挺显然的贪心 首先每次交换当然要尽量一次交换就多两个相同的位置 即 优先把 $\begin{bmatrix}a\\ b\end{bmatrix}$ 和 $\begin{bmatrix} ...

  2. C. Swap Letters 01字符串最少交换几次相等

    C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #585 (Div. 2) C. Swap Letters

    链接: https://codeforces.com/contest/1215/problem/C 题意: Monocarp has got two strings s and t having eq ...

  4. Codeforces Round 56-B. Letters Rearranging(思维)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. Educational Codeforces Round 60 C 思维 + 二分

    https://codeforces.com/contest/1117/problem/C 题意 在一个二维坐标轴上给你一个起点一个终点(x,y<=1e9),然后给你一串字符串代表每一秒的风向, ...

  6. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  7. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  8. Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】

    B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...

  9. Codeforces Problem 708A Letters Cyclic Shift

     题目链接: http://codeforces.com/problemset/problem/708/A 题目大意: 从字符串s中挑选出一个子串(非空),将该子串中的每个字母均替换成前一个字母,如' ...

  10. Codeforces 675C Money Transfers 思维题

    原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...

随机推荐

  1. App应用推广

    Android应用推广渠道: 360手机助手: http://dev.360.cn/ 应用宝: http://open.qq.com/ 百度手机助手: http://shouji.baidu.com/ ...

  2. WIN7下怎么安装iis教程

    点击开始→控制面板,然后再点击程序,勿点击卸载程序,否则到不了目标系统界面. 2 然后在程序和功能下面,点击打开和关闭windows功能. 3 进入Windows功能窗口,然后看到internet信息 ...

  3. smali与baksmali用法-基于2.2.2版本

    下载地址:https://bitbucket.org/JesusFreke/smali/downloads/?tab=downloads 反编译dex java -jar baksmali-2.2.1 ...

  4. 你真的懂return吗?

    递归算法中什么时候用return啥时候不用呢? 使用2个例子来说明:快速排序和二分查找 # 二分查找def binarySearch (arr, l, r, x): # 基本判断 if r >= ...

  5. Linux操作基础

    摘要 一.Linux操作系统概述 二.Linux操作系统安装 三.Linux文件系统及文件基础 四.Linux操作系统命令使用基础 五.Linux应用程序的安装与卸载基础 五.用户及进程 六.相关信息 ...

  6. 大道浮屠诀---NBU7.7.3_oracle11G单机-单机(异机恢复WINDOWS2008平台)

    现有环境说明: 一台WINDOWS2008R2:安装有NBU7.7.3,作为服务端 一台WINDOWS2008R2:安装有oracle11.2.0.3,作为数据库服务器 现假设数据库意外崩溃,需要进行 ...

  7. C-Ubuntu中MySQL出现ERROR1698(28000):Access denied for user root@localhost错误解决方法

    卸载原先的MySQL,重新安装过程中没有提示设置密码, 当键入命令:mysql -uroot -p; 不知密码是什么,输入主机的密码以及不输入密码,回车后都出现标题错误. 寻找多篇相关博文,验证如下博 ...

  8. 利用MySqlBulkLoader生成csv文件,批量添加数据

    DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Yw ...

  9. CR0 - CR4 ,5个寄存器,留念,每次都要翻手册,太费事了

  10. 在GitHub上编辑README.md排版样式

    如何在github的readme.md编辑出好看的样式排版文章呢...? 参考:https://blog.csdn.net/u012067966/article/details/50736647