• 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 }

     

  • 相关阅读:
    五分钟免费搭建一个自己的网站
    网站大全-工具类,学习类网站
    vscode常用插件
    vscode常用快捷键
    IIS 如何设置多个Access-Control-Allow-Origin
    ajax跨域,这应该是最全的解决方案了
    Failed to execute ‘createObjectURL’ on ‘URL’: No function was found that matched the signature provided.
    Github新项目Dress(好耶是女装)
    Eclipse常用快捷键
    Javase、Javaee、Javame的区别
  • 原文地址:https://www.cnblogs.com/Annetree/p/7126994.html
Copyright © 2020-2023  润新知