uvalive 3213 Ancient Cipher

题目:给定两个只含大写字母的等长字符串,问两者之间是否存在一一映射

分析:考察一一映射的概念,将两个字符串分别作字母统计,再按字母出现个递增的顺序排序(排列的是每个字母出现的个数),如果排序后结果一样那么两者是一一映射

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string>
 4 #include <algorithm>
 5 #define zz
 6 using namespace std;
 7 int main(){
 8 #ifndef zz
 9     freopen("in.txt", "r", stdin);
10 #endif
11     string s, t;
12     while(cin>>s>>t){
13         int ch1[26], ch2[26];
14         int i, j;
15         for(i=0; i<26; i++){
16             ch1[i] = 0;
17             ch2[i] = 0;
18         }
19         for(i=0; i<s.length(); i++){
20             ch1[s[i]-'A']++;
21             ch2[t[i]-'A']++;
22         }
23         sort(ch1, ch1+26);
24         sort(ch2, ch2+26);
25         bool ok = true;
26         for(i=0; i<26 && ok; i++){
27             if(ch1[i]!=ch2[i])
28                 ok = false;
29         }
30         if(ok) puts("YES");
31         else puts("NO");
32     }
33     return 0;
34 }
原文地址:https://www.cnblogs.com/zjutzz/p/2909796.html