题目来源:http://poj.org/problem?id=1107
Description
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
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.
Output
For each encrypted message, the output is a single line containing the decrypted string.
Sample Input
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
Sample Output
the_quick_brown_fox
abcklmxyz
the_quick_brown_fox_jumped_over_the_lazy_dog
ajsbktcludmv
此题题意是将26个字母和"_"分成3段,a~i为第一段,j~r为第二段,其他的为第三段。拿第一组测试数据来说,分为3组分别为:
t1{i c b f h e}
t2{o n o q k r}
t3{_ u _ w _ x t}
k值分别为k1=2,k2=3, k3=1,t1,t2,t3分别向右旋转ki布后得到的新数据分别为:
t1'{h e i c b f}
t2'{q k r o n o}
t3'{t _ u _ w _ x }
将跟换后的新数据替换掉在原字符串的位置即可
#include <stdio.h>
#include <string.h>
void rotate(char str[], int len, int k)
{
//将字符串向右旋转k个单位
int i, j;
char str1[81], str2[81];
k = len-k%len;
for(i = 0; i < k; i++)
{
str1[i] = str[i];
}
str1[i] = '\0';
j = 0;
for(i = k; i < len; i++)
{
str2[j++] = str[i];
}
str2[j] = '\0';
strcat(str2, str1);
strcpy(str, str2);
}
int main()
{
int i;
int k1, k2, k3;
int len1, len2, len3, len;
int n1[81], n2[81], n3[81];
char t1[81], t2[81], t3[81], str[81];
while(1)
{
scanf("%d %d %d", &k1, &k2, &k3);
if((k1+k2+k3) == 0) break;
scanf("%s", str);
len = strlen(str);
len1 = len2 = len3 = 0;
memset(t1, '\0', sizeof(t1));
memset(t2, '\0', sizeof(t2));
memset(t3, '\0', sizeof(t3));
for(i = 0; i < len; i++)
{
if(str[i] >= 'a' && str[i] <= 'i')
{
t1[len1] = str[i];
n1[len1++] = i;
}
else if(str[i] >= 'j' && str[i] <= 'r')
{
t2[len2] = str[i];
n2[len2++] = i;
}
else
{
t3[len3] = str[i];
n3[len3++] = i;
}
}
if(len1) rotate(t1, len1, k1);
if(len2) rotate(t2, len2, k2);
if(len3) rotate(t3, len3, k3);
for(i = 0; i < len1; i++)
{
str[n1[i]] = t1[i];
}
for(i = 0; i < len2; i++)
{
str[n2[i]] = t2[i];
}
for(i = 0; i < len3; i++)
{
str[n3[i]] = t3[i];
}
printf("%s\n", str);
}
return 0;
}
分享到:
相关推荐
北大POJ2159-Ancient Cipher 解题报告+AC代码
poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客
POJ题解及题目分类,共150道左右。C/C++
POJ部分题解
poj acm题解,包括绝大部分poj题目的题解,可以供acm爱好者学习研究
如果你为在poj上找不到解题思路而痛苦,那么这本书可以为你带来惊喜,里面包括了poj上一部分题解题报告~
Poj1155 题解 , 文档 , 资料
PKU OJ上的部分解题报告和源程序 分享一下
ACM题解 训练指南 北大ACM题解 北大ACM训练指南 北大ACM题解训练指南 北京大学ACM题目 源代码 POJ源代码 POJ做指南
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
经典O(n)求最长回文 acmer新手可看
C + + language learning poj100 question bank and code
1000 1002 1003 1004 1005 1006 1007 1008 1011 1012 1013 1019 1028 1045 1046 1068 1080 1088 1163 1207 1218 1256 1298 1299 1316 1326 1401 1455 1477 1488 1503 1504 1517 1519 1547 1552 1565 1579 1607 1656 ...
北大POJ2262-Goldbach's Conjecture 解题报告+AC代码
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
北大POJ2388-Who's in the Middle 解题报告+AC代码
POJ上的DP分类: 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈), 1742, 1887, 1926(马尔科夫矩阵,求...
W, X, and Y map to 9 There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is ...
acm poj题目分类介绍 包含一个题解文档 acm poj题目分类介绍 包含一个题解文档