joj 1019 Do the Untwist(加密—解密模拟题)_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3947 | 回复: 0   主题: joj 1019 Do the Untwist(加密—解密模拟题)        下一篇 
    本主题由 Administrator 于 2014-9-9 23:18:02 移动
chengliang.liao
注册用户
等级:新兵
经验:72
发帖:0
精华:0
注册:1970-1-1
状态:离线
发送短消息息给chengliang.liao 加好友    发送短消息息给chengliang.liao 发消息
发表于: IP:您无权察看 2014-9-9 14:34:26 | [全部帖] [楼主帖] 楼主

题目来源 :JOJ 1019

    解法类型 :模拟题

    作 者 :王福强Do the Untwist 

Status
In/Out
TIME Limit
MEMORY Limit
Submit Times
Solved Users
JUDGE TYPE
stdin/stdout
8192K
665
376
Standard
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 decryption. Twisting 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 and ciphercode 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 ciphercode according 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 y if 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:
Array
plaintext
'c'
'a'
't'
plaincode
ciphercode
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.) Sample Input5 cs.101 thqqxw.lui.qswer3 b_ylxmhzjsys.virpbkr0Sample Outputcatthis_is_a_secretbeware._dogs_barking


 题目大意:

    本题主要是给定一种加密方式,让你解密。

    题目分析:

    此题主要难点在于题目的理解上,题目很长,我也是用了很长时间才坚持读下来。现在回头想想读提示不要给自己设置心理障碍,就“静心,细心,耐心”的把题读下来,再“静心,细心,耐心”地分析以下,思路清晰一点。

    代码工整性很重要,这样很利于思路的清晰,我这次就是用了5个函数写的,而且全局变量首字母一定要大写。

    清晰的思路才是高效的保证。

    注意事项:

    读英文时,一定要保持“静心,细心,耐心”(嘿嘿,在这里提到了次这三心,这是我初中时最喜欢的学习方式,也是我成功的法宝)。

    源代码:

#include<stdio.h>
#include<string.h>
int PC[80],CipherCode[80];
char CipTxt[80];
int len,K;
bool input()
scanf("%d",&K);
char ch=getchar();
if(K==0)
return 0;
else
{
      scanf("%s",CipTxt);
      len=(int)strlen(CipTxt);
      return 1;
}
void CipCode()
for(int i=0;i<len;i++)
{
      if(CipTxt[i]=='_')
      CipherCode[i]=0;
      else if(CipTxt[i]=='.')
      CipherCode[i]=27;
      else
      CipherCode[i]=CipTxt[i]-'a'+1;
}
void PlainCode()
for(int i=0;i<len;i++)
{
      int temp=CipherCode[i];
      while(1)
      {
            if(temp+i<=27&&temp+i>=0)
            {
                  PC[(K*i)%len]=(temp+i);
                  break;
            }
            else
            temp-=28;
      }
}
void output()
for(int i=0;i<len;i++)
{
      if(PC[i]==0)
      printf("_");
      else if(PC[i]==27)
      printf(".");
      else
      {
            char ch=PC[i]+'a'-1;
            printf("%c",ch);
      }
}
printf("\n");
int main()
while(input())
{
      CipCode();
      PlainCode();
      output();
}
return 0;
#nenu acm/icpc club


该贴由system转至本版2014-9-9 23:18:02



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论