la 3213 Ancient Cipher

题目链接: 点击打开链接

题目大意: 给定两个长度均为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");
		else
			printf("NO\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值