2015
05-23

# Revolving Digits

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

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


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 999999999
using namespace std;

const int MAX=100000+10;
char s[MAX*2];
int next[MAX];

void get_next(char *a,int len){
int i=-1,j=0;
next[0]=-1;
while(j<len){
if(i == -1 || a[i] == a[j])next[++j]=++i;
else i=next[i];
}
}

void get_extend(char *a,int len){
int k=0,i=1;
next[0]=len;
while(k+1<len && a[k] == a[k+1])++k;
next[1]=k;
k=1;
while(++i<len/2){//只需要求到原串的长度即可
int maxr=k+next[k]-1;
next[i]=min(next[i-k],max(maxr-i+1,0));
while(i+next[i]<len && a[next[i]] == a[i+next[i]])++next[i];
if(i+next[i]>k+next[k])k=i;
}
}

int main(){
int t,num=0;
cin>>t;
while(t--){
scanf("%s",s);
int len=strlen(s);
get_next(s,len);
int temp=len%(len-next[len]) == 0?len/(len-next[len]):1;//求循环节循环了几次
for(int i=0;i<=len;++i)s[i+len]=s[i];
get_extend(s,len+len);
int a=0,b=0,c=0;
for(int i=0;i<len;++i){
if(next[i]>=len)++b;//表示等于原串的
else if(s[next[i]]<s[i+next[i]])++c;//表示大于原串的
else ++a;//表示小于原串的
}
cout<<"Case "<<++num<<": "<<a/temp<<' '<<b/temp<<' '<<c/temp<<endl;
}
return 0;
}