• POJ1026 Cipher(置换的幂运算)


    Cipher
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 21436   Accepted: 5891

    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 <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 #define MAXN 205
     6 int a[MAXN];
     7 int n;
     8 void zh(int* a,int *b)
     9 {
    10     int temp[MAXN];
    11     int temp1[MAXN];
    12     int i;
    13     for(i=1;i<=n;i++)
    14     {
    15         temp[i]=a[i];
    16         temp1[i]=b[i];
    17     }
    18     for(i=1;i<=n;i++)
    19         a[i]=temp1[temp[i]];
    20 }
    21 void solve(int k,int *re)
    22 {
    23     int i;
    24     int ans[MAXN];
    25     int temp[MAXN];
    26     for(i=1;i<=n;i++)
    27     {
    28         temp[i]=a[i];
    29         ans[i]=i;
    30     }
    31     while(k)
    32     {
    33         if(k&1)
    34             zh(ans,temp);
    35         zh(temp,temp);
    36         k>>=1;
    37     }
    38     for(i=1;i<=n;i++)
    39     {
    40         re[ans[i]]=i;
    41     }
    42 }
    43 int main()
    44 {
    45     int k;
    46     int i;
    47     char str[MAXN];
    48     int re[MAXN];
    49     int len;
    50     int cnt;
    51     char ch;
    52     while(scanf("%d",&n))
    53     {
    54         if(n==0)
    55             break;
    56         for(i=1;i<=n;i++)
    57             scanf("%d",&a[i]);
    58         while(scanf("%d",&k))
    59         {
    60             if(k==0)
    61                 break;
    62             getchar();
    63             memset(str,' ',sizeof(str));
    64             len=0;
    65             while((ch=getchar())!='
    ')
    66                 str[len++]=ch;
    67             solve(k,re);
    68             cnt=0;
    69             for(i=1;i<=n;i++)
    70             {
    71                 printf("%c",str[re[i]-1]);
    72                 if(re[i]-1<len)
    73                     cnt++;
    74             }
    75             printf("
    ");
    76         }
    77         printf("
    ");
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    处理集合_创建Set
    处理集合_通过对象模拟Set
    处理集合_key相等
    处理集合_创建第1个map
    idea实用快捷键
    Typroa 常用快捷键
    关于IDEA无法加载main方法的bug
    TCP通信的实现代码
    用GUI实现java版贪吃蛇小游戏
    UDP实现在线聊天功能
  • 原文地址:https://www.cnblogs.com/vwqv/p/5947033.html
Copyright © 2020-2023  润新知