Reverse Rot
Time Limit: 1000MS Memory limit: 65536K
题目描述
A very simplistic scheme, which was used at one time to encode information, is to rotate the characters within an alphabet and rewrite them. ROT13 is the variant in which the characters A-Z are rotated 13 places, and it was a commonly used insecure scheme that
attempted to "hide" data in many applications from the late 1990\‘s and into the early 2000\‘s.
It has been decided by Insecure Inc. to develop a product that "improves" upon this scheme by first reversing the entire string and then rotating it. As an example, if we apply this scheme to string ABCD with a reversal
and rotation of 1, after the reversal we would have DCBA and then after rotating that by 1 position we have the result EDCB.
Your task is to implement this encoding scheme for strings that contain only capital letters, underscores, and periods. Rotations are to be performed using the alphabet order:
ABCDEFGHIJKLMNOPQRSTUVWXYZ_.
Note that underscore follows Z, and the period follows the underscore. Thus a forward rotation of 1 means \‘A\‘ is shifted to \‘B\‘, that is, \‘A\‘→\‘B\‘, \‘B\‘→\‘C\‘, ..., \‘Z\‘→\‘_\‘,\‘_\‘→\‘.\‘,
and \‘.\‘→\‘A\‘. Likewise a rotation of 3 means \‘A\‘→\‘D\‘, \‘B\‘→\‘E\‘, ..., \‘.\‘→\‘C\‘.
输入
Each input line will consist of an integer N, followed by a string. N is the amount of forward rotation, such that 1 ≤ N ≤ 27. The
string is the message to be encrypted, and will consist of 1 to 40 characters, using only capital letters, underscores, and periods. The end of the input will be denoted by a final line with only the number 0.
输出
For each test case, display the "encrypted" message that results after being reversed and then shifted.
示例输入
1 ABCD 3 YO_THERE. 1 .DOT 14 ROAD 9 SHIFTING_AND_ROTATING_IS_NOT_ENCRYPTING 2 STRING_TO_BE_CONVERTED 1 SNQZDRQDUDQ 0
示例输出
EDCB CHUHKWBR. UPEA ROAD PWRAYF_LWNHAXWH.RHPWRAJAX_HMWJHPWRAORQ. FGVTGXPQEAGDAQVAIPKTVU REVERSE_ROT
提示
来源
2014 ACM MId-Central Reginal Programming Contest(MCPC2014)
示例程序
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<vector> #include<math.h> #include<map> #define inf 0x3f3f3f3f using namespace std; char a[101]; int n; int main() { while(scanf("%d",&n)!=EOF) { if(n == 0) { break; } scanf("%s",a); int len = strlen(a); n = n%28; for(int i=len-1;i>=0;i--) { if(a[i] == '.') { if(n == 27) { printf("_"); } else { printf("%c",'A'+n-1); } continue; } if(a[i] == '_') { if(n == 1) { printf("."); } else { printf("%c",'A'+n-2); } continue; } if(a[i]+n>'Z') { if(a[i]+n-'Z' == 1) { printf("_"); } else if(a[i]+n-'Z' == 2) { printf("."); } else { printf("%c",'A'+a[i]+n-'Z'-3); } } else { printf("%c",a[i]+n); } } printf("\n"); } return 0; }