zoukankan      html  css  js  c++  java
  • poj1026

    Cipher
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 18970   Accepted: 5067

    Description

    Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Public Key Cryptosystem, but their encoding and decoding is based on secret keys. They chose the secret key at their last meeting in Philadelphia on February 16th, 1996. They chose as a secret key a sequence of n distinct integers, a1 ; . . .; an, greater than zero and less or equal to n. The encoding is based on the following principle. The message is written down below the key, so that characters in the message and numbers in the key are correspondingly aligned. Character in the message at the position i is written in the encoded message at the position ai, where ai is the corresponding number in the key. And then the encoded message is encoded in the same way. This process is repeated k times. After kth encoding they exchange their message. 

    The length of the message is always less or equal than n. If the message is shorter than n, then spaces are added to the end of the message to get the message with the length n. 

    Help Alice and Bob and write program which reads the key and then a sequence of pairs consisting of k and message to be encoded k times and produces a list of encoded messages. 

    Input

    The input file consists of several blocks. Each block has a number 0 < n <= 200 in the first line. The next line contains a sequence of n numbers pairwise distinct and each greater than zero and less or equal than n. Next lines contain integer number k and one message of ascii characters separated by one space. The lines are ended with eol, this eol does not belong to the message. The block ends with the separate line with the number 0. After the last block there is in separate line the number 0.

    Output

    Output is divided into blocks corresponding to the input blocks. Each block contains the encoded input messages in the same order as in input file. Each encoded message in the output file has the lenght n. After each block there is one empty line.

    Sample Input

    10
    4 5 3 7 2 8 1 6 10 9
    1 Hello Bob
    1995 CERC
    0
    0
    

    Sample Output

    BolHeol  b
    C RCE

    求置换的循环节
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #define MAX 202
     5 int key[MAX], t[MAX];
     6 void get_time(int n)
     7 {
     8     int i, j, count;
     9     for (i = 1; i <= n; i++)
    10     {
    11         j = key[i];
    12         count = 1;
    13         while(i != j)
    14         {
    15             count++;
    16             j = key[j];
    17         }
    18         t[i] = count;
    19     }
    20 }
    21 int main()
    22 {
    23     int i, j, k, n, len;
    24     char src[MAX], dst[MAX];
    25 
    26     while(scanf("%d", &n)&&n)
    27     {
    28         for (i = 1; i <= n; i++)
    29             scanf("%d", &key[i]);
    30         memset(t, 0, sizeof(t));
    31         get_time(n);
    32         while(scanf("%d", &k)&&k)
    33         {
    34             getchar();
    35             gets(src + 1);
    36             for (i = strlen(src + 1) + 1; i <= n; i++)
    37                 src[i] = ' ';
    38             src[n + 1] = 0;
    39             for (i = 1; i <= n; i++)
    40             {
    41                 int pos = i;
    42                 for (j = 0; j < k % t[i]; j++)
    43                     pos = key[pos];
    44                 dst[pos] = src[i];
    45             }
    46             dst[n + 1] = 0;
    47             puts(dst + 1);
    48         }
    49         printf("
    ");
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    SpringBoot集成Swagger2中不同环境开关配置
    mysql
    pip 命令汇总
    mysql 时间查询(当天、本周,本月,上一个月的数据)
    MYSQL 常用函数
    java8 array、list操作 汇【6】)- Java8 Lambda表达式增强版Comparator和排序
    解决pip安装超时的问题
    Java对象为空时,将null转换为"" 保存值为空的属性
    mysql -- 模糊查询的四种方法
    Mysql 查询以某个字符开头的语句
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3720328.html
Copyright © 2011-2022 走看看