HDOJ 4333 Revolving Digits

扩展KMP,因为是求不同的串,所以相等的串只会出现1次,出现第二次的时候就说明有循环了

Revolving Digits

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1639    Accepted Submission(s): 472


Problem Description
One day Silence is 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 put all the digits to the front, so he will get the integer itself. For example, he can change 123 into 312, 231 and 123. Now he wanted to know how many different integers he can get that is less than the original integer, how many different integers he can get that is equal to the original integer and how many different integers he can get that is greater than the original integer. We will ensure that the original integer is positive and it has no leading zeros, but if we get an integer with some leading zeros by revolving the digits, we will regard the new integer as it has no leading zeros. For example, if the original integer is 104, we can get 410, 41 and 104.
 

Input
The first line of the input contains an integer T (1<=T<=50) which means the number of test cases. 
For each test cases, there is only one line that is the original integer N. we will ensure that N is an positive integer without leading zeros and N is less than 10^100000.
 

Output
For each test case, please 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 that 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
 

Source
2012 Multi-University Training Contest 4
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=210000;

char T[maxn*2],P[maxn];
int next[maxn],ex[maxn*2];

void pre_exkmp(char P[])
{
    int m=strlen(P);
    next[0]=m;
    int j=0,k=1;
    while(j+1<m&&P[j]==P[j+1]) j++;
    next[1]=j;
    for(int i=2;i<m;i++)
    {
        int p=next[k]+k-1;
        int L=next[i-k];
        if(i+L<p+1) next[i]=L;
        else
        {
            j=max(0,p-i-1);
            while(i+j<m&&P[i+j]==P[j]) j++;
            next[i]=j; k=i;
        }
    }
}

void exkmp(char P[],char T[])
{
    memset(ex,0,sizeof(ex));
    memset(next,0,sizeof(next));
    pre_exkmp(P);
    int j=0,k=0;
    int n=strlen(T),m=strlen(P);
    while(j<n&&j<m&&P[j]==T[j]) j++;
    ex[0]=j;
    for(int i=1;i<n;i++)
    {
        int p=ex[k]+k-1;
        int L=next[i-k];
        if(i+L<p+1) ex[i]=L;
        else
        {
            j=max(0,p-i+1);
            while(i+j<n&&j<m&&T[i+j]==P[j]) j++;
            ex[i]=j;k=i;
        }
    }
}

int main()
{
    int T_T,cas=1;
    scanf("%d",&T_T);
while(T_T--)
{
    scanf("%s",P);
    memset(T,0,sizeof(T));
    for(int i=0,m=strlen(P),sz=m*2;i<sz;i++) T[i]=P[i%m];
    exkmp(P,T);
    int ret1=0,ret2=0,ret3=0;
    for(int i=0,m=strlen(P),n=strlen(T);i<n;i++)
    {
        if(ex[i]==m)
        {
            if(ret2==0) ret2=1;
            else break;
        }
        else
        {
            if(T[i+ex[i]]>P[ex[i]]) ret3++;
            else ret1++;
        }
    }
    printf("Case %d: %d %d %d\n",cas++,ret1,ret2,ret3);
}
    return 0;
}


你可能感兴趣的:(HDOJ 4333 Revolving Digits)