(简单) 搜索 HOJ 1048 Cipher

来源:互联网 发布:网络购票岫岩到沈阳 编辑:程序博客网 时间:2024/06/05 08:22

Cipher

My Tags  (Edit)
Source : ACM ICPC Central European Regional 1995Time limit : 5 secMemory limit : 32 M

Submitted : 553, Accepted : 184

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 encodedk 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.

Example

Input file:
104 5 3 7 2 8 1 6 10 91 Hello Bob1995 CERC00
Output file:
BolHeol  bC RCE     
题意:给一系列的位置与位置的对应关系,就是在位置i的字符进过一次变换会到位置ai,对于输入的字符串和变换次数k输出最终的变换得到的字符创(要注意,字符串结尾不足n个的部分要用空格补全)

思路: 一开始我想着用dfs来找出当前位置经过变换后的最终位置,但是觉得应该会挺慢的,于是引入了数组pos[k][cur]记录由当前位置进行k次变换最终的位置,但是提交后发现TLE  于是发现k如果很大,不仅可能会爆栈,而且会非常慢,于是只能另寻出路了。
发现其实位置顶多只有200个,从一个位置进行一定次数的变换,一定会回到原点,也就是说存在环,环中的元素最多也就200个而已,如果我们能找出环的大小,那么dfs找位置的时候就最多只要变200次就能到达目的地了。于是就想办法找环吧。
从一个位置用dfs进行搜索,知道搜到已经遍历过的地方,然后记录这个环的大小。把所有的环找出来之后,这道题目就不攻自破了


代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define MAX 200+10
#define MOD 100000000
const int inf = 0x7fffffff;

bool vis[MAX];
char message[MAX];
int base[MAX];
char ans[MAX];
int loop_sz[MAX];

int find_loop(int cur,int step)
{
vis[cur] = true;
if (vis[base[cur]])
return loop_sz[cur] = step;
return loop_sz[cur] = find_loop(base[cur],step+1);
}

int find_pos(int cur,int k)
{
if (k==0) return cur;
return find_pos(base[cur],k-1);
}

int main()
{
int n;
while (scanf("%d",&n),n)
{
for (int i = 1 ; i <= n ; ++i)
scanf("%d",base+i);
memset(vis,false,sizeof(vis));
for (int i = 1 ; i <= n ; ++i) if (!vis[i])
find_loop(i,1);
int k;
while (scanf("%d",&k),k)
{
getchar();
gets(message+1);
int len = strlen(message+1);
for (int i = 1 ; i <= n ; ++i)
ans[i] = ' ';
for (int i = 1 ; i <= len ; ++i) if (message[i]!=' ') 
{
ans[find_pos(i,k%loop_sz[i])] = message[i];
}
ans[n+1] = '\0';
printf("%s\n",ans+1);
}
printf("\n");
}
}
0 0