【字符串大模拟】

学到了神奇的优化

cin优化,能跑的比scanf快!棒!

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char s1[1001],s2[1001],s3[1001];
int f[300],f2[300],len1,len2,len3;
int main()
{
    ios::sync_with_stdio(false);
    cin>>s1>>s2>>s3;
    len1=strlen(s1); len2=strlen(s2);
    if (len1<26) 
    {
        cout<<"Failed";
        return 0;
    }
    memset(f,-1,sizeof(f));
    memset(f2,-1,sizeof(f2));
    for (int i=0; i<len1; i++)
    {
        if ((f[s1[i]-'A']!=-1||f2[s2[i]-'A']!=-1)&&f2[s2[i]-'A']!=s1[i]-'A') 
            {
                cout<<"Failed";
                return 0;
            }
        if (f[s1[i]-'A']==-1) 
        {
            f[s1[i]-'A']=s2[i]-'A';
            f2[s2[i]-'A']=s1[i]-'A';
        }
    }
    for (int i=0; i<26; i++)
    if (f[i]==-1)
    {
        cout<<"Failed";
        return 0;
    }
    len3=strlen(s3);
    for (int i=0; i<len3; i++)
    {
        char c=f[s3[i]-'A']+'A';
        cout<<c;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Phantomhive/p/11829073.html