Codeforces 484C Strange Sorting (replacement)

Source: Internet
Author: User

Codeforces 484C Strange Sorting (replacement)

Link: Codeforces 484C Strange Sorting

Given a string with a length of N, there are now M queries, each time from left to right, perform D-sorting on the substring with a length of K one by one, and finally

Output the generated string.

Solution: the problem is a replacement idea. The replacement of L is shifted to the first place, and C corresponds to the replacement of K before D-sorting.

Execute L to ensure that D-sorting is a different K-length substring. Solve the string using the idea similar to the Rapid power of the matrix, and finally migrate it cyclically.

The corresponding N-K bit.

#include 
  
   #include 
   
    #include using namespace std;const int maxn = 1e6+5;int N, M, K, D, L[maxn];int C[maxn], P[maxn], x[maxn], tmp[maxn];char str[maxn];void multi () {    for (int i = 0; i < N; i++) tmp[i] = x[x[i]];    for (int i = 0; i < N; i++) x[i] = tmp[i];}int main () {    scanf("%s%d", str, &M);    N = strlen(str);    for (int i = 0; i < N; i++)        L[i] = (i ? i - 1 : N - 1);    while (M--) {        scanf("%d%d", &K, &D);        for (int i = 0; i < N; i++) C[i] = i;        int mv = 0;        for (int i = 0; i < D; i++) {            for (int j = i; j < K; j += D)                C[j] = mv++;        }        for (int i = 0; i < N; i++) P[i] = C[i];        for (int i = 0; i < N; i++) x[i] = C[L[i]];        int n = N - K;        while (n) {            if (n&1) {                for (int i = 0; i < N; i++)                    P[i] = x[P[i]];            }            multi();            n >>= 1;        }        for (int i = 0; i < N; i++) tmp[P[i]] = str[i];        for (int i = 0; i < N; i++) str[(i + (N - K)) % N] = tmp[i];        printf("%s\n", str);    }    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.