POJ - 1107 W's Cipher
2013-11-20 15:13
381 查看
POJ - 1107 W's Cipher
Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
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_foxabcklmxyz
the_quick_brown_fox_jumped_over_the_lazy_dog
ajsbktcludmv
Source
Mid-Central USA 20011 #include<stdio.h> 2 #include<string.h> 3 char str[100] = {0}; 4 char str1[100] = {0}; 5 char str2[100] = {0}; 6 char str3[100] = {0}; 7 8 int main() 9 { 10 int a, b, c, len, alen, blen, clen; 11 while(scanf("%d%d%d", &a, &b, &c)) { 12 if(a == 0 && b == 0 && c == 0) { 13 break; 14 } 15 scanf("%s", str); 16 len = strlen(str); 17 alen = blen = clen = 0; 18 for(int i = 0; i < len; i++) { 19 if(str[i] >= 'a' && str[i] <= 'i') { 20 str1[alen] = str[i]; 21 alen++; 22 } 23 else if(str[i] >= 'j' && str[i] <= 'r') { 24 str2[blen] = str[i]; 25 blen++; 26 } 27 else { 28 str3[clen] = str[i]; 29 clen++; 30 } 31 } 32 // puts(str1); 33 // puts(str2); 34 // puts(str3); 35 36 37 if(alen != 0) a = a % alen;//这一步是关键 38 if(blen != 0) b = b % blen;//这一步是关键 39 if(clen != 0) c = c % clen;//这一步是关键 40 int t1 = 0, t2 = 0, t3 = 0; 41 for(int i = 0; i < len; i++) { 42 if(str[i] >= 'a' && str[i] <= 'i') { 43 printf("%c", str1[(t1-a+alen)%alen]); 44 t1++; 45 } 46 else if(str[i] >= 'j' && str[i] <= 'r') { 47 printf("%c", str2[(t2-b+blen)%blen]); 48 t2++; 49 } 50 else { 51 printf("%c", str3[(t3-c+clen)%clen]); 52 t3++; 53 } 54 } 55 printf("\n"); 56 } 57 58 59 return 0; 60 }
相关文章推荐
- poj 1107 --W's Cipher
- POJ 1107:W's Cipher(模拟)
- acm pku 1107 W's Cipher的具体实现方法
- POJ_1659 Frogs' Neighborhood
- POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
- poj 1041 John's trip(欧拉回路)
- poj 3422 Kaka's Matrix Travels(最小费用最大流)
- poj 2965 The Pilots Brothers' refrigerator 普通dfs 超时 暑假第二题
- POJ 2411 Mondriaan's Dream(铺装问题) 状态DP
- [POJ 3207]Ikki's Story IV - Panda's Trick(2-SAT入门题)
- poj 2528 Mayor's posters
- POJ 1041 John's trip(无向图欧拉回路 && 路径)
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat)
- [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)
- poj 2262 Goldbach's Conjecture 【素数筛】
- poj 2388 Who&#39;s in the Middle
- POJ 2449 Remmarguts' Date Dij +A*
- POJ 3985 Knight's Problem bfs + 剪枝
- poj 2965 The Pilots Brothers' refrigerator (迭代加深dfs)
- POJ-3204-Ikki's Story I - Road Reconstruction