Joseph ring -- POJ3379, Joseph poj3379

Source: Internet
Author: User

Joseph ring -- POJ3379, Joseph poj3379

Description:

A string ring with a length of n is provided. Each time k letters are placed in the next letter of the corresponding position in the string, the next letter is executed m times and the last letter inserted is asked.

General idea:

We can only use a simulated method to solve the problem, but m is as big as 10 ^ 9, then it becomes to give a string of n + m each time put k characters to delete one, the last left a string of n length, ask the start position is what letter. In this case, it becomes the Joseph problem. The Joseph Ring problem can calculate the position of the last element without considering the content. Because the string is a ring, we can assume that the start position is 1, and the end position of the last operation is the corresponding position of the last element. In this process, you only need to record how many times this location has been removed.

The process is similar to the process of Joseph ring, using the formula f (I) = (f (I-1) + m) % I. If this formula is used for recursion, the time complexity is still O (m), so a small condition k <= 10000, therefore, you can delete y = (m-x)/(k + 1) + 1 nodes each time. Then, using the formula, the next start is x + y * (k + 1). Note that, if the updated x does not exceed m, then x needs to be removed from y. Because y nodes are removed, the number of y nodes must be removed. If the number of x Nodes exceeds m, then you don't need to consider removing the element, because the removed element number must be behind the current position. In this way, you only need to set x to m. Then, based on the formula, update m in m-y mode. Keep this process and reduce n + m to n to stop. Because the count starts from 1, you only need to count the number of operations in the process as 1. Finally, enter the statistical result and position into the string and output the corresponding letter.

Code:

#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn = 10000 + 10;int 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 %= 26;        a[x] = (a[x] - 'A' + sum) % 26 + 'A';        cout<<a[x]<<endl;    }}


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.