uva 1339 Ancient Cipher(字符串处理)

题目大意:给出两个字符串,判断两个字符串中的字符是否一一对应,一一对应指的是字符串中某个字符的个数与另一个字符串中某一个字符的个数相同(字符不可以重复考虑)

解题思路:开个数组统计字符的个数,用sort函数排序后比较。


#include <cstdio>
#include <cstring>

int main() {
	char A[110], B[110];
	while (scanf("%s%s", A, B) != EOF) {
		int len_1 = strlen(A);
		int len_2 = strlen(B);
		int cnt_1[30] = {0}, cnt_2[30] = {0};
		bool ok = true;
		for (int i = 0; i < len_1; i++)
			cnt_1[A[i]-'A']++;
		for (int i = 0; i < len_2; i++)
			cnt_2[B[i]-'A']++;

		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 26; j++)
				if (cnt_1[i] == cnt_2[j])
					cnt_1[i] = cnt_2[j] = 0;
			if (cnt_1[i])
				ok = false;
		}

		if (ok && len_1 == len_2)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


你可能感兴趣的:(uva 1339 Ancient Cipher(字符串处理))