找到循环节,然后对应的变换

Cipher
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20377   Accepted: 5491

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

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 202 int f[N];
char g[N];
char ans[N];
int flag[N];
int tmp[N]; int main()
{
int n;
while(scanf("%d",&n)&&n)
{
for(int i=;i<=n;i++)
scanf("%d",f+i);
int t;
while(scanf("%d",&t)&&t)
{
getchar();
gets(g);
int len=strlen(g);
for(int i=len;i<n;i++)
g[i]=' ';
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
if(flag[i]== ) continue;
int cnt=;
int s=i; tmp[cnt]=s;
while(f[s]!=i)
{
cnt++;
s=f[s];
tmp[cnt]=s;
flag[s]=;
}
int savecnt=cnt;
cnt=t%cnt;
s=i;
ans[tmp[ (cnt+) ] -]=g[ s- ];
int tcnt=;
while(f[s]!=i)
{
tcnt++;
s=f[s];
int cntcnt=tcnt+cnt;
if(cntcnt>savecnt) cntcnt -= savecnt;
ans[tmp[ cntcnt ] - ] = g[s-];
}
}
for(int i=;i<n;i++)
printf("%c",ans[i]);
printf("\n");
}
printf("\n");
}
return ;
}

最新文章

  1. Javascript数值转换(string,int,json)
  2. 用mysql触发器实现log记录
  3. Angular动态注册组件(controller,service...)
  4. PHP删除MySQL数据库下的所有数据表
  5. poj 1383 Labyrinth【迷宫bfs+树的直径】
  6. iptables 下开放ftp
  7. mac下Apache添加限速模块mod_bw
  8. 使用堆栈结构进行字符串表达式(&quot;7*2-5*3-3+6/3&quot;)的计算
  9. c++ virtual function 虚函数面试题
  10. c++编程思想(三)--c++中c 续,重点sizeof和内存对齐
  11. 在ASP.NET MVC中利用Aspose.cells 将查询出的数据导出为excel,并在浏览器中下载。
  12. C++多态实现原理
  13. Android Studio Gradle 多渠道自动打包,动态修改HostUrl,签名apk,混淆配置详解
  14. Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理
  15. 修改/dev/shm大小 tmps
  16. java字符串的替换replace、replaceAll、replaceFirst的区别详解
  17. Maven二
  18. VS2017
  19. 多项式相关&&生成函数相关&&一些题目(updating...)
  20. overflow的使用

热门文章

  1. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何把FBD功能块转换成ST语言
  2. 微信小程序 - 滑动显示地点信息(map)
  3. Unity Shader 效果学习
  4. npm install --no-bin-links中的参数“no-bin-links”表示什么意思
  5. javascript 捕获异常方法
  6. 脱星摘帽刺激 ST板块表现出众
  7. JSONP(转)
  8. Atitit.编程语言原理---方法重载的实现与设计&#160;调用方法的原理
  9. Yarn源码分析之MRAppMaster上MapReduce作业处理总流程(一)
  10. shiro session过期后ajax请求跳转(转)