Joseph Ring--poj3379

Source: Internet
Author: User

Title Description:

Give a string with a length of n, each time K adds the next letter of the alphabetic order in the corresponding position in the string, executes m times and asks what letter was last inserted.

General idea:

Just think of the words can only be solved by the simulation method, but M has 10^9 so big, and the problem upside down, it becomes a n+m string each time the K-character deleted one, the last left a string of length n, ask the starting position is what letter. That would turn out to be the question of Joseph's ring, which can be used to calculate the position of the last remaining element without considering the content. And because the string is a ring, it can be assumed that the starting position is 1, and the last action ends at the position of the last element. This process only needs to record how many times this position has been removed.

The process is similar to that of the Joseph Ring, using the formula F (i) = (f (i-1) +m)% I. If you use this formula to recursion the time complexity is still O (m), so the use of a small condition, k<=10000, so that each time you can delete y= (m-x)/(k+1) +1 nodes. And then using the formula, the next start is x+y* (k+1), it should be noted that if the updated X is not more than M, then X is required to subtract y, because the y node is removed, so the number to subtract Y, and if more than M, then do not consider the removed element, Because the removed element number must be at the back of the current position, so that only the X-mode m can be, and then according to the formula, will get the X-mode (m-y), update M. This process has been carried out to reduce the n+m to n stop, the process because it is counted from 1, so only need to count how many operations in the process position is 1. Finally, the result and position of the statistic are put into the string, and the corresponding letters can be output.

Code:

#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int maxn = 10000 + 10;i NT N,m,k;char a[maxn];int Main () {while    (scanf ("%d%d%d", &n,&k,&m)! = EOF) {        scanf ("%s", a+1);        M + = n;        int sum = 0;        int x = 1;        while (M > N) {            if (x = = 1) sum++;            int s = min (M-n, (m-x)/(k+1) +1);            x + = s* (k+1);            if (x > M) x-= m;            else x-= s;            M-= s;            x%= m;            if (!x) x = m;        }        Sum%=;        A[X] = (a[x]-' a ' + sum)% + ' a ';        cout<<a[x]<<endl;    }}


Joseph Ring--poj3379

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.