2015百度之星 列变位法解密

列变位法解密

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
列变位法是古典密码算法中变位加密的一种方法,具体过程如下 将明文字符分割成个数固定的分组(如5个一组,5即为密钥),按一组一行的次序整齐排列,**最后不足一组不放置任何字符**,完成后按列读取即成密文。 比如: 原文:123456789 密钥:4 变换后的矩阵: [pre]1234 5678 9xxx[/pre] (最后的几个x表示无任何字符,不是空格,不是制表符,就没有任何字符,下同) 密文:159263748 再比如: 原文:Hello, welcome to my dream world! 密钥:7 变换后的矩阵: [pre]Hello, welcome to my dream w orld!xx[/pre] 密文: Hw doeetrrlloellc adoomm!,my e w 实现一个利用列变位法的加密器对Bob来说轻而易举,可是,对Bob来说,想清楚如何写一个相应的解密器似乎有点困难,你能帮帮他吗?
Input
第一行一个整数$T$,表示$T$组数据。 每组数据包含$2$行 第一行,一个字符串$s(1 leq |s| leq 1e5)$,表示经过列变位法加密后的密文 第二行,一个整数$K(1 leq K leq |s|)$,表示原文在使用列变位法加密时的密钥 输入保证密文字符串中只含有ASCII码在$[0x20,0x7F)$范围内的字符
Output
对于每组数据,先输出一行 Case #i: 然后输出一行,包含一个字符串s_decrypt,表示解密后得到的明文
Sample Input
4
159263748
4
Hw doeetrrlloellc adoomm!,my  e w
7
Toodming is best
16
sokaisan
1
Sample Output
Case #1:
123456789
Case #2:
Hello, welcome to my dream world!
Case #3:
Toodming is best
Case #4:
sokaisan

Problem's Link:   http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=584&pid=1002


Mean: 

略 

analyse:

水题,直接画图模拟

Time complexity: O(n)

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-05-23-13.06
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;
const int MAXN=100010;
char s[MAXN];
int cha[MAXN];
int main()
{
        int t,Cas=1;
        scanf("%d",&t);
        getchar();
        while(t--)
        {
                gets(s);
                int n;
                scanf("%d",&n);
                getchar();
                printf("Case #%d:
",Cas++);
                int len=strlen(s);
                int line;
                if(len%n==0) line=len/n;
                else line=len/n+1;
                int m=len%n;
                memset(cha,0,sizeof cha);
                int cnt=1;
                if(m!=0)
                {
                        for(int i=m+1;i<n;++i)
                                cha[i]=cnt++;
                }
                for(int i=0;i<line;++i)
                {
                        if(len%n!=0 && i==line-1)
                        {
                                int sta=i;
                                for(int j=0;j<m;++j)
                                {
                                        printf("%c",s[sta]);
                                        sta+=line;
                                }


                        }
                        else
                        {
                                int sta=i;
                                for(int j=0;j<n;++j)
                                {
                                        printf("%c",s[sta-cha[j]]);
                                        sta+=line;
                                }
                        }
                }
                puts("");
        }
        return 0;
}
/*

*/
View Code
原文地址:https://www.cnblogs.com/crazyacking/p/4523962.html