POJ 1026

还是置换的基本概念.

有点要注意下,就是循环次数可能非常高,所以不能纯模拟,先mod一下,再处理.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<vector>
 4 #define MAXN 204
 5 using namespace std;
 6 char data[MAXN];
 7 char result[MAXN];
 8 int num[MAXN];
 9 vector<vector<int> > qun;
10 
11 void ini(int n)
12 {
13   bool vis[MAXN] = {false};
14   for (int i(1); i<=n; ++i) {
15     if (!vis[i]) {
16       vector<int> zq;
17       zq.push_back(i);
18       vis[i] = true;
19       int temp = i;
20       while (!vis[num[temp]]) {
21         temp = num[temp];
22         vis[temp] = true;
23         zq.push_back(temp);  
24       }
25       qun.push_back(zq);  
26     }  
27   }  
28 }
29 
30 void deal(int n)
31 {
32   for (int i(0); i<qun.size(); ++i) {
33     const int size = qun[i].size();
34     for (int j((size - n%size)%size),cnt(0); cnt<size; ++cnt,j = (j+1)%size) {
35       result[qun[i][cnt] - 1] = data[qun[i][j]];  
36     }  
37   } 
38 }
39 
40 int main()
41 {
42   int t,n,m;
43   int cnt = 0;
44   while (scanf("%d",&t) && t) {
45     for (int i(1); i<=t; ++i) {
46       scanf("%d",&num[i]);  
47     }
48     ini(t);      
49     while (scanf("%d",&n) && n) {
50       getchar();
51       int i(1);
52       for (; (data[i] = getchar()) != '\n'; ++i){}
53       for (;i<=t; ++i)data[i] = ' ';
54       deal(n);
55       for (int i(0); i<t; ++i)printf("%c",result[i]);
56       printf("\n");  
57     }
58     printf("\n");
59     qun.clear();
60   }
61   return 0;  
62 }
posted on 2012-05-23 15:34  Dev-T  阅读(241)  评论(0编辑  收藏  举报