2015百度之星资格赛1002

来源:互联网 发布:pta编程 编辑:程序博客网 时间:2024/05/23 21:49

</pre><p></p><p></p>题目名称:列变位法解密 <p>题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=584&pid=1002</p><p></p><div class="problem-widget" style="margin-top:30px; color:rgb(51,51,51); font-family:'Helvetica Neue','microsoft yahei',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"><div class="problem-widget-title" style="font-size:18px; font-weight:bold; margin-bottom:5px">Problem Description</div><div class="problem-widget-content markdown" style="padding:9.5px; margin:0px 0px 10px; line-height:1.42857143; word-break:break-word; border:1px solid rgb(204,204,204); background-color:rgb(245,245,245)"><p style="margin-top:0px; margin-bottom:10px">列变位法是古典密码算法中变位加密的一种方法,具体过程如下 将明文字符分割成个数固定的分组(如5个一组,5即为密钥),按一组一行的次序整齐排列,<span style="font-weight:700">最后不足一组不放置任何字符</span>,完成后按列读取即成密文。</p><p style="margin-top:0px; margin-bottom:10px">比如:</p><p style="margin-top:0px; margin-bottom:10px">原文:123456789</p><p style="margin-top:0px; margin-bottom:10px">密钥:4</p><p style="margin-top:0px; margin-bottom:10px">变换后的矩阵:</p><p style="margin-top:0px; margin-bottom:10px"></p><pre style="overflow:auto; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:13px; padding:0px; margin-top:0px; margin-bottom:0px; line-height:1.42857143; word-break:break-word; word-wrap:break-word; border:none; white-space:pre-wrap; background-color:transparent">123456789xxx

(最后的几个x表示无任何字符,不是空格,不是制表符,就没有任何字符,下同)

密文:159263748

再比如:

原文:Hello, welcome to my dream world!

密钥:7

变换后的矩阵:

Hello,welcome to my dream world!xx

密文:

Hw doeetrrlloellc adoomm!,my e w

实现一个利用列变位法的加密器对Bob来说轻而易举,可是,对Bob来说,想清楚如何写一个相应的解密器似乎有点困难,你能帮帮他吗?

Input

第一行一个整数T,表示T组数据。

每组数据包含2

第一行,一个字符串s1|s|1e5,表示经过列变位法加密后的密文

第二行,一个整数K1K|s|,表示原文在使用列变位法加密时的密钥

输入保证密文字符串中只含有ASCII码在[0x20,0x7F)范围内的字符

Output

对于每组数据,先输出一行

Case #i:

然后输出一行,包含一个字符串s_decrypt,表示解密后得到的明文

Sample Input
41592637484Hw doeetrrlloellc adoomm!,my  e w7Toodming is best16sokaisan1
Sample Output
Case #1:123456789Case #2:Hello, welcome to my dream world!Case #3:Toodming is bestCase #4:sokaisan

按照题目的意思,可以写出如下加密的代码(然而并没有什么卵用)。

#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<ctime>#include<cmath>#include<algorithm>#include<map>#include<set>#include<vector>#include<iostream>using namespace std;int main(){    int k,cc=1;    char s[10005];    while(cin>>k)    {        getchar();        gets(s);        int l=strlen(s);        for(int i=0;i<k;i++)            for(int j=0;j<(l-1)/k+1;j++)        {            if(j*k+i<l) putchar(s[j*k+i]);        }        printf("\n");    }return 0;}

那么,如何解密呢?

比如

159263748

4

这一组数据。我们可以发现前面3个本是一列,后面每两个是一列。

为什么是3个呢,9个数分成三行之后,第三行只有1个数。

也就是,n个数分成(n-1)/3+1行之后,最后一行只有n%k个数。

假设我们可以从1输出到9,那么可以发现上面这个例子的输出位置顺序是035714682。

我们把它分成三段 0357 1468 2。

可以看到0跟3差3,1跟4差3,而其他的都是差2。

那么可以想到当数字位置在(n/k+1)*(n%k)内时,它的步长为n/k+1,不在这个范围内时,步长为n/k。

那么,写两层循环直接打印就好。

int n=l/k,cnt=0;int m=(l/k+1)*(l%k);for(i=0;i<(l-1)/k+1;i++){    for(j=i;j<l;j<m?j+=n+1:j+=n)    {        if(cnt<l)        {            putchar(s[j]);            cnt++;        }        else break;    }}

所有代码如下

#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<ctime>#include<cmath>#include<algorithm>#include<map>#include<set>#include<vector>#include<iostream>using namespace std;#define RE(x) freopen(x,"r",stdin);#define WR(x) freopen(x,"w",stdout);int main(){    int k,t;    char s[100005];    scanf("%d",&t);    for(int cc=1;cc<=t;cc++)    {        getchar();        gets(s);        scanf("%d",&k);        int l=strlen(s);        int i,j,cnt=0;        printf("Case #%d:\n",cc);        int n=l/k;        int m=(l/k+1)*(l%k);        for(i=0;i<(l-1)/k+1;i++)        {            for(j=i;j<l;j<m?j+=n+1:j+=n)            {                if(cnt<l)                {                    putchar(s[j]);                    cnt++;                }                else break;            }        }        printf("\n");    }return 0;}




0 0
原创粉丝点击