紫薯P73 例题4-1

一、题意

有若干组数据。
每组两个字符串。
判断这两个字符串能否用一种映射规则(某个字母映射成另一个字母)进行映射。

二、解析

数每个字母的个数即可。最后个数排列后完全一样的话说明可以映射。

三、代码

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int cnt1[26], cnt2[26];

int main() {
    string str1, str2;
    while(cin >> str1 >> str2) {
        fill(cnt1, cnt1 + 26, 0);
        fill(cnt2, cnt2 + 26, 0);
        for(auto ch : str1) cnt1[ch - 'A'] ++;
        for(auto ch : str2) cnt2[ch - 'A'] ++;
        sort(cnt1, cnt1 + 26);
        sort(cnt2, cnt2 + 26);
        int ok = 1;
        for(int i = 0; ok && i < 26; i ++) if(cnt1[i] != cnt2[i]) ok = 0;
        cout << (ok ? "YES" : "NO") << endl;
    }
}

四、归纳