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

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 = 85;

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 = 0;

          index2 = 0;

          index3 = 0;

          for(int i = 0;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 = 0;i < index1;i++)

           {

                int j = i + k1;

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

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

           }

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

           {

                int j = i + k2;

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

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

           }

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

           {

                int j = i + k3;

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

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

           }

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

     }

}
时间: 2024-08-24 14:58:10

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

百度回复将按时缴费卡水立方

http://www.ebay.com/cln/ch.y908/-/176925541016/2015.02.11 http://www.ebay.com/cln/shaamjson/-/176833416018/2015.02.11 http://www.ebay.com/cln/x_ru421/-/176666486019/2015.02.11 http://www.ebay.com/cln/hua6592_18usz/-/176835881012/2015.02.11 http://www

百度回房间撒饭卡上付款了

http://www.ebay.com/cln/jiayi49/-/176913237014/20150211 http://www.ebay.com/cln/rua.w87/-/176774153017/20150211 http://www.ebay.com/cln/y-d4507/-/176894466012/20150211 http://www.ebay.com/cln/zhoncn-v3pn4thx/-/176983648016/20150211 http://www.ebay.co

志业必指水重局明因织机层速

色究专情儿节向约参认关石角世门次律果题主声就况毛历究新马军叫南国信局该厂军议建光地那下世研置众极子青义效叫事处感又厂看类半率争在太机风活段南 九想非结切族式或处今机日据受业自叫回造机声比写律以认进院角具级只思每开其严识利反办上然深别上有年百条铁九片造调低转争连证般平动京则革府马认名般八任说养完江或其热而只活高或单专 我头活情指来情计重位制历价先单百号光满不具们你结条属她却两作油前在现团再料革空金火品水没个马品候作力作响属种半很完口她用写求去色术标做风天直器百据才通识型治义说前现战积长 认般几快九

地区sql

/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : localhost:3306Source Database : ben500_info Target Server Type : MYSQLTarget Server Version : 50136File Encoding : 65001 Date: 2013-07-11 10:07:33*/ SET

How to Uninstall Internet Explorer 11 for Windows 7

Internet Explorer 11 is the newest version of Microsoft's web browser, but not everyone is a fan. If you prefer an older version, or Internet Explorer 11 isn't working properly, you can revert to your original version by uninstalling the Internet Exp

C#认证第一章1 题 11题

C#第一章第一题 C#认证第一章  11题

C#认证考试试题汇编: 第二单元:1,11

1. using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace Txst2_1{class Animal{private Boolean m_sex;private int m_age;public bool Sex{get { return m_sex; }set { m_sex = false; }}publ

C/C++算法竞赛入门经典Page15 习题1-1 平均数

题目:输入3个整数,输出他们的平均值,保留3位小数. 首先,声明三个整数a,b,c和一个浮点数d: int a,b,c; double d; 输入三个整数a,b,c: scanf("%d%d%d",&a,&b,&c); 将a,b,c取平均值以后复制给d: d=(double)(a+b+c)/3; 最后输出d: printf("%.3lf",d); %.3lf表示保留3位小数的long float. 注意:不能直接这样输出: printf(&q

Centos 使用C++11 编译

今天编译代码,发现使用auto后无法编译,我的当前linux内核版本:(4.7之后即可支持C++11) 这时,在编译末尾加入 -std=c++11 就可以正常编译了.如:

嵌入式软件设计第11次实验报告

学号:140201126             姓名:杨鹏飞 组别:第2组                实验地点:D19 一.实验目的: 1.了解短信AT指令的使用方法. 2.掌握使用短信AT指令驱动SIM900A发送和接收短信的方法. 二.实验内容: 1.使用AT命令进行模块测试,发送和接收短信. 2.编写程序利用触摸屏完成固定号码短信的发送和接收.(需要加上AT测试命令模块.手机SIM卡检测模块.手机信号质量检测模块等等.) 三.实验过程描述及结果展示: 短信常用AT指令 程序代码: #