---
title: "poj 1026 Cipher"
author: "antsmall"
date: "2011-01-22T00:08:51Z"
tags: []
categories: ["编程语言"]
slug: "poj-1026-cipher"
draft: false
lastmod: "2011-01-22T00:08:51Z"
id: 891262
url: "https://antsmall.iteye.com/blog/891262"
---

```
/*
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;
}
```