POJ 2159 Ancient Cipher 杂题

题意:给定 str1, str2, 如果 str2 经过加密可以变成 str1。 输出YES,否则输出NO. 加密方式有两种,一种是改变字符,一种是调换顺序。
题解:这题还是耽搁了一会儿。一开始把题意理解错了,将substitution cipher (置换密码):当做按字典序偏移任意个位置。所以一直WR。

看了别人的解释:

substitution cipher (置换密码):

Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different.For some letters substitute letter may coincide with the original letter. For example, applying substitution cipher that changes all letters from 'A' to 'Y' to the next ones in the alphabet, and changes 'Z' to 'A', to the message "VICTORIOUS" one gets the message "WJDUPSJPVT".

“移位”只是置换密码的一种,只要满足“Substitutes for all letters must be different.”就是置换了,比如

A->B

C->H

Z->D

对于这道题,input:

AAABB
CCCEE

应输出

YES

所以明文与密文的“字母频率的数组”应该是一样的,即明文中某字母出现8次,密文中也必须有某个字母出现8次。

所以字母种类,字母频率同时相等时,即被破解。

permutation cipher(排列密码):

Permutation cipher applies some permutation to the letters of the message. For example, applying the permutation <2, 1, 5, 4, 3, 7, 6, 10, 9, 8> to the message "VICTORIOUS" one gets the message "IVOTCIRSUO".

明文随机排列,所以字母种类和频率都不变。

对于这道题,排列密码考虑和不考虑对解题无影响!”


#include <cstring>
#include <iostream>
using namespace std;

char first[101],second[101];
int frequency[2][30];

int main()
{
	cin >> first >> second;
	int i, j, counter;
	int len = strlen(first);

	for ( i = 0; i < len; i++ )
	{
		frequency[0][first[i]-'A']++;
		frequency[1][second[i]-'A']++;
	}

	for ( i = 0; i < 26; i++ )
	{
		for ( j = 0; j < 26; j++ )
		{
			if ( frequency[0][i] == frequency[1][j] )
			{
				frequency[1][j] = -1; /* 当frequency[1][j] 被匹配过一次后,将其频率置为-1 */
				break;
			}
		}
		if ( j==26 ) break;
	}

	if ( i==26 )
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值