[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] POJ 1026 Cipher (组合数学,置换),布布扣,bubuko.com

时间: 07-25

[ACM] POJ 1026 Cipher (组合数学,置换)的相关文章

POJ 1026 Cipher(置换)

                                                               Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19502   Accepted: 5239 Description Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a P

poj 1026 Cipher

http://poj.org/problem?id=1026 这道题题意是给你一个置换群,再给你一个字符串,输出经过k次置换的字符串. 就是找循环节. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 3000 5 using namespace std; 6 7 int a[maxn],key[maxn]; 8 int n,k; 9 char str[maxn]

POJ 1026 Cipher 题解

看似简单的字符串处理,不过直接暴力法是会超时的. 故此需要优化,这里使用周期优化. 研究过数列序列的都知道,其实序列反复调用另外一个序列得到一个新的序列,都会出现周期的,问题是周期何时出现,如果利用这个周期. 这就需要分开每个数,使用一个新的数列记录每个数的周期,利用这个周期截去一大段数据,那么剩下的数据就很好处理了. 因为所有的周期数总和都不会超过n,数列的长度的,所以时间效率可以达到O(n),常数项为2. 其他难度,我觉得相对新手来说,就是执行的细节最困难了,比如下标的准确性,如何移动数据等

poj 1026 Cipher (置换群)

链接:poj 1026 题意:给定n个大小1-n的不同的整数作为密钥,给定一个字符串, 求将该字符串经过k次编码后的字符串 分析:暴力求解会超时,可以利用置换群的知识解题 置换群:一个有限集合的一一变换叫做置换,一对对置换组成了置换群. 对于一个集合a(a[1],a[2],a[3]...a[n]) 通过置换可以变成 (b[a[1]],b[a[2]],b[a[3]]...b[a[n]]) b的作用就是置换(可以理解为某种函数的作用),将原来的集合映射成具有 相应次序的集合a',a'可以看做是a的相

POJ 1026 Cipher[置换]

Cipher 时限:1000MS 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 Phila

POJ 1026 Cipher(置换群)

题目链接 题意 :由n个数字组成的密钥,每个数字都不相同,都在1-n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求最后的字符串序列,最后的空格不用输出. 思路 :如果按照最原始的求循环结的话会超时,因为k值范围很大.所以要用置换群,把每个的元素的循环求出来,直接对其取余即可.会3270的话,这个题理解起来挺容易的. 1 //1126 2 #include <stdio.h> 3 #include <st

[ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt