Hdu 4333 Revolving Digits(Exkmp)

Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25518 Accepted Submission(s): 5587
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

/*
exkmp.
把原串copy一遍.
先处理出每个后缀与原串的最长公共前缀长度.
Next[i]表示以i为始的最长公共前缀长度. 
然后做exkmp只比较每个后缀的前len个字符.
然后比较Next[i]与len的关系.
Next[i]>=len显然匹配前i个字符成功.
t[Next[i]]>s[i+Next[i]]说明该串较小.
(s串为t串copy后的串).
t[Next[i]]<t[i+Next[i]]说明该串较大.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 200001
using namespace std;
char t[MAXN],s[MAXN];
int Next[MAXN],tot,tot1,tot2,tot3;
void kmp()
{
    int l=strlen(t);
    int i=0,j=-1;
    Next[i]=-1;
    while(i<l)
    {
        if(j==-1||t[i]==t[j]) i++,j++,Next[i]=j;
        else j=Next[j];
    }
}
void get_next()
{
    int a=0,l=strlen(t);
    Next[0]=l;
    while(a<l-1&&t[a]==t[a+1]) a++;
    Next[1]=a;a=1;
    for(int k=2;k<l;k++)
    {
        int p=a+Next[a]-1,L=Next[k-a];
        if(k+L-1>=p){
            int j=max(p-k+1,0);
            while(k+j<l&&t[k+j]==t[j]) j++;
            Next[k]=j;
            a=k;
        }
        else Next[k]=L;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",t);
        int l=strlen(t);
        kmp();
        int k=l-Next[l],x;
        if(l%k==0) x=l/k;
        else x=1;
        for(int i=0;i<l;i++) t[l+i]=t[i];
        get_next();
        tot1=tot2=tot3=0;
        for(int i=0;i<l;i++)
        {
            if(Next[i]>=l) tot2++;
            else if(t[Next[i]]>t[i+Next[i]]) tot1++;
            else if(t[Next[i]]<t[i+Next[i]]) tot3++;
        }
        tot1/=x,tot2/=x,tot3/=x;
        printf("Case %d: %d %d %d
",++tot,tot1,tot2,tot3);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nancheng58/p/10068124.html