UVa 306/poj 1026 Cipher (permutation group)

Source: Internet
Author: User
Tags eol time limit

306-cipher

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=242

http://poj.org/problem?id=1026

Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is isn't a public Key cryptosystem, but their encoding and decoding are based on the secret keys. They chose the secret key at their's last meeting into Philadelphia on February 16th, 1996. They chose as a secret key a sequence of n distinct integers, greater than zero and less or equal ton. The encoding is based on the following principle. The message was written down below the key, so this characters in the message and numbers in the key are correspondingly AL igned. Character in the Positioni are written in the encoded message at the position where is the corresponding number in the key. And then the encoded message was encoded in the same way. this procThe ESS is the REPEATEDK times. AFTERKTH encoding they exchange their message.

The length is always less or equal than N. If the message is shorter Thann, then spaces are added to the ' end of the ' the ' message ' to ' the ' the ' message ' with the ' lengthn.

Help Alice and Bob and write program which reads the key and then a sequence of pairs consisting OFK and. Enc ODEDK times and produces a list of encoded messages.

Input

The input file consists of several blocks. Each blocks has a number in the the The next line contains a sequence ofn numbers pairwise distinct and each greater than zero and less or equal. Next lines contain integer numberk and one message of the ASCII characters separated by one space. The lines are ended with EOL, this EOL does not belong to the message. The block ends with the "separate line" with the number 0. After the "last" block there was in separate line the number 0.

Output

The Output is divided into blocks corresponding to the input blocks. Each blocks contains the encoded input messages in the same order as in input file. Each encoded message in the output file has the LENGHTN. After the there is one empty line.

Sample Input


4 5 3 7 2 8 1 6 9 1
Hello Bob
1995 CERC
0
0

Sample Output

Bolheol  b
C RCE     

"Character in" the position I was written in the position , where is the corresponding number in the key.

The encryption process is as follows: (Take the 7th character of the original text ch as an example)

In the sample, CH is written in the first A7 (that is, position 1th) in an encrypted ciphertext. Then it can be inferred that CH is written in the two-encrypted ciphertext (A7) (that is, position 4th), in the three encrypted ciphertext (a (A7)) (i.e. 7th position). denoted is a^1 (7) =1,a^2 (7) =4,a^3 (7) = 7.

If the a^0 (7) =7 is included, we seem to have found some periodic regularity: a^ (m%3) (7) =7,1,4 (m%3=0,1,2)

Accordingly, the sequence {an} is written in the form of a non alternating rotation of synthesis: (4 7 1) (5 2) (3) (8 6) (10 9)

In this way, the first character of the original text in the K-encrypted ciphertext, should be written in the a^ (K%ti) (i), where the Ti refers to the rotation of the length (also can be called the minimum positive cycle), the detailed calculation details see the code below.

Note: Do not read the middle space with "%d" because the first character of the original text may be a space.

Complexity: O (N)

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Complete code:

/*uva:0.049s*//*poj:94ms,180kb*/#include <cstdio> #include <cstring> #include <vector>  
#include <utility> using namespace std;  
    
const int MAXN = 205;  
Char S[MAXN], ANS[MAXN];  
int A[MAXN];  
BOOL VIS[MAXN];  
Pair<int, int> BELONG[MAXN];  
    
Vector<int> GROUP[MAXN], tmp;  
    int main () {int n, I, J, CNT, K; while (scanf ("%d", &n), N) {for (i = 1; I <= n; ++i) {scanf ("%d", &k)  
            ;  
        A[i] = k;  
        memset (Vis, 0, sizeof (VIS));  
        for (i = 0; i < n; ++i) group[i].clear ();  
        CNT = 0; for (i = 1; I <= n; ++i) {if (!vis[i)) {for (j = a[i];!vis[j ];  
                    j = A[j]) {Belong[j] = Make_pair (CNT, group[cnt].size ());  
           Group[cnt].push_back (j);///tectonic rotation vis[j] = true;     } ++cnt; }///prepare while (scanf ("%d", &k), k) {GetChar ();///why it's not written "%d" to read out the middle space?  
            Because perhaps the first character of the original text is the space Orz ... gets (s + 1);  
            for (i = strlen (s + 1) + 1; I <= n; ++i) s[i] = ';  
                for (i = 1; I <= n; ++i) {tmp = Group[belong[i].first];  
            ans[tmp[(Belong[i].second + k)% Tmp.size ()]] = s[i];  
            } ans[n + 1] = 0;  
        Puts (ans + 1);  
    } putchar (10);  
return 0; }

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.