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. AJAX(包括跨域)post请求封装

    function ajaxPost(dataUrl, parameter, callback, bef_callback, com_callback, err_callback) { $.ajax({ ...

  2. webpack最基本的用法

    webpack 安装 webpack是所以Node.js开发的工具,可通过npm安装,首先要保证node已经安装完毕,可以去node官网下载, 然后通过npm下载webpack npm install ...

  3. JCF——工具类

  4. PHP学习(MVC架构与面向对象)

    想好好的学一下php中的一些面向对象的知识,以前只是为了打CTF随意的学了一下,但是为了以后的代码审计(准备PHP这边把thinkphp这个框架好好的学一下). PHP面向对象的基本知识 类与对象 类 ...

  5. 【UR #2】跳蚤公路

    [UR #2]跳蚤公路 参照yjc方法.也就是地铁环线那个题. 求每个点不在负环内的x的取值范围.然后所有1到j能到i的j的范围取交.得到答案. 每个边形如kx+b的直线,每个环也是 每个点不在负环内 ...

  6. Android_开发片段(Part 3)

    1.Android中的五种布局方式:线性布局(Linear Layout).相对布局(Relative Layout).表格布局(Table Layout).网格视图(Grid View).标签布局( ...

  7. error LNK2001: 无法解析的外部符号 __imp__RegEnumKeyExA@32

    错误: error LNK2001: 无法解析的外部符号 __imp__OpenProcessToken@12 error LNK2001: 无法解析的外部符号 __imp__LookupPrivil ...

  8. 10、jqueryEasyUI感觉自己还是改行做前端吧

    1.jquery的局部方法开发 //首先引入jquery的相关包组件 <script type="text/javascript" src="../js/jquer ...

  9. hbase启动的时候报:cat: /home/hadoop/hbase-0.94.6-cdh4.5.0/target/cached_classpath.txt: 没有那个文件或目录

    启动hbase的时候: -cdh4.5.0/bin$ hbase shell cat: /home/hadoop/hbase--cdh4.5.0/target/cached_classpath.txt ...

  10. 入门 Webpack,看这篇就够

    写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...