csu 1567 Reverse Rot

1567: Reverse Rot

            Time Limit: 1 Sec       Memory Limit: 128 Mb       Submitted: 372       Solved: 203    

Description

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'.

Input

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.

Output

For each test case, display the "encrypted" message that results after being reversed and then shifted.

Sample Input

1 ABCD
3 YO_THERE.
1 .DOT
14 ROAD
9 SHIFTING_AND_ROTATING_IS_NOT_ENCRYPTING
2 STRING_TO_BE_CONVERTED
1 SNQZDRQDUDQ
0

Sample Output

EDCB
CHUHKWBR.
UPEA
ROAD
PWRAYF_LWNHAXWH.RHPWRAJAX_HMWJHPWRAORQ.
FGVTGXPQEAGDAQVAIPKTVU
REVERSE_ROT




模拟题,先用reverse函数将字符串逆转,再判断每一个字符加上m后对28取模的情况,注意此处将_和.记作Z后面的2个字符就可以了.




#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <map>
using namespace std;
int main()
{
   char  a[111];
   int   a1;
   while(cin>>a1)
   {
   	if(a1==0)break;
   	cin>>a;
      reverse(a,a+strlen(a));
      //cout<<a<<endl;
      for(int i=0;i<strlen(a);i++)
      {
         if(a[i]>='A'&&a[i]<='Z')
         {
         	int c=(a[i]-'A'+a1)%28;
         	if(c>=0&&c<=25)
         	a[i]=c+'A';
         	if(c==26)a[i]='_';
         	else if(c==27)a[i]='.';
		 }
		 else if(a[i]=='_')
		 {
		 	int c=(26+a1)%28;
		 		if(c>=0&&c<=25)
         	a[i]=c+'A';
         	else if(c==26)a[i]='_';
         	else if(c==27)a[i]='.';
		 }
		 else if(a[i]=='.')
		 {
		 	int c=(27+a1)%28;
		 		if(c>=0&&c<=25)
         	a[i]=c+'A';
         	else if(c==26)a[i]='_';
         	   else if(c==27 )a[i]='.';
		 }
	  }
	  cout<<a<<endl;
   }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值