恶心思维模拟 记出现三种,两种,一种字符的位置的数量分别为v3 v2 v1
(1) v3>1 不可能 因为每人最多错一次
(2) v3==1
(1) v2>1 不可能
(2) v2==1 这时情况很复杂 很难继续分类讨论 直接暴力枚举出现三种和两种字符的那两个位置 然后看有几种结果
(3) 模棱两可 可能有一人是对的 有可能全错
(3) v3==0 情况更复杂... 见代码
#include <bits/stdc++.h>
using namespace std;
int pre[4][200010];
int n;
char a[200010],b[200010],c[200010],ans[200010];
int solve(char aa,char bb,char cc)
{
int book[30];
int i,res;
memset(book,0,sizeof(book));
book[aa-'a']++,book[bb-'a']++,book[cc-'a']++;
res=0;
for(i=0;i<26;i++)
{
if(book[i]!=0) res++;
}
return res;
}
int gou(int p1,int p2,char aa,char bb)
{
int book[4][3];
int i;
memset(book,0,sizeof(book));
if(a[p1]==aa) book[1][1]=1;
if(a[p2]==bb) book[1][2]=1;
if(b[p1]==aa) book[2][1]=1;
if(b[p2]==bb) book[2][2]=1;
if(c[p1]==aa) book[3][1]=1;
if(c[p2]==bb) book[3][2]=1;
for(i=1;i<=3;i++)
{
if(!book[i][1]&&!book[i][2]) return 0;
}
return 1;
}
int gougou(int p1,int p2,int p3,char aa,char bb,char cc)
{
int book[4][4];
int i,j,cnt;
memset(book,0,sizeof(book));
if(a[p1]==aa) book[1][1]=1;
if(a[p2]==bb) book[1][2]=1;
if(a[p3]==cc) book[1][3]=1;
if(b[p1]==aa) book[2][1]=1;
if(b[p2]==bb) book[2][2]=1;
if(b[p3]==cc) book[2][3]=1;
if(c[p1]==aa) book[3][1]=1;
if(c[p2]==bb) book[3][2]=1;
if(c[p3]==cc) book[3][3]=1;
for(i=1;i<=3;i++)
{
cnt=0;
for(j=1;j<=3;j++)
{
if(!book[i][j]) cnt++;
}
if(cnt>1) return 0;
}
return 1;
}
int main()
{
int book[4];
int i,res,p;
int cnt,p1,p2,p3;
char tem,ii,jj,kk;
while(scanf("%s%s%s",a,b,c)!=EOF)
{
memset(book,0,sizeof(book));
n=strlen(a);
for(i=0;i<n;i++)
{
res=solve(a[i],b[i],c[i]);
book[res]++;
pre[res][book[res]]=i;
}
if(book[3]>1)
{
printf("Impossible\n");
}
else if(book[3]==1)
{
if(book[2]==0)
{
printf("Ambiguous\n");
}
else if(book[2]==1)
{
cnt=0,p1=pre[3][1],p2=pre[2][1];
memcpy(ans,a,sizeof(a));
for(ii='a';ii<='z';ii++)
{
for(jj='a';jj<='z';jj++)
{
if(gou(p1,p2,ii,jj))
{
cnt++;
ans[p1]=ii,ans[p2]=jj;
}
}
}
if(cnt==0)
{
printf("Impossible");
}
else if(cnt>1)
{
printf("Ambiguous\n");
}
else
{
printf("%s\n",ans);
}
}
else
{
printf("Impossible\n");
}
}
else
{
if(book[2]==0)
{
printf("Ambiguous\n");
}
else if(book[2]==1)
{
printf("Ambiguous\n");
}
else if(book[2]==2)
{
cnt=0,p1=pre[2][1],p2=pre[2][2];
memcpy(ans,a,sizeof(a));
for(ii='a';ii<='z';ii++)
{
for(jj='a';jj<='z';jj++)
{
if(gou(p1,p2,ii,jj))
{
cnt++;
ans[p1]=ii,ans[p2]=jj;
}
}
}
if(cnt==0)
{
printf("Impossible");
}
else if(cnt>1)
{
printf("Ambiguous\n");
}
else
{
printf("%s\n",ans);
}
}
else if(book[2]==3)
{
cnt=0,p1=pre[2][1],p2=pre[2][2],p3=pre[2][3];
memcpy(ans,a,sizeof(a));
for(ii='a';ii<='z';ii++)
{
for(jj='a';jj<='z';jj++)
{
for(kk='a';kk<='z';kk++)
{
if(gougou(p1,p2,p3,ii,jj,kk))
{
cnt++;
ans[p1]=ii,ans[p2]=jj,ans[p3]=kk;
}
}
}
}
if(cnt==0)
{
printf("Impossible");
}
else if(cnt>1)
{
printf("Ambiguous\n");
}
else
{
printf("%s\n",ans);
}
}
else
{
printf("Impossible\n");
}
}
}
return 0;
}