浙大ZOJ 1006 Do the Untwist问题解决及其他人的解决方法

一、工程代码及算法设计注释

--------------------------------------------------do_the_untwist.h----------------------------------------------

#ifndef JLU_CCST_GDC_DO_THE_UNTWIST_H
#define JLU_CCST_GDC_DO_THE_UNTWIST_H
#include <string>

extern std::string decode(std::string ciphercode,int key);
extern void testDecode();

#endif//JLU_CCST_GDC_DO_THE_UNTWIST_H

------------------------------------------------do_the_untwist.cpp-- ----------------------------------------------

/**
题目来源:浙大ACM在线测试——ZOJ,题目编号1005,题名"Do the Untwist"
URL:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1005
Author:hellogdc<gdcjlu@163.com>
Finish Time:2014.01.03
*/

/**
算法设计:
通过分析由plaincode转为ciphercode的公式,可以得到如下由ciphercode转为plaincode的公式:
plaincode[k*i mod n]=i+ciphercode[i]+28*p。p为整数。
通过分析,可以得到如下结论:对于每个ciphercode,p只能为0或者-1.
*/
#include <iostream>
#include <string>
using namespace std;

static const int CRYPTOGRAPHY_CHAR_COUNT=28;

string decode(string ciphercode,int key){
	int len=ciphercode.length();
	string plaincode(len,' ');
	int chStart='a'-1;
	
	for(int i=0;i<len;i++){
		//ciphertext转为ciphercode
		int t=0;
		switch(ciphercode[i]){
		case '_': 
			t=0;break;
		case '.':
			t=27;break;
		default:
			t=ciphercode[i]-chStart;
			break;
		}
		t+=i;

		//ciphercode转为plaincode
		t=t%CRYPTOGRAPHY_CHAR_COUNT;

		//plaincode转为plaintext
		switch(t){
		case 0:
			t='_';break;
		case (CRYPTOGRAPHY_CHAR_COUNT-1):
			t='.';break;
		default:
			t+=chStart;
		}
		plaincode[(key*i)%len]=t;
	}
	return plaincode;
}

void testDecode(){
	string ciphercode="cs.";
	int key=5;
	cout<<"cipercode="<<ciphercode<<endl;
	cout<<"key="<<key<<endl;
	cout<<"plaincode="<<decode(ciphercode,key)<<endl;
	//
	ciphercode="thqqxw.lui.qswer";
	key=101;
	cout<<"cipercode="<<ciphercode<<endl;
	cout<<"key="<<key<<endl;
	cout<<"plaincode="<<decode(ciphercode,key)<<endl;
	//
	ciphercode="b_ylxmhzjsys.virpbkr";
	key=3;
	cout<<"cipercode="<<ciphercode<<endl;
	cout<<"key="<<key<<endl;
	cout<<"plaincode="<<decode(ciphercode,key)<<endl;
}

-------------------------------------------------- main.cpp ----------------------------------------------

#if 1

#include "do_the_untwist.h"
int main(){
	testDecode();
}

#endif
二、 提交并被ZOJ成功接受的代码——算法核心代码

--------------------------------------------------submit_main.cpp----------------------------------------------

/**
all codes are copied from do_the_untwist.cpp
*/
#include <iostream>
#include <string>
using namespace std;

static const int CRYPTOGRAPHY_CHAR_COUNT=28;

string untwist(string ciphertext,int key){
	int len=ciphertext.length();
	string plaintext(len,' ');
	int chStart='a'-1;
	
	for(int i=0;i<len;i++){
		int t=0;
		switch(ciphertext[i]){
		case '_': 
			t=0;break;
		case '.':
			t=27;break;
		default:
			t=ciphertext[i]-chStart;
			break;
		}
		t+=i;
		t=t%CRYPTOGRAPHY_CHAR_COUNT;
		switch(t){
		case 0:
			t='_';break;
		case (CRYPTOGRAPHY_CHAR_COUNT-1):
			t='.';break;
		default:
			t+=chStart;
		}
		plaintext[(key*i)%len]=t;
	}
	return plaintext;
}

int main(){
	int key;
	string ciphertext;
	while(true){
		cin>>key;
		if(key==0)
			break;
		cin>>ciphertext;
		cout<<untwist(ciphertext,key)<<endl;
	}
}


三、网上别人的代码

注:因为我提交的结果又错了,但是我感觉我设计的处理没有问题,所以认为应该是在输入上没处理好,所以到网上搜一下别人的做法,参考一下他们在输入上的处理。果然,一改之后,就可以成功提交了。

原文地址:http://blog.csdn.net/rongyongfeikai2/article/details/7315297


Do the Untwist

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Cryptography deals with methods of secret communication that transform a message (the plaintext) into a disguised form (the ciphertext) so that no one seeing the ciphertext will be able to figure out the plaintext except the intended recipient. Transforming the plaintext to the ciphertext is encryption; transforming the ciphertext to the plaintext is decryptionTwisting is a simple encryption method that requires that the sender and recipient both agree on a secret key k, which is a positive integer.

The twisting method uses four arrays: plaintext and ciphertext are arrays of characters, and plaincode andciphercode are arrays of integers. All arrays are of length n, where n is the length of the message to be encrypted. Arrays are origin zero, so the elements are numbered from 0 to n - 1. For this problem all messages will contain only lowercase letters, the period, and the underscore (representing a space).

The message to be encrypted is stored in plaintext. Given a key k, the encryption method works as follows. First convert the letters in plaintext to integer codes in plaincode according to the following rule: '_' = 0, 'a' = 1, 'b' = 2, ..., 'z' = 26, and '.' = 27. Next, convert each code in plaincode to an encrypted code in ciphercodeaccording to the following formula: for all i from 0 to n - 1,

ciphercode[ i] = ( plaincode[ ki mod  n-  i) mod 28.

(Here x mod y is the positive remainder when x is divided by y. For example, 3 mod 7 = 3, 22 mod 8 = 6, and -1 mod 28 = 27. You can use the C '%' operator or Pascal 'mod' operator to compute this as long as you add yif the result is negative.) Finally, convert the codes in ciphercode back to letters in ciphertext according to the rule listed above. The final twisted message is in ciphertext. Twisting the message cat using the key 5 yields the following:

Array012
plaintext'c''a''t'
plaincode3120
ciphercode31927
ciphertext'c''s''.'

Your task is to write a program that can untwist messages, i.e., convert the ciphertext back to the original plaintext given the key k. For example, given the key 5 and ciphertext 'cs.', your program must output the plaintext 'cat'.

The input file contains one or more test cases, followed by a line containing only the number 0 that signals the end of the file. Each test case is on a line by itself and consists of the key k, a space, and then a twisted message containing at least one and at most 70 characters. The key k will be a positive integer not greater than 300. For each test case, output the untwisted message on a line by itself.

Note: you can assume that untwisting a message always yields a unique result. (For those of you with some knowledge of basic number theory or abstract algebra, this will be the case provided that the greatest common divisor of the key k and length n is 1, which it will be for all test cases.)

Example input:

5 cs.
101 thqqxw.lui.qswer
3 b_ylxmhzjsys.virpbkr
0

Example output:

cat
this_is_a_secret
beware._dogs_barking

这道题是一道简单的模拟题,即告诉你加密的方法,然后输入是加密后的密文,希望程序运行后,输出解密后的明文。

那么,解密过程实际上是加密过程的逆向过程。

首先,令plainText为明文字符串数组,cipherText为密文字符串数组,plainCode为明文数字数组,cipherCode为密文数字数组。

1.输入密文,放入cipherText中,len=strlen(cipherText)

2.根据'a'~'z'为1~26,'_'为0,'.'为27,将cipherText转变为数字放入cipherCode中。

3.plainCode[(i*k)%len]=(cipherCode[i]+i)%28

4.将plainCode再按上述规则转变为明文数组plainText

5.输出plainText

代码如下:

[cpp]  view plain copy
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. //明文字符串  
  4. char  plainText[70],cipherText[70];  
  5. int plainCode[70],cipherCode[70];  
  6. int k,len;  
  7. int main()  
  8. {  
  9.    int i;  
  10.    while(1)  
  11.    {  
  12.       scanf("%d",&k);  
  13.       if(k==0)  
  14.       {  
  15.           break;   
  16.       }   
  17.       scanf("%s",cipherText);  
  18.         
  19.       len=strlen(cipherText);  
  20.         
  21.       for(i=0;i<len;i++)  
  22.       {  
  23.          if(cipherText[i]>='a'&&cipherText[i]<='z')  
  24.          {  
  25.              cipherCode[i]=cipherText[i]-'a'+1;   
  26.          }   
  27.          else if(cipherText[i]=='_')  
  28.          {  
  29.              cipherCode[i]=0;  
  30.          }  
  31.          else if(cipherText[i]=='.')  
  32.          {  
  33.              cipherCode[i]=27;  
  34.          }  
  35.       }  
  36.       for(i=0;i<len;i++)  
  37.       {  
  38.          plainCode[(i*k)%len]=(cipherCode[i]+i)%28;   
  39.       }  
  40.       for(i=0;i<len;i++)  
  41.       {  
  42.          if(plainCode[i]>=1&&plainCode[i]<=26)  
  43.          {  
  44.             plainText[i]='a'-1+plainCode[i];   
  45.          }   
  46.          else if(plainCode[i]==0)  
  47.          {  
  48.             plainText[i]='_';   
  49.          }  
  50.          else if(plainCode[i]==27)  
  51.          {  
  52.             plainText[i]='.';   
  53.          }  
  54.       }  
  55.       for(i=0;i<len;i++)  
  56.       {  
  57.          printf("%c",plainText[i]);   
  58.       }  
  59.       printf("\n");  
  60.    }  
  61.    system("pause");  
  62.    return 0;   
  63. }  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值