Description
给出三个字符串 a,b,c a , b , c ,已知这三个字符串和 s s 串至多差一个字符,求串
Input
三个等长的只由小写字母组成的字符串 a,b,c, a , b , c , ,串长不超过 2⋅105 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 至多差一个字符,那么和 b,c b , c 分别至多差两个字符,找出 a a 字符串与字符串不同的位置,显然不同的位置数不超过 3 3 个,否则无解,首先判断本身是不是合法解,然后枚举这三个位置中的一个为 a a 和相异的位置,然后枚举该位置的字符判断是否有解,统计解的数量即可
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;
}