这题的话,我们可以把字符串序列里面的字母直接计数,然后比较两个数组里面的数字是否一一相同,然后就可以直接判定YES or NO。
因为它题目中说的就是一种映射的关系,首先我们读入之后,把两个字符串的不同字母个数给统计出来,然后我们做一个排序,排序之后,如果这两个数字序列的长短是一样的,就说明有映射的可能性。
然后我们一一比较,只有每次每个从前向后的数组格子里面的填的数字个数相同才是可以的,因为这样直接就可以把密码序列里面的字母映射到答案序列的字母上,然后就得到答案了,这题不要想太多。
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int cnt1[30],cnt2[30];
bool cmp(int a,int b)
{
return a > b;
}
int main()
{
string a, s;
while (cin>>s>>a) {
bool flag = 1;
memset(cnt1, 0, sizeof(cnt1));
memset(cnt2, 0, sizeof(cnt2));
for (int i = 0; i < s.length(); i++) {
cnt1[s[i] - 'A']++;
cnt2[a[i] - 'A']++;
}
sort(cnt1, cnt1 + 30,cmp);
sort(cnt2, cnt2 + 30,cmp);
for (int i = 0; cnt1[i] != 0;i++) {
if (cnt1[i]==cnt2[i]) {
continue;
}
else {
flag=0;
break;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}