古代密码

其实很简单的啊,就简简单单的分别记录两个数组中每个字母出现的次数,分别排序,判断是否相同就好。

我看完题,想出了一个极其错误的思路:直接把储存字符的数组排序,再判断a[ i ] + (b[1] - a[1])是否等于b[ i ] 。为什么错误呢?因为这么加不会使Z跳回A而会使Z跳到其它字符(根据ascii码)。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char aa[1005],bb[1005];
int a[1005],b[1005],lena,lenb;
bool cmp(int a,int b)
{
    return a > b;
}
int main()
{
    scanf("%s%s",aa+1,bb+1);
    lena = strlen(aa+1);
    lenb = strlen(bb+1);
    if(lena != lenb)
    {
        printf("NO");
        return 0;
    }
    for(int i = 1;i <= lena;i++)
        a[aa[i]]++;
    for(int i = 1;i <= lenb;i++)
        b[bb[i]]++;
    sort(a + 0,a + 100+1,cmp);
    sort(b + 0,b + 100+1,cmp);
    for(int i = 1;i <= lena;i++)
    {
        if(a[i] != b[i])
        {
            printf("NO");
            return 0;
        }
    }
    printf("YES");
    return 0;
} 
View Code
原文地址:https://www.cnblogs.com/darlingroot/p/11288447.html