Poj1026--cipher (permutation group)

Source: Internet
Author: User

Topic Link: Click to open the link

Title: Give a sequence of encodings, each of which encodes the character on the I bit back to the a[i] bit. Then give a k, and the initial string, ask what the string after K-Times is.

K may be very large, can not be violent, so to use the permutation group, to find the rotation of the ring, assuming that the number of m in the ring, then each code m, it represents this back to the initial state, can be used k%m, so reduce the number of encodings. If the position of the record rotation, then for the first character in the rotation of the encoding k, it becomes the rotation of the (i+k)%m characters. This will directly calculate the final result.

Note that the problem is not difficult, but the input is a headache,,, after several times WA, tested only (except Getline):

Gets (str);

int len = strlen (str);

for (i = len; I <= N; i++)

Str[i] = ";

And be sure to have the back for, or else it would be wrong,,,,,


#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std;    Char str[210], s[210], ch; int cnt; int a[210], vis[210]; vector <int> vec[210]; int vec_num; void Grop (int n) {    int I, J;    Vec_num = 0;    memset (vis,0,sizeof (VIS));        for (i = 1; I <= n; i++) {if (vis[a[i]) continue;        j = i;            while (!vis[A[j]) {vis[A[j]] = 1;            vec[Vec_num].push_back (j);        j = A[j];    } vec_num++;    }}int Main () {int n, m, K, I, J, L, NUM, mod, temp;        while (scanf ("%d", &n) && N) {for (i = 1; I <= n; i++) {scanf ("%d", &a[i]);        } for (i = 0; i < n; i++) vec[i].clear ();        Grop (n);            while (scanf ("%d", &k) && k) {cnt = 0;            Memset (s,0,sizeof (s));            memset (str,0,sizeof (str));            /*getchar (); while (ch = GetChar ()) {if (ch = = ' \ n ') break;            STR[++CNT] = ch;            }*/gets (str);            int len = strlen (str);            for (i = len; I <= n; i++) str[i] = ";                for (i = 0; i < Vec_num; i++) {num = Vec[i].size ();                    for (j = 0; j < Num; J + +) {L = (j+k)%num;                s[Vec[i][l]] = str[Vec[i][j];            }} for (i = 1; I <= n; i++) {printf ("%c", S[i]);        } printf ("\ n");    } printf ("\ n"); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj1026--cipher (permutation group)

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.