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. Delphi 获取系统当前进程、窗口句柄、文件属性以及程序运行状态

    uses TLHelp32,PsAPI;(1)显示进程列表: procedure TForm1.Button2Click(Sender: TObject); var lppe: TProcessEnt ...

  2. Hbase节点的管理|服役和退役节点

    Base节点的管理 1.服役(commissioning) 当启动regionserver时,regionserver会向Hmaster注册并开始接收本地数据,开始的时候,新加入的节点不会有任何数据, ...

  3. thinkphp 表单令牌

    表单令牌 ThinkPHP支持表单令牌验证功能,可以有效防止表单的重复提交等安全防护.要启用表单令牌功能,需要配置行为绑定, 在应用或者模块的配置目录下面的行为定义文件tags.php中 就是在你的 ...

  4. VMProtect使用小计【二】 – 加壳查看

    Release 我这里使用的是Release的版本,Debug的版本分析没有多少的必要,因为程序发布过之后就是Release的,我们先看一下这个文件 原程序分析 使用OD打开VMProtectDemo ...

  5. CSS:CSS 媒体类型

    ylbtech-CSS:CSS 媒体类型 1.返回顶部 1. CSS 媒体类型 媒体类型允许你指定文件将如何在不同媒体呈现.该文件可以以不同的方式显示在屏幕上,在纸张上,或听觉浏览器等等. 媒体类型 ...

  6. c# winForm DotNetBar控件之SuperGridControl

    1.添加表头 sgc.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row;//点击选中一行 DevComponents.DotNet ...

  7. string中的stringstream

    博客: 加速:ios::sync_with_stdio(false); 举个例子: 題目:输入的第一行有一个数字 N 代表接下來有 N 行资料,每一行资料里有不固定个数的整数(最多20个,每行最大20 ...

  8. python中 try、except、finally执行顺序

    我们虽然经常用到try...except 作为异常补货,但是其实很少去研究try源码和机制,也许点进去看过,也是看不出个所以然来 class Exception(BaseException): &qu ...

  9. 制作一个自己的xhprof测试平台

    1 1.首先安装php开发环境,比如lnmp. 2.安装xhprof ps: 记住从github上面下载(https://github.com/phacility/xhprof), 不要从pecl.p ...

  10. Python脚本的name