# HDU 4333 Revolving Digits （扩展KMP）

## 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


## AC 代码

#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=m;
int j=0;
while(j+1<m&&x[j]==x[j+1])j++;
nextt=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=-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()
{
int T;
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; // 循环节个数
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;
}