zoukankan      html  css  js  c++  java
  • poj 1026 Cipher(置换群)

    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
    
    

    启发博客:http://www.cnblogs.com/luyingfeng/p/3776755.html

    思路 :如果按照最原始的求循环结的话会超时,因为k值范围很大。所以要用置换群,把每个的元素的循环求出来,直接对其取余即可。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<string.h>
     5 using namespace std;
     6 
     7 int a[205],c[205];//置换,c用来记录数组里每个元素的循环节
     8 int n;
     9 
    10 void cir()//计算循环节
    11 {
    12     int cnt,pos;
    13     for(int i=1;i<=n;i++)
    14     {
    15         cnt=1;pos=a[i];
    16         while(pos!=i)
    17         {
    18             cnt++;
    19             pos=a[pos];
    20         }
    21         c[i]=cnt;
    22     }
    23 }
    24 
    25 char b[205],r[205];
    26 
    27 int main()
    28 {
    29     int i,len,j,pos;
    30     long long k;
    31     while(~scanf("%d",&n)&&n)
    32     {
    33         for(i=1;i<=n;i++)
    34             scanf("%d",&a[i]);
    35         cir();
    36         while(~scanf("%lld",&k)&&k)
    37         {
    38             getchar();
    39             gets(b+1);
    40             len=strlen(b+1);
    41             for(i=len+1;i<=n;i++)
    42                 b[i]=' ';
    43             memset(r,' ',sizeof(r));
    44             for(i=1;i<=n;i++)
    45             {
    46                 pos=i;
    47                 for(j=1;j<=k%c[i];j++)
    48                     pos=a[pos];
    49                 r[pos]=b[i];
    50             }
    51             for(i=1;i<=n;i++)
    52                 printf("%c",r[i]);
    53             printf("
    ");
    54         }
    55         printf("
    ");
    56     }
    57     return 0;
    58 }

     

  • 相关阅读:
    算法|LeetCode之找零钱[Java]
    算法|LeetCode之最大子段和[Java]
    算法|LeetCode之打家劫舍[Java]
    设计模式|观察者模式
    设计模式|原则—依赖倒置原则
    设计模式|原则—开闭原则
    数据库|MyBatis项目配置案例详解与Web下的增删改查实现[附项目源码]
    数据库|深入浅出MYSQL数据库—思维导图[附下载链接]
    算法|深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
    Java|单向链表的实现
  • 原文地址:https://www.cnblogs.com/Annetree/p/7126994.html
Copyright © 2011-2022 走看看