poj-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

解题思路:

字符串的模拟,注意每次循环右移时,最初右移的长度k应小于字符串长度l,即右移k(k>l)和右移(k%l)等价。主要学习字符串的循环移位。还有就是要考虑到这三部分字符串中,有的可能是空字符串的情况(最初一直running time error就是这个原因)。

# include<stdio.h>
# include<string.h>
# include<memory.h>
char a1[100],a2[100],a3[100];
char b1[100],b2[100],b3[100];

void change(char *s1,int k,int l,char *s2)
{
       int i=0;
	   for(i=0;i<l;i++)
		   s2[(i+k)%l]=s1[i];
}
int main()
{
	int i1,i2,i3;
    int mark[100];//保留所有字符属于三个部分中的某一个的信息
	int k1,k2,k3;
	int i=0;
	while(scanf("%d%d%d",&k1,&k2,&k3)!=EOF)
	{
	   if(k1==0&&k2==0&&k3==0)
		   break;
	   i1=i2=i3=0;

	   char s[100];
	   scanf("%s",s);
	   int len=strlen(s);

	   for(i=0;i<len;i++)
	   {
	      if(s[i]>='a'&&s[i]<='i')
		  {
		       a1[i1++]=s[i];
			   mark[i]=1;
		  }
		  else if(s[i]>='j'&&s[i]<='r')
		  {
			  a2[i2++]=s[i];
			  mark[i]=2;
		  }
		  else
		  {
			  a3[i3++]=s[i];
			  mark[i]=3;
		  }
	   }
	    
	   //保证循环右移的长度小于串的长度
	   //可能出现a1,a2,a3有的为空字符串的情况,所以这步一定要考虑
	   if(i1!=0) k1%=i1;
	   if(i2!=0) k2%=i2;
	   if(i3!=0) k3%=i3;

	   change(a1,k1,i1,b1);
	   change(a2,k2,i2,b2);
	   change(a3,k3,i3,b3);

	   i1=i2=i3=0;
	   for(i=0;i<len;i++)
	   {
	      if(mark[i]==1)
			  printf("%c",b1[i1++]);
		  else if(mark[i]==2)
			  printf("%c",b2[i2++]);
		  else
			  printf("%c",b3[i3++]);
	   }
	   printf("\n");
	}
  return 0;
}

猜你喜欢

转载自blog.csdn.net/zhuixun_/article/details/80152601