• CodeForces1215CSwap Letters思维


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

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<iostream>
     5 using namespace std;
     6 typedef long long ll;
     7 const int N=2e5+20;
     8 char s[N],t[N];
     9 int ab[N],ba[N];//记录下标
    10 
    11 int main()
    12 {
    13     int n;
    14     while(~scanf("%d",&n))
    15     {
    16         scanf("%s%s",s,t);
    17 //        int ab=0,ba=0;
    18 //        for(int i=0;i<n;i++)
    19 //        {
    20 //            if(s[i]=='a'&&t[i]=='b')
    21 //                ab++;
    22 //            else if(s[i]=='b'&&t[i]=='a')
    23 //                ba++;
    24 //            else
    25 //                continue;
    26 //        }
    27 //        if(ab!=ba)
    28 //            printf("-1\n");//s=ab、t=ab这样子不对了就
    29         int p=1,q=1;
    30         for(int i=0; i<n; i++)
    31         {
    32             if(s[i]=='a'&&t[i]=='b')
    33                 ab[p++]=i+1;
    34             else if(s[i]=='b'&&t[i]=='a')
    35                 ba[q++]=i+1;
    36             else
    37                 continue;
    38         }
    39         p--,q--;
    40         int sum=p+q;
    41         if(sum%2)//如果和是奇数
    42         {
    43             printf("-1\n");
    44             continue;
    45         }
    46         if(p%2&&q%2)
    47         //即奇+奇=偶
    48         //把多余的那一项拿出来单独进行交换即可
    49         {
    50             int kk=(p-1)/2+(q-1)/2+1;
    51             printf("%d\n",kk+1);
    52             for(int i=1;i<=p-1;i+=2)
    53                 printf("%d %d\n",ab[i],ab[i+1]);
    54             for(int i=1;i<=q-1;i+=2)
    55                 printf("%d %d\n",ba[i],ba[i+1]);
    56             printf("%d %d\n%d %d\n",ba[q],ba[q],ba[q],ab[p]);
    57         }
    58         else if(p%2==0&&q%2==0)
    59         //即偶+偶=偶
    60         //所以ab和ab之间交换,ba和ba之间进行交换
    61         {
    62             printf("%d\n",(p+q)/2);
    63             for(int i=1;i<=p;i+=2)
    64                 printf("%d %d\n",ab[i],ab[i+1]);
    65             for(int i=1;i<=q;i+=2)
    66                 printf("%d %d\n",ba[i],ba[i+1]);
    67         }
    68     }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    MMU_段式映射
    MMU段式映射(VA -> PA)过程分析
    NOR FLASH驱动程序
    PCB上 如何显示 汉字
    poj1273 Drainage Ditches
    poj2349 Arctic Network
    poj3660 Cow Contest
    poj3259 Wormholes
    poj3159 Candies
    poj1011 Sticks
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11675664.html
Copyright © 2020-2023  润新知