Cipher(置换群)

Cipher

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20821   Accepted: 5708

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
题解:给你一个位置,再给你字符串,问转换k次后的字串,暴力了下,超时了。。。然后就是用置换群,找出循环节,主要是ans[b[(j+k)%num]]=s[b[j]];每一个位置变换(k%num)次大神的解释拿来:
对于置换的数组(我们用next数组代替),里面的每一个元素都会存在于(有且只有)一个置换子群里面。
     例如 题中数据: (4 -> 7 -> 1 -> 4)(5 -> 2 -> 3 -> 3) (8 -> 6 -> 8)(10 -> 9 -> 10)。   可找到4个子群。
思路:每个子群都有一定数目的各不相同的元素,用变量t记录子群元素个数,用son[]存储子群元素,就有son[0],son[1]...son[t-1]个元素。
      那么我们可以得到如下变换规则-> 新字符数组gain[son[(k + j) % t]] = 原字符数组str[son[j]] (0 <= j <= t-1)。
公式推导过程:一个子群 有t个元素说明它的循环节为t, 经过k次置换 那么肯定就有son[(k + j) % t] = son[j]; 
            例如 题目数据:4-1 5-1 3-1 7-1 2-1 8-1 1-1 6-1 10-1 9-1 (为了对应字符串的下标自减1) Hello+Bob+ 空格用+表示
            拿子群(3 -> 6 -> 0 -> 3【已经减一】)来说,其中t = 3,son[0] = 3, son[1] = 6, son[2] = 0。
            当k = 1时, { j = 0,son[(k + j) % t] = 6,gain[6] = str[3] = l
                         j = 1,son[(k + j) % t] = 0,gain[0] = str[6] = B
                         j = 2,son[(k + j) % t] = 3,gain[3] = str[0] = H
                       } 暴力超时:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=210;
int pos[MAXN];
int main(){
    int N;
    while(~scanf("%d",&N),N){
        int k;
        char s[MAXN],ans[MAXN];
        for(int i=0;i<N;i++)SI(pos[i]);
        while(scanf("%d%*c",&k),k){
            mem(s,0);
            gets(s);
            while(k--){
                for(int i=0;i<N;i++){
                if(i>=strlen(s))ans[pos[i]-1]=‘ ‘;
                else ans[pos[i]-1]=s[i];
                }
                ans[N]=‘\0‘;
                strcpy(s,ans);
            }
            puts(ans);
        }
    }
    return 0;
}

置换群:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=210;
int next[MAXN];
int vis[MAXN];
char ans[MAXN];
int b[MAXN];
int main(){
    int N;
    while(~scanf("%d",&N),N){
        int k;
        char s[MAXN];
        for(int i=0;i<N;i++)SI(next[i]),next[i]--;
        while(scanf("%d%*c",&k),k){
            gets(s);
            mem(vis,0);
            for(int i=strlen(s);i<N;i++)s[i]=‘ ‘;
            for(int i=0;i<N;i++){
                if(!vis[i]){
                    int j=i,num=0;
                    while(!vis[j]){
                        vis[j]=1;
                        b[num++]=j;
                        j=next[j];
                    }
                    for(j=0;j<num;j++){
                        ans[b[(j+k)%num]]=s[b[j]];
                    }
                }
            }
            ans[N]=‘\0‘;
            puts(ans);
        }
        puts("");
    }
    return 0;
}
时间: 2024-10-10 14:06:40

Cipher(置换群)的相关文章

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的相

hdu 1439 Cipher(置换群)

题目链接:hdu 1439 Cipher 题意: 给你一个n元的key,让你将一个字符串置换k次. 题解: 暴力将每个数的循环数找到,然后k%后对应填字符就行了. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 const int N=300; 6 char s[N],ans[N]; 7 int a[N],rep[N],n,k; 8 9 int ma

POJ 1026 Cipher(置换群)

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

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 3128 关于置换群的规律

Leonardo's Notebook Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2433   Accepted: 1037 Description - I just bought Leonardo's secret notebook! Rare object collector Stan Ucker was really agitated but his friend, special investigator S

Cracking The Vigenere Cipher

In order to crack "Vigenere Cipher" under the circumstance that the key length can be only 3, 4 or 5, I used frequency analysis to find possible keys and compared the Euclidean distance of all candidate keys calculated with "Relative freque

POJ1721_CARDS【置换群】

CARDS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1452 Accepted: 776 Description Alice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the same label) and a shuffle machine. We assume that N is an

[BZOJ 1025] 游戏 置换群 背包DP

题意 对于一个 $n$ 阶置换群 $A$ , 它的循环节大小分别为 $a_1, a_2, ..., a_m$ , 则有 $\sum_{i = 1} ^ m a_i = n$ . 定义 $f(A)$ 为它的所有循环节的最小公倍数, 即 $f(A) = [a_1, a_2, ..., a_m]$ . 求在所有 $n$ 阶置换群中, $f(A)$ 有多少种取值. $n \le 1000$ . 分析 判断 $K$ 可不可取. $K = \prod_{i = 1} ^ r {s_r} ^ {t_r}$ 可

【置换群】【枚举约数】hdu6038 Function

把b数组的所有置换群求出来,用数组记录一下每个大小所出现的次数. 然后求a的置换群,对每个置换群求能被其整除的b的置换群的大小总和(只有这些才能满足构造出一个f,且不自相矛盾),然后把它们全都乘起来就是答案. #include<cstdio> #include<cstring> using namespace std; typedef long long ll; #define MOD 1000000007ll int n,m,a[100010],b[100010],zu; boo