题目1485:W's Cipher

题目描述:

Weird Wally's Wireless Widgets, Inc. manufactures an eclectic assortment of small, wireless, network capable devices, ranging from dog collars, to pencils, to fishing bobbers. All these devices have very small memories. Encryption algorithms like Rijndael, the candidate for the Advanced Encryption Standard (AES) are demonstrably secure but they don't fit in such a tiny memory. In order to provide some security for transmissions to and from the devices, WWWW uses the following algorithm, which you are to implement.
Encrypting a message requires three integer keys, k1, k2, and k3. The letters [a-i] form one group, [j-r] a second group, and everything else ([s-z] and underscore) the third group. Within each group the letters are rotated left by ki positions in the message. Each group is rotated independently of the other two. Decrypting the message means doing a right rotation by ki positions within each group.
Consider the message the_quick_brown_fox encrypted with ki values of 2, 3 and 1. The encrypted string is _icuo_bfnwhoq_kxert. The figure below shows the decrypting right rotations for one character in each of the three character groups.

Looking at all the letters in the group [a-i] we see {i,c,b,f,h,e} appear at positions {2,3,7,8,11,17} within the encrypted message. After a right rotation of k1=2, these positions contain the letters {h,e,i,c,b,f}. The table below shows the intermediate strings that come from doing all the rotations in the first group, then all rotations in the second group, then all the rotations in the third group. Rotating letters in one group will not change any letters in any of the other groups.

All input strings contain only lowercase letters and underscores(_). Each string will be at most 80 characters long. The ki are all positive integers in the range 1-100.

输入:

Input consists of information for one or more encrypted messages. Each problem begins with one line containing k1, k2, and k3 followed by a line containing the encrypted message. The end of the input is signalled by a line with all key values of 0.

输出:

For each encrypted message, the output is a single line containing the decrypted string.

样例输入:
2 3 1
_icuo_bfnwhoq_kxert
1 1 1
bcalmkyzx
3 7 4
wcb_mxfep_dorul_eov_qtkrhe_ozany_dgtoh_u_eji
2 4 3
cjvdksaltbmu
0 0 0
样例输出:
the_quick_brown_fox
abcklmxyz
the_quick_brown_fox_jumped_over_the_lazy_dog
ajsbktcludmv


英文描述,要好好读懂题意。

对属于不同group中的字符分别进行保存,这里要注意的是,要保存该字符所在的位置而不是字符本身,因为一个字符可能出现多次,如果保存字符本身的话,在后续的匹配过程中,会出现错误。使用map保存rotation前和rotation后的对应关系。

#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
char str[81], res[81];
int group1[81], group2[81], group3[81];
map<int, int> m1, m2, m3;

int main() {
	int k1, k2, k3;
	while (scanf("%d%d%d", &k1, &k2, &k3) != EOF) {
		if (k1 == 0 && k2 == 0 && k3 == 0)
			break;
		scanf("%s", str);
		strcpy(res, str);
		int len1 = 0, len2 = 0, len3 = 0;
		m1.clear();
		m2.clear();
		m3.clear();
		int len = strlen(str);
		for (int i = 0; i < len; i++) {
			if (str[i] >= 'a' && str[i] <= 'i')
				group1[len1++] = i;
			if (str[i] >= 'j' && str[i] <= 'r')
				group2[len2++] = i;
			if (str[i] >= 's' && str[i] <= 'z')
				group3[len3++] = i;
			if (str[i] == '_')
				group3[len3++] = i;
		}
		for (int i = 0; i < len1; i++)
			m1.insert(make_pair(group1[(i + k1) % len1], group1[i]));
		for (int i = 0; i < len2; i++)
			m2.insert(make_pair(group2[(i + k2) % len2], group2[i]));
		for (int i = 0; i < len3; i++)
			m3.insert(make_pair(group3[(i + k3) % len3], group3[i]));
		for (int i = 0; i < len; i++) {
			if (str[i] >= 'a' && str[i] <= 'i')
				res[i] = str[m1[i]];
			if (str[i] >= 'j' && str[i] <= 'r')
				res[i] = str[m2[i]];
			if (str[i] >= 's' && str[i] <= 'z')
				res[i] = str[m3[i]];
			if (str[i] == '_')
				res[i] = str[m3[i]];
		}
		printf("%s\n", res);
	}
	return 0;
}

题目链接:

http://ac.jobdu.com/problem.php?pid=1485

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值