C. Swap Letters

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
  1. 4
  2. abab
  3. aabb
output
  1. 2
  2. 3 3
  3. 3 2
input
  1. 1
  2. a
  3. b
output
  1. -1
input
  1. 8
  2. babbaabb
  3. abababaa
output
  1. 3
  2. 2 6
  3. 1 3
  4. 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.

题意:给你两个字符串,问最少需要交换多少次可以使这两个字符串相等,并且输出交换方案

题解:因为字符只有a,b两种;所以不相等的时候只有两种情况

1、

   

  b

2、

   b

  a

分别统计这两种情况的出现次数,用k1,k2表示

如果k1,k2有一个为奇数,一个为偶数   ,即(k1+k2)%2==1,则不可能交换之后两字符串相等,输出 -1

否则 先让同一种不相等情况的先两两交换,交换次数为k1/2+k2/2

最后判断k1,k2是否都是奇数,如果是的话,最后只剩下如下一组不相等的情况

a  b

b  a

这里需要交换两次才能使两字符串相等

------------1

b  b

a  a

------------2

b  a

b  a

------------

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<math.h>
  5. #include<stack>
  6. #include<string.h>
  7. #include<string>
  8. #include<vector>
  9. #define ll long long
  10. using namespace std;
  11. int pos1[],pos2[];
  12. int main()
  13. {
  14. string s1,s2;
  15. int t;
  16. cin>>t;
  17. cin>>s1;
  18. cin>>s2;
  19.  
  20. int k1=,k2=;
  21. for(int i=;i<t;i++)
  22. {
  23. if(s1[i]!=s2[i]&&s1[i]=='a')
  24. pos1[k1++]=i+;//输出下标是从1开始
  25.  
  26. if(s1[i]!=s2[i]&&s1[i]=='b')
  27. pos2[k2++]=i+;
  28. }
  29. if((k1+k2)%==)//k1,k2一个为奇数,一个为偶数
  30. cout<<-<<endl;
  31. else
  32. {
  33. int cnt=k1/+k2/;
  34. if(k1%==&&k2%==)//如果k1,k2为奇数最后交换的时候要交换两次
  35. cnt=cnt+;
  36. cout<<cnt<<endl;
  37. int i,j;
  38. for(i=;i+<k1;i=i+)
  39. cout<<pos1[i]<<' '<<pos1[i+]<<endl;
  40.  
  41. for(j=;j+<k2;j=j+)
  42. cout<<pos2[j]<<' '<<pos2[j+]<<endl;
  43.  
  44. if(i!=k1&&j!=k2)//处理最后一次交换下标
  45. {
  46. cout<<pos1[k1-]<<' '<<pos1[k1-]<<endl;
  47. cout<<pos1[k1-]<<' '<<pos2[k2-]<<endl;
  48. }
  49. }
  50.  
  51. return ;
  52. }

C. Swap Letters 01字符串最少交换几次相等的更多相关文章

  1. hiho #1326 : 有序01字符串

    #1326 : 有序01字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个01字符串,你每次可以将一个0修改成1,或者将一个1修改成0.那么,你最少需要修改 ...

  2. hiho 有序01字符串 dp

    题目1 : 有序01字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个01字符串,你每次可以将一个0修改成1,或者将一个1修改成0.那么,你最少需要修改多少 ...

  3. 使序列有序的最少交换次数(minimum swaps)

    交换相邻两数 如果只是交换相邻两数,那么最少交换次数为该序列的逆序数. 交换任意两数 数字的总个数减去循环节的个数?? A cycle is a set of elements, each of wh ...

  4. 深度优先搜索 codevs 1065 01字符串

    codevs 1065 01字符串  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 输出仅有0和1组成的长度为n的字符串,并且 ...

  5. Codevs 1065 01字符串

    1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 输出仅有0和1组成的长度为n的字符串,并且其中不能含有 ...

  6. codevs——1065 01字符串

    1065 01字符串  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 输出仅有0和1组成的长度为n的字符串, ...

  7. NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))

    Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...

  8. P1071 01字符串的交叉安排

    题目描述 你有 \(n(1 \le n \le 10^6)\) 个字符'0' 和 \(m(1 \le m \le 10^6)\) 个字符'1'.你需要使用这些字符拼接成一个01字符串,使得满足如下两个 ...

  9. Java实现 LeetCode 777 在LR字符串中交换相邻字符(分析题)

    777. 在LR字符串中交换相邻字符 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作.一次移动操作指用一个"L ...

随机推荐

  1. 根据IP地址查找MAC地址

    ping 地址 arp -a得到ip对应的mac

  2. django urls.py 中的name 使用方法

    使用场景: 当我们在url的时候,一般情况下都是使用很明确的url地址.如在网页里面使用<a href="/login">登录</a>.像这样的链接有很 多 ...

  3. 喵星之旅-狂奔的兔子-centos7一键安装redmine

    一.安装环境 CentOS-7-x86_64-DVD-1908.iso 二.获取安装文件 从官网获取,在下载页面提供了多种安装,最下方是一键安装版本,里面有两种选择,一个是安装包,一个是虚拟机硬盘文件 ...

  4. 7,请描述下cookies,sessionStorage和localStorage的区别

    7,请描述下cookies,sessionStorage和localStorage的区别 首先,cookie是网站为了标识用户身份而储存在用户本地终端(client side,百科: 本地终端指与计算 ...

  5. java中4种常用线程池

    一.线程池 线程池:说白了,就是一种线程使用模式.线程过多会带来调度开销,进而影响整体性能.而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务,这避免了在处理短时间任务时创建与销毁线程的代价 ...

  6. Mysql 中使用 utfmb4 需要注意的问题

    查资料时看到一个前人的经验总结,非常有用: http://seanlook.com/2016/10/23/mysql-utf8mb4/

  7. Fluent_Python_Part3函数即对象,06-dp-1class-func,一等函数与设计模式

    使用一等函数实现设计模式 中文电子书P278 合理利用作为一等对象的函数,把模式中涉及的某些类的实例替换成简单的函数,从而简化代码. 1. 重构"策略"模式 中文电子书P282 P ...

  8. JDBC 预编译语句对象

    Statement的安全问题:Statement的执行其实是直接拼接SQL语句,看成一个整体,然后再一起执行的. String sql = "xxx"; // ? 预先对SQL语句 ...

  9. CentOS7安装jenkis

    注意:终止运行Ctrl+c , 退回到shell命令Ctrl+d 一.先检查是否有java [root@huangyh huangyh]#  rpm -qa |grep java 或 java 因为C ...

  10. 吴裕雄 python 神经网络——TensorFlow 图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt image_raw_data = tf.gfile ...