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 }

     

  • 相关阅读:
    我对闭包的几点初步认识
    python 字符串的split()函数详解
    使用两个不同类型的数据进行加法计算时,使用异常处理语句捕获由于数据类型错误而出现的异常,发生生成错误。是否继续并运行上次的成功生成?
    自定义一个抽象类,用来计算圆的面积
    接口里不能包括字段,构造函数,析构函数,静态成员或常量等,否则会导致错误
    抽象类与抽象方法的使用
    如何设置修改WPS批注上的用户信息名称
    在双击控件进入到程序代码编辑界面后,没写东西不影响运行,但删除后报错
    电影管理系统修改后,为啥不能识别数据库
    添加现有项到当前项目的几点注意事项
  • 原文地址:https://www.cnblogs.com/Annetree/p/7126994.html
Copyright © 2011-2022 走看看