• C. Swap Letters


    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

    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.

    =========================================================================

    首先不匹配有两种情况

    a b

    b a

    如果两种情况都是偶数,那么一定能够置换成功,且只需要对单个情况每两个进行两两组合即可

    如果两个都是奇数,那么两个各剩下一个,单独耗费两次(参考样例1)

    如果一奇一偶,那么显然是不可以的

    1. # include
    2. #include
    3. # include
    4. # include
    5. # include
    6. # include
    7. using namespace std;
    8. typedef long long int ll;
    9. vector<int>v1,v2;
    10. int main()
    11. {
    12. int n;
    13. cin>>n;
    14. string a,b;
    15. cin>>a>>b;
    16. for(int i=0; i
    17. {
    18. if(a[i]=='a'&&b[i]=='b')
    19. v1.push_back(i+1);
    20. else if(a[i]=='b'&&b[i]=='a')
    21. v2.push_back(i+1);
    22. }
    23. if((v1.size()+v2.size())%2)
    24. {
    25. cout<<-1;
    26. }
    27. else
    28. {
    29. if(v1.size()%2==0&&v2.size()%2==0)
    30. {
    31. cout<<(v1.size()+v2.size())/2<
    32. if(v1.size())
    33. {
    34. for(int i=0; isize(); i+=2)
    35. {
    36. cout<" "<1]<
    37. }
    38. }
    39. if(v2.size())
    40. {
    41. for(int i=0; isize(); i+=2)
    42. {
    43. cout<" "<1]<
    44. }
    45. }
    46. }
    47. else if(v1.size()%2&&v2.size()%2)
    48. {
    49. cout<<(v1.size()+v2.size()-2)/2+2<
    50. if(v1.size())
    51. {
    52. for(int i=0; i+1size(); i+=2)
    53. {
    54. cout<" "<1]<
    55. }
    56. }
    57. if(v2.size())
    58. {
    59. for(int i=0; i+1size(); i+=2)
    60. {
    61. cout<" "<1]<
    62. }
    63. }
    64. cout<size()-1]<<" "<size()-1]<
    65. cout<size()-1]<<" "<size()-1]<
    66. }
    67. }
    68. return 0;
    69. }

  • 相关阅读:
    JVM内存模型和结构详解(五大模型图解)
    商汤&上海AI实验室联合发布:自动驾驶全栈式高精度标定工具箱(含车、IMU、相机、激光雷达等的标定)
    006-JavaSE基础巩固练习:while循环结构的练习
    【无标题】
    基于三段式命令及筛选器的rbac权限控制方案
    数据结构与算法8-排序算法:插入排序、希尔排序、归并排序
    这三款手机视频拼接软件,可以帮你把视频拼出高级感
    供应链 | 顶会CIKM论文精读:面向大规模三维装箱问题的数据驱动树形搜索算法
    ubuntu20.04禁止自动休眠的几种方式
    建筑能源管理(3)——建筑能源监管
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126323507