POJ 1026 Cipher

Source: Internet
Author: User

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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.