HDU 4333 Revolving Digits (extended KMP) __ String

Source: Internet
Author: User
Problem Description

One day silence was interested in revolving the digits of a positive integer. In the revolving operation, he can put several last digits to the front of the integer. Of course, he can be put all of the digits to the front and so he would get the integer itself. For example, his can change 123 into 312, 231 and 123. Now it wanted to know how many different integers he can get this is less than the original integer, how many different in Tegers He can get this is equal to the original integer and, many different integers he can get this is greater than th E original integer. We'll ensure that the original integer is positive and it has no leading zeros, but if we have integer with some Leadi ng Zeros by revolving the digits, we'll regard the new integer as it has no leading. For example, if the original integer is, we can get 410, and 104.


Input

The ' The ' input contains an integer T (1<=t<=50) which means the number of test cases.

For each test cases, there are only one of the original integer N. We ensure that n was an positive integer w Ithout leading zeros and N is less than 10^100000.


Output

For each test case, in the output a line which is ' case x:l E G ', X means the number of the test case. And L means the number of integers is less than N so we can get by revolving digits. E means the number of integers is equal to N. G means the number of integers is greater than N.


Sample Input

1
341


Sample Output

Case 1:1 1 1


the

Give a number, each time the last one to get to the front, has been like that, respectively, the number of the formation is less than, equal to and more than the number of the original number.

For example: 134 can form 134,341,413 three numbers, so they are 1,1,1.


train of Thought

Because the topic to a number of the range is very large [: 10^100000] So of course can not be treated with integer ~ ~

Using string storage, we can then create a new string by copying the current string two times, using the extended KMP to find the longest common prefix for s[i...n-1 and s[0...n-1], and then traversing the original string, if Next[i]>=n is to say that it encounters a substring that is the same as the original string, if the Str[next[i]]>str[i+next[i]] Description finds a substring smaller than the original string, or greater than the original string.

Note that it is possible to have the same substring, such as 3131 copy two times to 31313131, and there are the same substring 1313.

So we have to first find out the number of the original string of the cycle, and finally based on the expansion of KMP solution to remove the part can be.


AC Code

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <cmath> #include <
Algorithm> #include <iostream> using namespace std;

const int MAXN = 110000;
    void Pre_ekmp (char *x,int m,int *nextt) {nextt[0]=m;
    int j=0;
    while (J+1<m&&x[j]==x[j+1]) j + +;
    Nextt[1]=j;
    int k=1;
        for (int i=2; i<m; i++) {int p=nextt[k]+k-1;
        int l=nextt[i-k];
        if (i+l<p+1) nextt[i]=l;
            else {J=max (0,p-i+1);
            while (I+j<m&&x[i+j]==x[j]) j + +;
            Nextt[i]=j;
        K=i;
}} char str[maxn*2];

int NEXTT[MAXN*2],NEXTVAL[MAXN];
    void Get_next (char *t,int len) {int i=0,j=-1;
    Nextval[0]=-1; while (I<len) {if (j==-1| |
            T[i]==t[j]) {i++;
            j + +;
            if (T[i]!=t[j]) nextval[i]=j;
        else Nextval[i]=nextval[j];
    else J=nextval[j]; int main () {intT
    scanf ("%d", &t);
        for (int ti=1; ti<=t; ti++) {scanf ("%s", str);
        int N=strlen (str);
        strcat (STR,STR);
        PRE_EKMP (STR,N*2,NEXTT);
        Get_next (Str,n);
        int mod=n-nextval[n];
        int temp=1; if (n%mod==0) Temp=n/mod;
        The number of cyclic nodes int ans1=0,ans2=0,ans3=0;
            for (int i=0; i<n; i++) {int no=nextt[i];
            if (no>=n) ans2++;
            else if (Str[no]>str[i+no]) ans1++;
        else ans3++;
    printf ("Case%d:%d%d%d\n", ti,ans1/temp,ans2/temp,ans3/temp);
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.