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
Sample Output
//拓展kmp
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 10^100000*2;
char str[MAXN];
int next[MAXN];
void getnext(char *s,int *n)
{
int len = strlen(s),a = 0;
n[0] = len;
while(a<len-1 && s[a] == s[a+1])
a++;
n[1] = a;
a = 1;
for(int k = 2; k<len; k++)
{
int p = a+n[a]-1,l = n[k-a];
if((k-1)+l>=p)
{
int j = (p-k+1)>0?(p-k+1):0;
while(k+j<len && s[k+j] == s[j])
j++;
n[k] = j;
a = k;
}
else
n[k] = l;
}
}
int main()
{
int n;
cin >> n;
getchar();
for(int m = 1; m<=n; m++)
{
gets(str);
int len = strlen(str);
for(int j = 0; j<len; j++)
{
str[len+j] = str[j];
}
str[2*len] = '\0';
getnext(str,next);
int l = 0,e = 0,g = 0;
int k;
for(int i = 1; i<=len; i++)
{
if(i+next[i]>=len)
{
k = len%i?len:i;
break;
}
}
for(int i = 0; i<k; i++)
{
if(next[i] >= len)
e++;
else if(next[i] > 0 && next[i]<len)
{
if(str[i+next[i]]>str[next[i]])
g++;
else
l++;
}
else if(next[i] == 0)
{
if(str[i]>str[0])
g++;
else
l++;
}
}
printf("Case %d: %d %d %d\n",m,l,e,g);
}
return 0;
}