Codeforces Round #303 (Div. 2) B. Equidistant String 水题

本文探讨如何根据两个只包含0和1的字符串构造第三个字符串,使得它与这两个字符串在相同位置的不同字符数量相等。若存在多个解,任选其一;若不存在解,则输出impossible。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Equidistant String

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/545/problem/B

Description

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.

 

Sample Input

0001
1011

Sample Output

0011

HINT

 

题意

给你两串只含01的字符串,然后让你构造出一组字符串,让他们的和构造出来的字符串差异都相同

题解:

首先看这两组有多少个位置不一样,如果是奇数,直接输出不可能,如果是偶数,那么就前一半为s,后一半为t就好了

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

string s;
string t;
int main()
{

    cin>>s>>t;
    int flag=0;
    int n=s.size();
    if(s==t)
    {
        cout<<s<<endl;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        if(s[i]!=t[i])
            flag++;
    }
    if(flag%2==1)
    {
        printf("impossible\n");
    }
    else
    {
        flag/=2;
        for(int i=0;i<n;i++)
        {
            if(s[i]!=t[i])
            {
                if(flag>0)
                {
                    cout<<s[i];
                    flag--;
                }
                else
                    cout<<t[i];
            }
            else
                cout<<s[i];
        }
    }
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4516130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值