1 solutions

  • 0
    @ 2025-1-3 14:23:10

    算法分析

    从前往后扫描,然后记录匹配关系,如果扫描的过程中发现冲突就直接结束,否则就在扫描结束的时候,判断是否26个字母都匹配完成了

    代码实现

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+10;
    int fs[N],ft[N];
    string s,t;
    int main()
    {
        string a;
    	cin>>s>>t>>a;
    	int n=s.size(),m=t.size();
    	bool st=true;
    	int cnt=0;
    	for(int i=0;i<n;i++)
    	{
    		if(fs[s[i]]==0) //fs表示从明文到密文
    		{
    		    cnt++; //出现了一个字母
    			fs[s[i]]=t[i];
    		}
    		else
    		{
    			if(fs[s[i]]!=t[i])
    			{
    				st=false;
    			}
    		}
    		if(ft[t[i]]==0) //ft表示从密文到明文
    		{
    			ft[t[i]]=s[i];
    		}
    		else
    		{
    			if(ft[t[i]]!=s[i])
    			{
    				st=false;
    			 } 
    		}
    	}
    	if(st&&cnt==26) //没有出现冲突且26个字母都可以找到
    	{
    	    string t;
    	    for(auto x:a)
    	    {
    	        t+=fs[x];
    	    }
    	    cout<<t;
    	}
    	else cout<<"Failed";
    	return 0;
    }
    
    • 1

    Information

    ID
    1080
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    # Submissions
    1
    Accepted
    1
    Uploaded By