传送门:【HDU】1439 Cipher
题目分析:置换群基础。while一遍得到循环节。
代码如下:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define REP( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )
const int MAXN = 205 ;
int a[MAXN] ;
char s[MAXN] ;
char ans[MAXN] ;
bool vis[MAXN] ;
int circle[MAXN] ;
int S[MAXN] , top ;
int n ;
void scanf ( int& x , char c = 0 ) {
while ( ( c = getchar () ) < '0' || c > '9' ) ;
x = c - '0' ;
while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}
void solve () {
int k ;
CLR ( vis , 0 ) ;
FOR ( i , 1 , n ) scanf ( a[i] ) ;
FOR ( i , 1 , n ) if ( !vis[i] ) {
int now = i ;
top = 0 ;
while ( !vis[now] ) S[top ++] = now , vis[now] = 1 , now = a[now] ;
REP ( j , 0 , top ) circle[S[j]] = top ;
}
while ( scanf ( "%d" , &k ) && k ) {
getchar () ;
int c = 1 ;
while ( ( s[c] = getchar () ) != '\n' ) ++ c ;
FOR ( i , c , n ) s[i] = ' ' ;
FOR ( i , 1 , n ) {
int step = k % circle[i] , now = i ;
while ( step -- ) now = a[now] ;
ans[now] = s[i] ;
}
ans[n + 1] = 0 ;
printf ( "%s\n" , ans + 1 ) ;
}
printf ( "\n" ) ;
}
int main () {
while ( scanf ( "%d" , &n ) && n ) solve () ;
return 0 ;
}