UVa:1339 Ancient Cipher

确实没有想到这个题的思路,我还尝试用搜索来做。

这俩种加密方式都是1对1的,所以不会改变某个字母出现的次数。

这样只要统计明文和密文中字母出现次数是否都一致即可。

 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#define MAXN 105
#define MOD 1000000007
#define INF 10000000
#define ll long long
using namespace std;
int main()
{
    char str1[MAXN],str2[MAXN];
    while(scanf("%s%s",str1,str2)!=EOF)
    {
        int L=strlen(str1),cnt1[MAXN]= {0},cnt2[MAXN]= {0};
        bool ok=true;
        for(int i=0; i<L; ++i)
        {
            int a=str1[i]-'A',b=str2[i]-'A';
            cnt1[a]++;
            cnt2[b]++;
        }
        vector<int> vec1,vec2;
        for(int i=0; i<=26; ++i)
        {
            if(cnt1[i]) vec1.push_back(cnt1[i]);
            if(cnt2[i]) vec2.push_back(cnt2[i]);
        }
        sort(vec1.begin(),vec1.end());
        sort(vec2.begin(),vec2.end());
        for(int i=0; i<vec1.size(); ++i)
            if(vec1[i]!=vec2[i])
            {
                ok=false;
                break;
            }
        if(!ok||strlen(str1)!=strlen(str2)) puts("NO");
        else puts("YES");
    }
    return 0;
}


 

你可能感兴趣的:(UVa:1339 Ancient Cipher)