POJ 1107 W's Cipher

原创 2015年11月21日 20:40:48

题目链接: http://poj.org/problem?id=1107

W's Cipher
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5322   Accepted: 2675

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

题意:纯粹模拟,s [a-i] form one group, [j-r] a second group, and everything else ([s-z] and underscore) the third group.

           每组字符串旋转ki个单位,然后取代原先字符串的位置

          例如 

拿第一组测试数据来说,分为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 }

       然后取代原先位置即可

坑点:1 1 1

           a

          这种特殊情况要考虑,也就是并不是t1 t2 t3都可能有字符,在处理时要防止越界和数组为空的情况,否则会RE!哭


血一样 的教训!

代码如下:

#include <cstdio>
#include <cstring>
#define N 81
char a[N],t1[N],t2[N],t3[N];
void fix(char str[],int k,int len)  
{   
    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,k1,k2,k3,x,y,z,lena;
	while(~scanf("%d%d%d",&k1,&k2,&k3))
	{
		if(k1+k2+k3==0) break;
		scanf("%s",a);
		memset(t1, '\0', sizeof(t1));  
                memset(t2, '\0', sizeof(t2));  
                memset(t3, '\0', sizeof(t3)); 
		lena=strlen(a);
                x=0,y=0,z=0;
		for(i=0;i<lena;i++)
		{
			if(a[i]>='a' && a[i]<='i') t1[x++]=a[i];
			else if(a[i]>='j' && a[i]<='r')t2[y++]=a[i];
			else if( (a[i]>='s' && a[i]<='z' ) || a[i]=='_') t3[z++]=a[i];
		}
		//cout<<t1<<" "<<t2<<" "<<t3<<endl;
		if(strlen(t1))
		    fix(t1,k1,strlen(t1));
 	        if(strlen(t2))
		    fix(t2,k2,strlen(t2));
		if(strlen(t3))    
		    fix(t3,k3,strlen(t3));
		//cout<<t1<<" "<<t2<<" "<<t3;
		x=0,y=0,z=0;
		for(i=0;i<lena;i++)
		{
		    if(a[i]>='a' && a[i]<='i')   a[i]=t1[x++];
			else if(a[i]>='j' && a[i]<='r') a[i]=t2[y++];
			else if( (a[i]>='s' && a[i]<='z' ) || a[i]=='_') a[i]=t3[z++];
		}
		printf("%s\n",a);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj1107

这道题目很水,但是自己做了好长时间,各种问题。首先是读题,到最后才知道是什么个意思。就是把字符分成三段,每段用这一段中与次字符相距ki个字符替换,但是中间遇到了更大的问题,反正是各种错。后来想到保存位...
  • zhang20072844
  • zhang20072844
  • 2010年05月21日 13:39
  • 775

POJ 1107 : W\'s Cipher - 密码,字符串模拟

题意:对于一个仅含有a-z与下划线的字符串进行一下方式编码。1、给定3个数,所有字符分作3组,a-i一组,j-r一组,其余一组。其中每一组对应与3个数中的其中一个数。2、对于每一组,将每个字符循环左移...
  • Booky_Amnesia
  • Booky_Amnesia
  • 2015年01月10日 21:44
  • 575

poj 1107 --W's Cipher

  这道题主要是计算下标需要注意,这点上合循环队列很相似,(i+k1)%length求出相对位置。  // W's Cipher.cpp : Defines the entry point for t...
  • lhcxx1987
  • lhcxx1987
  • 2010年12月04日 23:45
  • 479

POJ 1107 W's Cipher 已被翻译

给出加密文件,密钥k1,k2,k3,输出源文件。字母a到i使用密钥k1,字母j到r使用密钥k2,字母s到z和"_"使用密钥k3。例如(_icuo_bfnwhoq_kxert),第一步找到所有a到i的字...
  • woniupengpeng
  • woniupengpeng
  • 2016年12月03日 17:09
  • 108

POJ 1107 W's Cipher 解密

点击打开链接 W's Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...
  • Dinivity123
  • Dinivity123
  • 2014年02月21日 16:16
  • 1166

POJ 1107 W's Cipher (模拟解密)

这是一道简单的
  • yzj577
  • yzj577
  • 2014年07月22日 13:27
  • 528

POJ - 1107 W&#39;s Cipher

POJ - 1107 W's Cipher Time Limit: 1000MS   Memory Limit: 10000KB   64bi...
  • csuhoward
  • csuhoward
  • 2013年11月20日 15:13
  • 3042

文章标题poj 1107:W's Cipher (模拟)

W’s CipherDescriptionWeird Wally’s Wireless Widgets, Inc. manufactures an eclectic assortment of sma...
  • Wang_SF2015
  • Wang_SF2015
  • 2016年09月01日 00:32
  • 147

poj 1107 (jobdu 1485) W's Cipher AC代码

这道题其实不难,但做了很久,一开始题没看清,题目要求三组分别移位,一开始全局移位了。 当然还需注意的就是字符位置和值得变化,先保存每个字符的位置,然后改变值而不改变位置,最后将改变后的值映射回原来的位...
  • njukingway
  • njukingway
  • 2014年02月22日 11:09
  • 695

POJ1107 W's Cipher

#include #include int main() { //freopen("t.txt","r",stdin); char a[81],ans[81]; int c1...
  • xuyouqiao2005
  • xuyouqiao2005
  • 2014年07月18日 22:11
  • 314
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1107 W's Cipher
举报原因:
原因补充:

(最多只允许输入30个字)