P1071 潜伏者

· · 题解

#include<bits/stdc++.h>
using namespace std;
string a,b,c;
char f[27];
bool r[27];
char w[27];
int p;
long long abl,cdl;
void find()
{
    for (int i=0;i<=25;i++)
    {
        if (f[i]=='0')
        {
            p=1;
        }
    }
    return;
}
void realize()
{
    int x,y;
    for (int i=0;i<abl;i++)
    {
        x=a[i]-'A';
        y=b[i]-'A';
        if (f[x]!=b[i]&&f[x]!='0')
        {
            p=1;
            return;
        }
        if (r[y]==1&&w[y]!=a[i])
        {
            p=1;
            return;
        }
        else
        {
            f[x]=b[i];
            r[y]=1;
            w[y]=a[i];
        }
    }
    return;
}
void change()
{
    int x;
    for (int i=0;i<cdl;i++)
    {
        x=c[i]-'A';
        cout<<f[x];
    }
    return;
}
int main()
{
    memset(f,'0',sizeof(f));
    cin>>a>>b>>c;
    abl=a.size();
    cdl=c.size();
    realize();
    find();
    if (p==1)
    {
        cout<<"Failed"<<endl;
        return 0;
    }
    change();
    cout<<endl;
    return 0;
}