题目描述
You are a member of a naive spy agency. For secure communication,members of the agency use a very simple encryption algorithm – which changes each symbol in the message ‘progressively’, i.e., based on the symbols preceding it. The allowed symbols are space and the 26 lowercase English letters. For encryption purposes we assign them the values 0 (for space) and 1 through 26 (for a–z). We’ll let v(s) represent the numeric value of symbol s.
Consider a message with symbols s1, s2, . . . , sn. The encryption algorithm starts by converting the first symbol s1 into its associated value u1 = v(s1). Then for each subsequent symbol si in the message, the computed value is ui = v(si) + ui−1 — the sum of its associated value and the computed value for the previous symbol. (Note that when there is a space in the input
message, the previous scrambled letter is repeated.) This process continues until all the ui are computed.
At this point, the message is a sequence of numeric values. We need to convert it back to symbols to print it out. We do this by taking the value ui modulo 27 (since there are 27 valid symbols), and replacing that value with its corresponding symbol. For example, if ui = 32, then 32 mod 27 = 5, which is the symbol ‘e’ (since v(e) = 5).
Let’s look at an example. Suppose we want to encrypt the string “my pie”.

  1. First, convert each symbol si into v(si): [13, 25, 0, 16, 9, 5].
  2. Next, compute each ui: [13, 38, 38, 54, 63, 68].
  3. Then, use modulus on the ui: [13, 11, 11, 0, 9, 14].
  4. Finally, convert these back to symbols: “mkk in”.
    Create a program that takes text and encrypts it using this algorithm, and also decrypts text that has been encrypted with this algorithm.

输入
The input to your program consists of a single integer 1 ≤ n ≤ 100 on its own line. This number is followed by n lines, each containing the letter ‘e’ or ‘d’, a single space, and then a message made up of lowercase letters (a–z) and spaces, continuing to the end of the line. Each message is between 1 and 80 characters long. The letters ‘d’ and ‘e’ indicate that your program decrypts or encrypts the subsequent string, respectively.

输出
Output the result of encrypting or decrypting each message from the input on its own separate line. Note that differences in whitespace are significant in this problem. Therefore your output must match the correct output character-for-character, including spaces.

样例输入
复制样例数据
7
e testing multiple letters rrrrrrrrrrrrr
e this particularly long sentence can test encryption
d tajbbrsjcloiuvmywwhwjqqqinauzmpuuxyllejbvv nqhfvoxlz
e my pie
d mkk in
e the quick brown fox jumps over the lazy dog
d taffwqzbmmofuqddjyvvezlatthchzzs eeqrqoosgn
样例输出
tyqjsfmmzteygwhmmycwpulddvmdvmdvmdvmdv
tajbbrsjcloiuvmywwhwjqqqinauzmpuuxyllejbvv nqhfvoxlz
this particularly long sentence can test encryption
mkk in
my pie
taffwqzbmmofuqddjyvvezlatthchzzs eeqrqoosgn
the quick brown fox jumps over the lazy dog

比较复杂的一道模拟题,模拟的是对密码的解码和加密,输入e表示加密,输入d则表示解码。加密的规则是先给原字符一个对应的数,空格为0,字母从1开始对应直到26,一共27个合法字符,从头开始,将对应位置换为字符的前缀和,然后对前缀和%27,这个数对应的字符即为加密后的字符串。

加密不麻烦,麻烦之处在于解码。解码的时候需要把上面的那个过程倒过来,这里提供两种AC方法,两种区别在于解码的过程,第一种麻烦一些,先把加密后的字符对应的数字存起来,从第一个数开始,利用前缀和递增的性质,对存起来的数字不断加27,这是在逆序取模的过程,如果这个数大于了上面那个数字,就说明找到了取模之前的前缀和,和上一个前缀和做差即为一个加密前的数字,输出对应字符即可,不断将新的前缀和存在num数组里,重复这个过程即可。另一种方法是用两个数组,用num存储前缀和,用sum存储每个数字,还是实现第一种方法的那个过程,换成两个数组会更加清晰。

方法一AC代码

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int num[1005];
int main()
{int t;cin>>t;getchar();while(t--){memset(num,0,sizeof(num));char c=getchar();getchar();string str,ans;getline(cin,str);if(c=='e'){for(int i=0;i<str.length();i++){if(str[i]==' ')num[i]+=0;else num[i]+=str[i]-'a'+1;num[i+1]+=num[i];num[i]=num[i]%27;if(num[i]==0)ans+=' ';elseans+='a'+num[i]-1;}}else if(c=='d'){if(str[0]==' ')num[0]=0;else num[0]=str[0]-'a'+1;ans+=str[0]; for(int i=1;i<str.length();i++){if(str[i]==' ')num[i]=0;else num[i]=str[i]-'a'+1;for(int j=0;;j++){int tt=num[i]+27*j;if(tt>=num[i-1]){int temp=(tt-num[i-1])%27;if(temp==0)ans+=' ';elseans+='a'+temp-1;num[i]=tt;break;}}} }   cout<<ans<<endl;}return 0;
}

方法二AC代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int num[1005];
int sum[1005];
int main()
{int t;cin>>t;getchar();while(t--){memset(num,0,sizeof(num));memset(sum,0,sizeof(sum));char c=getchar();getchar();string str,ans;getline(cin,str);for(int i=0;i<str.length();i++){if(str[i]==' ')sum[i]=0;else sum[i]=str[i]-'a'+1;}if(c=='e'){for(int i=1;i<str.length();i++)sum[i]=(sum[i]+sum[i-1])%27;}else if(c=='d'){for(int i=1;i<str.length();i++){num[i]=num[i-1]+sum[i-1];while(sum[i]<num[i]) sum[i]+=27;sum[i]=(sum[i]-num[i])%27;}     }   for(int i=0;i<str.length();i++)if(sum[i]==0)ans+=' ';elseans+='a'+sum[i]-1;cout<<ans<<endl;}return 0;
}

Progressive Scramble 复杂模拟相关推荐

  1. (转)PWA(Progressive Web App)渐进式Web应用程序

    PWA 编辑 讨论 PWA(Progressive Web App)是一种理念,使用多种技术来增强web app的功能,可以让网站的体验变得更好,能够模拟一些原生功能,比如通知推送.在移动端利用标准化 ...

  2. HTTP progressive download渐进式传输

    综述的协议对比,可以参考不同音视频传输协议的对比 比如现在常见的移动端互动直播,常使用HTTP-flv方式在网络上传输.使用flv极为简单的封装格式,再叠加http良好的网络兼容性,另外播放延迟和首帧 ...

  3. SPINN: Synergistic Progressive Inferenceof Neural Networks over Device and Cloud

    题目:SPINN: Synergistic Progressive Inferenceof Neural Networks over Device and Cloud SPINN:设备和云上神经网络的 ...

  4. deinterlace 隔行 progressive 逐行 3:2pulldown

    转自:http://blog.csdn.net/yuyin86/article/details/13628943 3:2/2:2 Pull down模式基于逐行扫描技术上进行的,针对以电影胶片为最初拍 ...

  5. 【论文笔记】PSCC-Net: Progressive Spatio-Channel Correlation Network for Image Manipulation Detection and

    PSCC-Net: Progressive Spatio-Channel Correlation Network for Image Manipulation Detection and Locali ...

  6. PWA(Progressive Web App)入门系列:(一)PWA简介

    前言 PWA做为一门Google推出的WEB端的新技术,好处不言而喻,但目前对于相关方面的知识不是很丰富,这里我推出一下这方面的入门教程系列,提供PWA方面学习. 什么是PWA PWA全称Progre ...

  7. PWA (Progressive Web App)

    PWA (Progressive Web App) 1.简介 是一种理念,使用多种技术来增强web app的功能,可以让网站的体验变得更好,能够模拟一些原生的功能,比如:通知推送 Js-to-Nati ...

  8. 【阅读笔记】PSA-GAN PROGRESSIVE SELF ATTENTION GANS FOR SYNTHETIC TIME SERIES

    CCF none , ICLR2022 Paul J , Michael B S , Pedro M , et al. PSA-GAN: Progressive Self Attention GANs ...

  9. PCSX2模拟《铁拳5》相关设置

    默认情况下使用pcsx2 1.6.0模拟铁拳5,主要会出现两个问题 问题 解决方案 屏幕抖动 菜单 Config > Video (GS) > Plugin Settings,开启&quo ...

最新文章

  1. 自监督3D手部姿态估计方法
  2. thinkphp使用echarts_Thinkphp 与Echarts-php 使用
  3. Datawhale-零基础入门NLP-新闻文本分类Task03
  4. 字符串和字符数组里的字符串比较
  5. 吴恩达机器学习week2
  6. Ubuntu 用户提权到Root
  7. Kubernetes之Pod调度
  8. #Pragma Pack(n)与内存分配 pragma pack(push,1)与#pragma pack(1)的区别
  9. XAF框架简介-C#语言
  10. 学习笔记(1):2020软考数据库系统工程师-基础知识培训视频-计算机系统--体系结构概述...
  11. 苹方字体 for linux,苹果苹方字体/苹方黑体全套完整版下载(ttf版)
  12. 基于PCL的QT开发(两个月内更新完)
  13. Poi和easyExcel
  14. BPM平台为企业“增值”
  15. 基于tiny4412的u-boot移植(二)
  16. 一篇文教你使用python Turtle库画出“精美碎花小清新风格树”快来拿代码!
  17. Your account has been flagged. Because of that, your profile is hidden from the public. If you belie
  18. Android应用市场平台应用认领
  19. 数字藏品系统开发,APP小程序成品源码搭建开发
  20. http协议中get和post的区别:

热门文章

  1. JAVA利用JXL导出/生成 EXCEL1
  2. C++静态库与动态库(转)
  3. archlinux 更新错误 Unrecognized archive format
  4. Tomcat性能监控LambdaProbe
  5. autogen.sh 的使用
  6. numpy linspace
  7. iptables说明(转)
  8. 9 [面向对象]-内置方法
  9. Mac OS X安装 ffmpeg
  10. IE10、IE11解决不能播放Flash的问题!