Uva 1339:Ancient Cipher
Posted xietx1995
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Uva 1339:Ancient Cipher相关的知识,希望对你有一定的参考价值。
题目传送门:Uva 1339:Ancient Cipher
题目大意:第二个字符串的每个字母和另一个字母一一映射之后,再重新排列能否得到地一个字符串。
思路:一一映射和重排只是改变了字母的值和位置,但是没有改变字母种类的数量,所以只需统计两个字符串中每种字母出现的次数,然后对出现次数进行排序,然后比较排序后的两个数组即可。
例如:
HAHB
MEMC
从ROME到MAMA,字母种类还是只有三种,每种字母出现的次数分别为2、1、1(M、E、C)及2、1、1(H、A、B)。所以是什么字母并不重要。
AC代码:
#include <stdio.h>
#include <string.h>
#include <memory.h>
void swap(int* a, int* b)
int t = *a;
*a = *b;
*b = t;
int partition(int a[], int p, int q)
int x = a[q];
int i = p - 1, j;
for (j = p; j < q; ++j)
if (a[j] < x)
swap(&a[++i], &a[j]);
swap(&a[++i], &a[q]);
return i;
void quickSort(int a[], int p, int q)
if (p < q)
int m = partition(a, p, q);
quickSort(a, p, m - 1);
quickSort(a, m + 1, q);
int main()
char a[105], b[105];
int countA[26], countB[26];
while (2 == scanf("%s%s", a, b))
memset(countA, 0, sizeof(countA));
memset(countB, 0, sizeof(countB));
int len = strlen(a), i;
for (i = 0; i < len; ++i)
countA[a[i] - 'A']++;
countB[b[i] - 'A']++;
quickSort(countA, 0, 25);
quickSort(countB, 0, 25);
for (i = 0; i < 26; ++i)
if (countA[i] != countB[i])
break;
if (i == 26)
printf("YES\\n");
else
printf("NO\\n");
return 0;
以上是关于Uva 1339:Ancient Cipher的主要内容,如果未能解决你的问题,请参考以下文章