LA 3213 Ancient Cipher (水题,转化)

题意:给定两个长度相同的字符串,判断它们之间是否存在一一对应关系,顺序不定。

析:刚开始没看到顺序不定,然后写完没胡把样例看完就交了,结果WA了一次。。。其实这是一个水题,既然顺序不定,那么更简单,我们只要统计两个串中每个字母出现的次数,然后再排序(从大到小还是从小到大无所谓),只要它们的次数对应相等,那么它就可以一一对应。

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>

using namespace std;
const int maxn = 100 + 10;
char s1[maxn], s2[maxn];
int num1[26], num2[26];

int main(){
    while(~scanf("%s %s", s1, s2)){
        int n = strlen(s1);
        memset(num1, 0, sizeof(num1));
        memset(num2, 0, sizeof(num2));
        for(int i = 0; i < n; ++i)  ++num1[s1[i]-'A'];
        for(int i = 0; i < n; ++i)  ++num2[s2[i]-'A'];
        sort(num1, num1+26);
        sort(num2, num2+26);

        bool ok = true;
        for(int i = 0; i < 26; ++i)
            if(num1[i] != num2[i]) { ok = false;  break; }
        if(ok)  puts("YES");
        else  puts("NO");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5533161.html