POJ2159 Ancient Cipher;cstring和string区别;sort函数

问题:

Ancient Cipher

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 38044 Accepted: 12387

Description

Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers in those times were so called substitution cipher and permutation 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". 
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". 
It was quickly noticed that being applied separately, both substitution cipher and permutation cipher were rather weak. But when being combined, they were strong enough for those times. Thus, the most important messages were first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. Encrypting the message "VICTORIOUS" with the combination of the ciphers described above one gets the message "JWPUDJSTVP". 
Archeologists have recently found the message engraved on a stone plate. At the first glance it seemed completely meaningless, so it was suggested that the message was encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message that was encrypted, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.

Input

Input contains two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line. It also contains only capital letters of the English alphabet. 
The lengths of both lines of the input are equal and do not exceed 100.

Output

Output "YES" if the message on the first line of the input file could be the result of encrypting the message on the second line, or "NO" in the other case.

Sample Input

JWPUDJSTVP
VICTORIOUS

Sample Output

YES

大概意思:

这是个加密题,例子中说的是两种加密方式的示例(只是示例不是真正的加密算法)

1 从A到Y共25个字母,每个字母被它的后一个字母替换,最后一个Z用A替换

2 给一个排序的序列<2, 1, 5, 4, 3, 7, 6, 10, 9, 8> 给 "VICTORIOUS" 它就会把字母按照序列排序,最后成为"IVOTCIRSUO". 

两种方式单独应用密码很容易破解,但是要是结合着用会很强势(用完第一种用第二种),现在有考古人员发现了这种刻在石板上的加密后的密文,他们猜测明文,因此想要借助电脑程序来验证他们的猜测是否正确。

输入:共两行,两行字母个数相等,都不超过100个

             第一行:所有的标点都已经被去掉,都是大写英文字母;

             第二行:猜测的明文,都是大写英文字母;

输出:YES/NO

想法:

想法1(未成功):

从例子中看,第一种有点像凯撒加密(字母后移),但是题目没说一定是字母后移有可能是随机替换,准则是替换的那个字母不能重复(例子:不允许A替换为X,B也替换成X),但是一打乱顺序之后就有点麻烦。先打乱顺序还是先替换,效果一样。

想着先实现一下全排列,之后在循环中执行替换(先试试凯撒密码向后移动一个字母的情况,结果发现不是采用的这种加密方法),如果没有具体点的加密方式我还是没法实现。

想法2:

参照大佬的博客,相同的单词一定会变成另外相同的单词,经过排列只是位置变了,但是频率不变.因此两个字符串的频率一致就是关键,把出现频数相同的字母归结为一类(而不必理会组成这一类中的各个字母类型是否一致,但是字母的数量必须一致,例如密文中ABCD都分别出现一次,把他们归为一类,明文中BNUJ也都分别出现一次,把他们也归结为一类,那么这时认为ABCD和BNUJ这两类是等价的;但若密文中ABCD都分别出现一次,而明文中出现一次的只有 KIO三个字母,那么 ABCD和KIO是不等价的),并将出现的次数进行排序,再比较两个频数序列是否相同即可(比较的是频数列,而不是字母列),相同说明可由明文串经变换得出密文串(看这口气好像是说“至于怎么变换出来就不是我们该考虑的问题了,我们管不着!”  )

代码实现:

 想法1实现:      

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

#define N 10
string plaintext = "VICTORIOUS";	//明文
string cryptograph = "JWPUDJSTVP";	//密文	
string temp = "";
bool judge = false;

void isEncrypt(int k,int m)	//判断是否加密
{	
	//全排列,然后字母向后推移一个
	if (k > m)
	{
		temp = "";
		for (int i = 0; i <= m; i++)
		{
			temp += plaintext[i] + 1;	//字母向后推移一个
			//cout << plaintext[i];   
		}
		//cout << endl;  //不能加这个输出,因为占用的内存太大会abort()
		int  j = 0;
		for (; j < plaintext.length(); j++)
		{
			if (temp.at(j) != cryptograph.at(j))	//只要有不一样的地方就退出
				break;
		}
		if (j == plaintext.length())	//有不一样的会导致j不等于字母序列长度
			judge == true;
	}
	else
	{
		for (int i = k; i <= m; i++)
		{
			swap(plaintext.at(i), plaintext.at(k));
			isEncrypt(k + 1, m);
			swap(plaintext.at(i), plaintext.at(k));
		}
	}
}

int main()
{
	isEncrypt(0, plaintext.length() - 1);
	if (judge == true)
		cout << "YES";
	else
		cout << "NO";
	return 0;
}

 想法2实现:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

#define N 26

void count_frequency(string input, int *count)	//统计各字母出现的频次
{
	int  len = input.length();
	for (int i = 0; i < len; i++)
	{
		count[input[i] - 'A']++;
	}
	sort(count, count + 26);	//从小到大排列,参数都是地址
}

bool isSame(int *a1, int *a2)	//比较统计的频次是否相等
{
	for (int i = 0; i < 26; i++)
	{
		if (a1[i] != a2[i])
		{
			return false;
		}
	}
	return true;
}

int main()
{
	int count_plaintext[N] = { 0 };	//全初始化为0
	int count_cryptograph[N] = { 0 };
	string cryptograph;	//密文	
	string plaintext;	//明文
	cin >> cryptograph>>plaintext;
	count_frequency(plaintext, count_plaintext);
	count_frequency(cryptograph, count_cryptograph);
	if (isSame(count_plaintext, count_cryptograph))
		cout << "YES";
	else
		cout << "NO";
	return 0;
}

 

cstring和string区别

CString(cstring):CString是MFC或者ATL中的实现,是MFC里面封装的一个关于字符串处理的功能很强大的类,
只有支持MFC的工程才能使用,在MFC中使用不需要自己加,但在其他类型的程序中需要加#include<CString>。
如在linux上的工程就不能用CString(只有windows支持,微软支持),只能使用标准C++中的string类。

string:string为标准模板类(STL)定义的字符串类,已纳入C++标准中。另外因为string类是在标准C++库中,
使用之前需要声明名称空间std;而CString类并不在std命名空间中,因为它并不是c++标准库,只是一个微软的封装库。
所以string类的程序移植性更好。string类与CString类的方法接口也不太一样。一般我们使用cout重载string类,而不是CString类

string.h:c语言中关于字符数组的函数定义的头文件,常用函数有strlen,strcpy,strcmp等,
与string类无半点关系,所以头文件<string>、<string.h>是没有关系的两个东西   

string头文件使用

https://blog.csdn.net/superna666/article/details/52809007/
length()是考虑到传统C函数strlen而对应设置的,而size()是考虑到string作为一个STL容器,应该具有的common member.

algorithm:

https://baike.baidu.com/item/algorithm/1581833?fr=aladdin

sort函数:

sort(start,end,排序方法):
//起始地址,结束地址,排序方法(从小到大还是从大到小)

less<数据类型>()//从小到大排序
greater<数据类型>()//从大到小排序

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值