• Codeforces Round #303 (Div. 2) B 水 贪心


    B. Equidistant String
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:

    We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti.

    As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.

    It's time for Susie to go to bed, help her find such string p or state that it is impossible.

    Input

    The first line contains string s of length n.

    The second line contains string t of length n.

    The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.

    Output

    Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).

    If there are multiple possible answers, print any of them.

    Examples
    input
    0001
    1011
    output
    0011
    input
    000
    111
    output
    impossible
    Note

    In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.

     题意:给你两个长度相同的01串s,t   找到一个新的01串p  

    使得p与s对应位置 字符不同的数量 等于 p与t对应位置 字符不同 的数量

    不存在则输出impossible

     题解:遍历一遍统计s,t对应位置字符不同的数量jishu 并标记位置

    如果jishu是奇数 则输出impossible 否则 取s串 对于标记的位置  取反jishu/2个位置的值

    输出

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 //#include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<queue>
    14 #include<cmath>
    15 #define ll __int64
    16 #define PI acos(-1.0)
    17 #define mod 1000000007
    18 using namespace std;
    19 char a[100005];
    20 char b[100005];
    21 int mp[100005];
    22 int main()
    23 {
    24     memset(mp,0,sizeof(mp));
    25     scanf("%s",a);
    26     scanf("%s",b);
    27     int len=strlen(a);
    28     int jishu=0;
    29     for(int i=0;i<len;i++)
    30     {
    31         if(a[i]!=b[i])
    32             {
    33 
    34              jishu++;
    35             mp[i]=1;
    36             }
    37     }
    38     if(jishu%2)
    39     {
    40         printf("impossible
    ");
    41         return 0;
    42     }
    43     jishu/=2;
    44     for(int i=0;i<len;i++)
    45     {
    46         if(jishu)
    47         {
    48             if(mp[i])
    49             {
    50                 if(a[i]=='1')
    51                 printf("0");
    52                 else
    53                 printf("1");
    54                 jishu--;
    55             }
    56             else
    57               printf("%c",a[i]);
    58         }
    59         else
    60             printf("%c",a[i]);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    HDU 1124 Factorial(简单数论)
    29.QT主窗口加widget
    28.开始画面和异形窗口
    27.Qt时钟
    26.QT颜色与布局
    25.QT进度条
    146.正则表达式
    24.qint64转QString 以及获取文件属性
    23.QFile遍历
    22.监视文件
  • 原文地址:https://www.cnblogs.com/hsd-/p/5733465.html
Copyright © 2020-2023  润新知