la 3213 Ancient Cipher

来源:互联网 发布:新开淘宝店铺 编辑:程序博客网 时间:2024/06/06 08:30

题目链接: 点击打开链接

题目大意: 给定两个长度均为n的字符串,判断其中一个字符串重新排列后,每个字母能否和另一个字符串的字母一一对应.比如ABB和CDD.

思路: 字符串匹配

分析:

1. 两个字符串如果能一一对应,那么对应的字母出现次数一定是相同的.

2. 因此对每个字符串,计算所有字母出现的次数,再依次比对这些次数.如果有不相同的次数说明无法一一对应.

代码:

#include <iostream>#include <algorithm>#include <string>#include <cstdio>#include <memory.h>using namespace std;const int maxn = 30;int main(){string a, b;while (cin >> a >> b){int a_count[maxn], b_count[maxn], i;memset(a_count, 0, sizeof(a_count));memset(b_count, 0, sizeof(b_count));int l = a.length();for (i = 0; i < l; ++i){++a_count[a[i]-'A'];++b_count[b[i]-'A'];}sort(a_count, a_count+26);sort(b_count, b_count+26);for (i = 25; i >= 0; --i)if (a_count[i] != b_count[i])break;if (i == -1)printf("YES\n");elseprintf("NO\n");}return 0;}


原创粉丝点击