[ACM] POJ 1026 Cipher (组合数学,置换)

Cipher
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19228   Accepted: 5148

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

Source

Central Europe 1995


解题思路:

根据一个n个数置换,把一个字符串上的元素进行变换K次,输出K次变换后的字符串,如果字符串的长度不为n,那么在字符串末尾加空格使其长度为n。

比如样例

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

意思是Hello Bob  根据置换规则置换一次, 比如H它是字符串中的第一位,现在要变成字符串中的第4位,依次类推。

1  2  3  4  5  6  7  8  9  10

4  5  3  7  2  8  1  6  10   9     循环节有 (1,4,7)  (2,5) (3) (6,8) (9,10),对整个字符串变换,可以分别对每个循环节进行变换

有这样一个性质:

设,T^k=e (T为一循环,e为单位置换),那么k的最小正整数解为T的长度。也就是一个循环节循环它的长度次数,可以回到本身,那么题目中的变换K次等价于变换K%循环节长度  次数。

因此我们可以求出一个置换里面循环节的个数和所有的循环节的长度以及每个循环节所包含的元素。然后再对每个循环节进行K%(本个循环节的长度)次模拟。

参考:

http://www.cnblogs.com/mcflurry/archive/2012/06/24/2560467.html

http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669660.html

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int n;
int num[210];//输入的整数
char s[210];//输入的字符串
bool vis[210];//计算循环节时用到
int cir[210][210];//cir[i][0]表示第i个循环节的长度,cir[i]表示第i个循环节里面包括什么元素
int cnt;//循环节的个数
int k;//循环的个数

int getCircle()
{
    cnt=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        int len=0;//循环节的长度
        int temp=i;
        while(!vis[temp])
        {
            cir[cnt][++len]=temp;
            vis[temp]=true;
            temp=num[temp];
        }
        if(len)//别忘了加这一句话
            cir[cnt++][0]=len;
    }
    return cnt;
}

void solve()
{
    for(int i=0;i<cnt;i++)//对于每个循环节
    {
        int len=k%cir[i][0];//需要循环的次数。
        while(len--)//模拟len次交换
        {
            for(int j=2;j<=cir[i][0];j++)
            {
                char temp=s[cir[i][1]];
                s[cir[i][1]]=s[cir[i][j]];
                s[cir[i][j]]=temp;
            }
        }
    }
    for(int i=1;i<=n;i++)
        cout<<s[i];
    cout<<endl;
}

int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            cin>>num[i];
        cnt=getCircle();
        while(cin>>k&&k)
        {
            gets(s);//k后面紧跟着地空格为s[0]。
            for(int i=strlen(s);i<=n;i++)
                s[i]=' ';
            s[n+1]='\0';
            solve();
        }
        cout<<endl;
    }
    return 0;
}



你可能感兴趣的:(ACM,置换)