pku 2159 Ancient Cipher

怎么说呢,题目挺水的,题意理解错了,以为是凯撒排序, 然后两个快排,wa掉了- -|||

太恶心了,题目给的样例居然是用凯撒密码能过的!!!

然后看了Discuss,发现原来题目是要求判断两个字符串中,第一个串中出现的字母数及其频率,对应与第二个字符串出现的频率相同即可

例如:AABBCEF    BCCGGHJ,A出现的次数是2,B出现的次数是2, C,E,F都是1;第二串中 C,G出现2次,B,H,J出现一次,即频率是是相同的,输出YES

思路:用哈希表发就可以了,记录每个字母出现个数,然后两次快排, 比较就行了

0MS   144K

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
char fir[105],sec[105];
int hash1[30], hash2[30];

int main()
{
scanf("%s%s", fir,sec);
memset(hash1, 0, sizeof(hash1));
memset(hash2, 0, sizeof(hash2));
int n=strlen(fir);
for(int i=0; i<n; i++)
{
hash1[fir[i]-'A']++;
hash2[sec[i]-'A']++;
}

sort(hash1, hash1+26);
sort(hash2, hash2+26);
bool flag=1;
for(int i=25; i>=0; i--)
{
if(hash1[i]==0 && hash2[i]==0)//从小到大排序,当从后向前比较时,当第一次出现0后,之后所有比较均可略去,因为这之后两者都是0;
break;
if(hash1[i]!=hash2[i])
{
flag=0;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
return 0;
}
原文地址:https://www.cnblogs.com/FreeAquar/p/2005182.html