POJ 2159 Ancient Cipher 水题

密码加密,密码都是大写英文字母,有两种加密方式,替换方式排列方式。替换就是把每个字母用别的字母替换,而且不能重复。排列方式就是把字母重新排列顺序。这两种方式混合使用。现在给你A字符串和B字符串,A是密文,问B是否是原文。

排列的解决方法比较简单,就把两个字符串都按字典序排序,看看是否相同即可。
替换的解决方式,观察每个字母的出现次数,然后就可以得到。
综合一下就是,统计每个字母的出现次数,然后排序,观察是否相同。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char str1[105], str2[105];
int cnt1[26], cnt2[26];
int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%s%s", str1, str2);
    for (int i = 0; i < strlen(str1); i++)
    {
        cnt1[str1[i] - 'A']++;
    }
    for(int i = 0; i < strlen(str2); i++)
    {
        cnt2[str2[i] - 'A']++;
    }
    sort(cnt1, cnt1 + 26);
    sort(cnt2, cnt2 + 26);
    bool flag = true;
    for (int i = 0; i < 26; ++i)
    {
        if (cnt1[i] != cnt2[i])
        {
            flag = false;
            break;
        }
    }
    if (flag)
        printf("YES\n");
    else
        printf("NO\n");
    //printf("end\n");
    //while (1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/code12hour/article/details/81225551
今日推荐