ITeye博客

poj 1026 Cipher

antsmall · 2011-01-22 00:08 · algorithm · 编程语言 · Markdown

/*
poj 1026 Cipher
antsmall
2010-12-5
http://poj.org/problem?id=1026
*/

#include <iostream> 
using namespace std;

char msg[205];
char emsg[205];
int code[201];
int times[201];
int k;
int pos;
int mLen;
int n;
char space;

int main() {
 while(scanf("%d\n", &n) != EOF && n != 0) {
  for(int i = 0; i < n; i++) {scanf("%d", &code[i]); code[i]--;}
  scanf("\n");

  for(int i = 0; i < n; i++) {
   pos = code[i]; 
   times[i] = 1;
   while(pos != i) {pos = code[pos]; ++times[i];}
  }

  while(scanf("%d", &k) != EOF) {
   if(k == 0) {
    scanf("\n");
    break;
   }

   scanf("%c",&space);
   cin.getline(msg, 205, '\n');
   mLen = strlen(msg);
   if(mLen < n) memset(msg + mLen, ' ', sizeof(char)*(n-mLen));

   for(int i = 0; i < n; i++) {
    pos = i;
    int tmp = k%times[i];
    while(tmp--) pos = code[pos];
    emsg[pos] = msg[i];
   }
   emsg[n] = '\0';
   printf("%s\n", emsg);
  }
  putchar('\n');
 }
 return 0;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。