Question: give you n numbers, and then give you a string. Sort the strings again according to the instructions of n numbers.
10 (10 digits)
4 5 3 7 2 8 1 6 10 9
1 Hello Bob (1 indicates rearranging once, and the space is followed by the string to be arranged)
1995 CERC (1995 indicates rearranging 1995 times)
BolHeol B
C RCE
Example: 4 5 3 7 2 8 1 6 10 9
H e l o B o B (space filling is not enough)
Obtained: B o l H e o l B
Analysis:
Key:
4 5 3 7 2 8 1 6 10 9 (the subscript converted to the array is: 3 5 2 6 1 7 0 6 9 8)
The message length is also 10, and the subscript is 0 1 2 3 4 5 6 7 8 9. In fact, the subscript of the message is replaced by the key, after several exchanges, the system will return to the original position. For example, the number of exchanges is the cycle length. For example: 0-> 3-> 6-> 0, so the cycle length of the message character at location 0 loopLen [0] = 3 1-> 4-> 1, so the cycle length of the message character at location 1 is loopLen [1] = 2, so we can calculate the cycle length of each position. When k encryption is required, you only need to swap the character k % loopLen [I] On the position I to obtain the expected character at the final position.
# Include <iostream> # include <cstdio> using namespace std; int main () {int path [200] [200]; // save path int keyarr [200]; // encrypted number int period [200]; // retention period int N; // number of keys contained in the encryption int k; // number of times the char src [500] is encrypted, ans [500]; // source string to be encrypted (scanf ("% d", & N); N; scanf ("% d", & N )) {int key; int I, j; memset (period, 0, sizeof (int) * 200); for (I = 0; I <N; I ++) {scanf ("% d", & key); keyarr [I] = key-1; // because the index starts from 0} for (I = 0; I <N; I ++) {j = I; // search for the I-th dollar Prime cycle path [I] [0] = j; period [I] = 1; for (j = keyarr [j]; j! = I; j = keyarr [j]) {path [I] [period [I] ++] = j ;}} for (scanf ("% d ", & k); k; scanf ("% d", & k) {getchar (); // read the content in the empty buffer zone gets (src ); for (I = 0; src [I]; I ++); for (; I <N; I ++) src [I] = ''; // fill in the space for (I = 0; I <N; I ++) ans [path [I] [k % period [I] = src [I]; ans [N] = 0; puts (ans );} printf ("\ n") ;}return 0 ;}< SPAN style = "FONT-SIZE: 24px"> <STRONG> <SPAN style = "LINE-HEIGHT: 26px; WHITE-SPACE: pre-wrap "> </SPAN> </STRONG> </SPAN>