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 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 inplaintext 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 ciphercodeback 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 | 0 | 1 | 2 |
| plaintext | 'c' | 'a' | 't' |
| plaincode | 3 | 1 | 20 |
| ciphercode | 3 | 19 | 27 |
| 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
Source: Zhejiang University Local Contest 2001
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int SZ = ;
int k;
int pc[SZ];
char ct[SZ], pt[SZ]; int main()
{
while(scanf("%d", &k) && k)
{
scanf("%s", ct);
int n = strlen(ct);
for(int i = ; ct[i] != '\0'; i++)
{
int cc;
switch(ct[i])
{
case '_':
cc = ;
break;
case '.':
cc = ;
break;
default:
cc = ct[i] - 'a' + ;
}
int j = (k * i) % n;
pc[j] = cc + i;
while(pc[j] > )
pc[j] -= ;
if(pc[j] == )
pt[j] = '_';
else if(pc[j] == )
pt[j] = '.';
else
pt[j] = (char)(pc[j] + 'a' - ); }
pt[n] = '\0';
printf("%s\n", pt);
}
return ;
}
Do the Untwist(模拟)的更多相关文章
- [ZOJ 1006] Do the Untwist (模拟实现解密)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6 题目大意:给你加密方式,请你求出解密. 直接逆运算搞,用到同余定理 ...
- ZOJ 1006:Do the Untwist(模拟)
Do the Untwist Time Limit: 2 Seconds Memory Limit: 65536 KB Cryptography deals with methods of ...
- App开发:模拟服务器数据接口 - MockApi
为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- Python 爬虫模拟登陆知乎
在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...
- HTML 事件(四) 模拟事件操作
本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4. ...
- 模拟AngularJS之依赖注入
一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...
- webapp应用--模拟电子书翻页效果
前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...
- javascript动画系列第一篇——模拟拖拽
× 目录 [1]原理介绍 [2]代码实现 [3]代码优化[4]拖拽冲突[5]IE兼容 前面的话 从本文开始,介绍javascript动画系列.javascript本身是具有原生拖放功能的,但是由于兼容 ...
随机推荐
- web压力测试_(收集)
作者:ZeldaZzz链接:http://www.zhihu.com/question/19867883/answer/89775858来源:知乎著作权归作者所有,转载请联系作者获得授权. 一个完整的 ...
- java下执行mongodb
1.1连单台mongodb Mongo mg = newMongo();//默认连本机127.0.0.1 端口为27017 Mongo mg = newMongo(ip);//可以指定ip 端口默认 ...
- MiniOS系统
实验一 命令解释程序的编写 一.目的和要求 1. 实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语言编程初步. 2.实验要求 编写类似于DOS,UNIX的 ...
- 使用Log4在测试过程中打印执行日志 及配置log4j.properties!
http://zengxiantao.iteye.com/blog/1881706 1.环境配置:到网上下载log4j-1.2.17.jar包!完后 添加到 项目的build path 中即可! 2. ...
- (十二)Jmeter之Bean Shell的使用(一)
一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...
- fsockopen 异步非阻塞式请求数据
index.php <?php ini_set ( "max_execution_time", "0" ); // 要传递的数据 $form_data = ...
- [Oracle收费标准]
http://www.oracle.com/us/corporate/pricing/technology-price-list-070617.pdf 1: 数据库 2. 中间件 3. weblogi ...
- SQL 抛出异常的例子 RAISERROR 的使用
先创建一个procedure 当输入的值不在0-100之间时会报出异常 create proc proc_x @a int as begin ) ,) else select @a end go 测试 ...
- Android四大组件之Intent
Intent不是android几大组件框架,但是确实是android 各大组件之间沟通的桥梁. 尤其Intent对于activity有很大的关系. 一下是我个人对task以及backstack的总结.
- 框架----Django之Form提交验证(二)
一.Form提交验证之(学生表.老师表.班级表)的添加和编辑实现案例 1. 浏览器访问 http://127.0.0.1:8000/student_list/ http://127.0.0.1:800 ...