pku 1107:

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.

题目大意:给出一种加密方式,现在给出明文让你解密生成密文。这种加密方式的细节是,对于一个密文字符串,将其分成三个组,然后对应三个密匙k1、k2、k3.加密过程是一个轮转过程,以第一组为例,其字符集合形式是{a,b,c,d},对应的位置集合是{2,4,6,7}这两个集合的元素形成一一映射,设密匙k1 = 2,则完成对字符集合向左轮转k1次,而位置集合不变,即加密之后字符集合编程{c、d、a、b}。(这里也可以参考题目描述中给出的解释)。三组字符串分别进行这种轮换,然后基于字符集合和位置集合的一一映射,得到明文。

分析:

数据结构:

很显然我们应该建立三个字符串数组来分别记录三组字符,而对于每个字符,通过对加密方式的理解能够看到,它在字符串中的位置非常重要,因此这里我们应该建立记录每个字符参数的结构体。

解密方法:能够看到,这种加密方式的揭秘方法非常简单,考虑到加密是完成向左的轮转,解密自然而然是向右轮转。

程序语言实现解密:这里我们先利用一个空串str来记录解密后的密文,现在我们有第一组的字符集合c = {c1,c2,c3,c4…cn},及与之形成一一映射的位置集合num = {num1,num2,num3…numn},密匙是k1,则遍历num数组,有如下的对应关系:

str[numi + k1] = ci.

同样对于第二组和第三组,也是相同的解谜思路。

接下来便是模拟编程实现了。

#include<cstdio>

#include<cstring>

using namespace std;

const int maxn = ;

struct ele

{

    char c;

    int p;

};

int main()

{

     char str[maxn];

     int k1 , k2 , k3;

     struct ele group1[maxn] , group2[maxn] , group3[maxn];

     while(scanf("%d%d%d",&k1,&k2,&k3) != EOF)

     {

          getchar();

          scanf("%s",str);

          int index1 , index2 , index3;

          index1 = ;

          index2 = ;

          index3 = ;

          for(int i = ;i < strlen(str);i++)

          {

               if(str[i] >= 'a' && str[i] <= 'i')

                   group1[index1].p = i , group1[index1++].c = str[i];

               else if(str[i] >= 'j' && str[i] <= 'r')

                   group2[index2].p = i , group2[index2++].c = str[i];

               else

                   group3[index3].p = i , group3[index3++].c = str[i];

          }//分组并记录字符的参数

          //解密

           for(int i = ;i < index1;i++)

           {

                int j = i + k1;

                  if(j >= index1)   j %= index1;//因为是一个轮转,千万不要忘记取余

                  str[group1[j].p] = group1[i].c;

           }

           for(int i = ;i < index2;i++)

           {

                int j = i + k2;

                  if(j >= index2)   j %= index2;

                  str[group2[j].p] = group2[i].c;

           }

           for(int i = ;i < index3;i++)

           {

                int j = i + k3;

                  if(j >= index3)   j %= index3;

                  str[group3[j].p] = group3[i].c;

           }

           printf("%s\n",str);

     }

}

《ACM国际大学生程序设计竞赛题解I》——6.11的更多相关文章

  1. 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题

    这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...

  2. 《ACM国际大学生程序设计竞赛题解I》——6.10

    Pku 1143: Description Christine and Matt are playing an exciting game they just invented: the Number ...

  3. 《ACM国际大学生程序设计竞赛题解I》——6.8

    Poj1068: Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in ...

  4. 《ACM国际大学生程序设计竞赛题解Ⅰ》——模拟题

    这篇文章来介绍一些模拟题,即一类按照题目要求将现实的操作转换成程序语言. zoj1003: On every June 1st, the Children's Day, there will be a ...

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会部分题解

    题目链接 2018 ACM 国际大学生程序设计竞赛上海大都会 下午午休起床被同学叫去打比赛233 然后已经过了2.5h了 先挑过得多的做了 .... A题 rand x*n 次点,每次judge一个点 ...

  6. 2018 ACM 国际大学生程序设计竞赛上海大都会赛

    传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...

  7. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  8. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. 部分A+B_1

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...

  2. Intellij Idea 13 vmoptions (Mac版本)

    -ea -server -Xms1g -Xmx1g -Xss16m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DoEscapeAnalysis -XX:+ ...

  3. PL/SQL Developer远程连接Oracle数据库

    首先打开电脑,到pl/sql安装的指定目录[D:\app\DZL\product\11.2.0\dbhome_1\NETWORK\ADMIN]找到[tnsnames.ora]     打开[tnsna ...

  4. OC - 22.隐式动画

    简介 每个UI控件,默认自动创建一个图层(根图层),即每个UI控件对应于至少一个图层 每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层)   ...

  5. cas sso单点登录系列8_抛弃Https让Cas以Http协议提供单点登录服务

    转:http://blog.csdn.net/ycyk_168/article/details/18668951 本文环境: 1.apache-tomcat-7.0.50-windows-x86 2. ...

  6. SGU 134.Centroid(图心)

    SGU链接: 时间限制:0.25s 空间限制:4M 题意: 给出一个树(节点数<=16000),一个节点的重量定义为从树中去除这个点后,新得到的所有树中节点最多的树的节点数.树的中心定义为所有节 ...

  7. Xcode编译项目出现访问private key提示框

    原因: 在编译时Xcode进行codesign时需要访问"private key"时没有权限,然后让询问是否允许,有三个选项,全部允许.否绝.允许,一次弹出4个(我遇到的) 遇到问 ...

  8. Telerik_2012_Q3 破解全套下载链接

    1.Telerik_OpenAccess_ORM_2012_3_1012_SDK.zip (暂未提供下载) 2. Telerik_OpenAccess_ORM_2012_3_1012.zip 3. T ...

  9. Java环境配置出现的问题及解决办法

    我使用的安装文件,从博客园一个童鞋那里找到的,连接是http://www.cnblogs.com/SelectError/p/3205582.html#commentform 开发基础环境,版本为Ja ...

  10. linux 下面 opcache 拓展

    PHP 5.5.0 及后续版本中已经绑定了 OPcache 扩展,只需要在编译安装的时候, 如果你使用--disable-all参数 禁用了默认扩展的构建, 那么必须使用--enable-opcach ...