GYM 101755 M.Forgotten Spell(水~)

253 篇文章 2 订阅

Description

给出三个字符串 a,b,c a , b , c ,已知这三个字符串和 s s 串至多差一个字符,求s

Input

三个等长的只由小写字母组成的字符串 a,b,c, a , b , c , ,串长不超过 2105 2 ⋅ 10 5

Output

如果无解则输出 Impossible I m p o s s i b l e ,如果多解则输出 Ambiguous A m b i g u o u s ,否则输出唯一解

Sample Input

aab
aca
daa

Sample Output

aaa

Solution

由于每个串和 s s 至多差一个字符,那么a b,c b , c 分别至多差两个字符,找出 a a 字符串与b,c字符串不同的位置,显然不同的位置数不超过 3 3 个,否则无解,首先判断a本身是不是合法解,然后枚举这三个位置中的一个为 a a s相异的位置,然后枚举该位置的字符判断是否有解,统计解的数量即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=200005;
char s[3][maxn];
int n,vis[maxn];
bool check()
{
    for(int i=1;i<=2;i++)
    {
        int num=0;
        for(int j=0;j<n;j++)
            if(s[0][j]!=s[i][j])
            {
                num++;
                if(num>1)return 0;
            }
    }
    return 1;
}
int main()
{
    for(int i=0;i<3;i++)scanf("%s",s[i]);
    n=strlen(s[0]);
    int m=0,flag=1,pos[10];
    for(int i=1;i<=2;i++)
    {
        for(int j=0;j<n;j++)
            if(s[0][j]!=s[i][j])vis[j]=1;
    }
    for(int i=0;i<n;i++)
        if(vis[i])
        {
            pos[m++]=i;
            if(m>4)break;
        }
    if(m>3)flag=0;
    int ans=0,anspos;
    char ansc;
    if(flag)
    {
        if(check())ans++,anspos=0,ansc=s[0][0];
        for(int i=0;i<m;i++)
        {
            char t=s[0][pos[i]];
            for(int j=0;j<26;j++)
                if('a'+j!=t)
                {
                    s[0][pos[i]]='a'+j;
                    if(check())ans++,anspos=pos[i],ansc=j+'a';
                }
            s[0][pos[i]]=t;
        }
        if(ans==0&&m)flag=0;
        if(m==0)ans=2;
    }
    if(!flag)printf("Impossible\n");
    else if(ans>1)printf("Ambiguous\n");
    else 
    {
        s[0][anspos]=ansc;
        printf("%s\n",s[0]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值