zoukankan      html  css  js  c++  java
  • ACM学习历程—BestCoder 2015百度之星资格赛1002 列变位法解密(vector容器)

    Problem Description

    列变位法是古典密码算法中变位加密的一种方法,具体过程如下 将明文字符分割成个数固定的分组(如5个一组,5即为密钥),按一组一行的次序整齐排列,最后不足一组不放置任何字符,完成后按列读取即成密文。

    比如:

    原文:123456789

    密钥:4

    变换后的矩阵:

    1234
    5678
    9xxx

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

    密文:159263748

    再比如:

    原文:Hello, welcome to my dream world!

    密钥:7

    变换后的矩阵:

    Hello,
    welcome
     to my 
    dream w
    orld!xx

    密文:

    Hw doeetrrlloellc adoomm!,my  e w

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

    Input

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

    每组数据包含

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

    第二行,一个整数K1K|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

    题目要求是将密文逆回去得到明文。

    首先密文的矩阵的行数是strlen(str) / key,其中str表示输入的密文,如果不整除,需要加1。

    然后就是把字符填回矩阵,注意到可能会出现矩阵某个位置是空的,而且空的那一个行从那个位置开始往后都是空的。

    于是需要控制一个L,表示从那个位置开始后的列都是L。

    然后L需要在rest%(k-st-1) == 0 && rest/(k-st-1) < L这个条件时减1。

    这个表示剩余的字符数rest能被剩余的列数整除,而且除出来的结构小于L,这个稍微画一下就能理解出来。

    考虑到矩阵的列数不确定,所以采用了vector容器。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <vector>
    #include <queue>
    #include <string>
    #define LL long long
    
    using namespace std;
    
    char str[100005];
    int k, len;
    vector <char> m[100005];
    
    void Work()
    {
        gets(str);
        scanf("%d", &k);
        getchar();
    
        int now = 0, L, rest = strlen(str);
        len = rest/k + 1;
        if (rest%k == 0)
            len--;
        for (int i = 0; i < len; ++i)
            m[i].clear();
        L = len;
        for (int st = 0; st < k; ++st)
        {
            for (int i = 0; i < L; ++i)
            {
                if (str[now] == '')
                    return;
                m[i].push_back(str[now]);
                now++;
                rest--;
            }
            if ((k-st-1) && rest%(k-st-1) == 0 && rest/(k-st-1) < L)
                L--;
        }
    }
    
    void Output()
    {
        for (int i = 0; i < len; ++i)
            for (int j = 0; j < m[i].size(); ++j)
                    printf("%c", m[i][j]);
        printf("
    ");
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        int T;
        scanf("%d", &T);
        getchar();
        for (int times = 1; times <= T; ++times)
        {
            printf("Case #%d:
    ", times);
            Work();
            Output();
        }
        return 0;
    }
  • 相关阅读:
    Jmeter自动化测试工具的简单使用--HTTP测试
    学习前端编程之前的一些建议
    深入浅出C#中的静态与非静态
    Jenkins 基于svn部署的两种方式
    粒子群基础
    python环境搭建:python3环境安装+pycharm工具使用
    python 空字符串是其他任何字符的子集
    文件复制 4种方法用时比较
    map,list,set的遍历方法
    JSP学习日记(四)
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4524931.html
Copyright © 2011-2022 走看看