问题大意是问通过两种加密方式可不可以由第一个字符串转换到第二个
(1)调换字母的顺序
(2)把一个字母映射到另一个字母
实质就是问两个字符串中字母出现次数的关系,如果每个字母都能在另一个字符串中找到
出现次数相等的字母则两个字符串可以互相替换。
做法:
先用一个哈希表统计两个每个字符串各个字母出现的次数,哈希函数是该字母在字母表里的位置,所以大小为26。
然后对哈希表各个位置进行排序,如果两个哈希表排序结果相同则两个字符串可以互换否则不能。
</pre>源代码:<pre name="code" class="html">#include<iostream>
#include<Cstdio>
#include <algorithm>
#include<Cstring>
using namespace std;
char input1[101], input2[101];
int hash1[26], hash2[26];
void ShellSort(int array[],int length)
{
int d = length/2; //设置希尔排序的增量
int i ;
int j;
int temp;
while(d>=1)
{
for(i=d;i<length;i++)
{
temp=array[i];
j=i-d;
while(j>=0 && array[j]<temp)
{
array[j+d]=array[j];
j=j-d;
}
array[j+d] = temp;
}
d= d/2; //缩小增量
}
}
int main()
{
char *p1, *p2, i;
bool flag;
while(scanf("%s%s", input1, input2) != EOF)
{
p1 = input1;
p2 = input2;
memset(hash1, 0, sizeof(hash1));
memset(hash2, 0, sizeof(hash1));
while(*p1 != '\0')
{
hash1[(*p1) - 'A']++;
p1 ++;
}
while(*p2 != '\0')
{
hash2[(*p2) - 'A']++;
p2 ++;
}
ShellSort(hash1, 26);
ShellSort(hash2, 26);
flag = true;
for(i = 0; i < 26; ++i)
{
if(hash1[i] != hash2[i])
{
flag = false;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}