Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24729 Accepted Submission(s): 5381
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.
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.
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.
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
这道题就是求一个数字循环移位后和原数字大小比对结果的计数,求小于原数的,等于原数的,大于原数的不同的由原数循环移位得到的数的个数。
由于每个位置只有一个数,考虑对于每个位置O(1)出解。那么对于比较大小的性质,可以发现:一定会有头一段相等,然后接着开始不同,所以可以求LCP。
然后可以用后缀数组,把原数复制一遍拼接到后面,找到原数的位置,再枚举每个循环移位后的位置,用RMQ求出LCP,然后统计答案;这里我用的扩展KMP,一样求LCP,但感觉更适合这道题,比后缀数组完美些。
然而还没完,因为要找出不同的数,上面的方法可能重复计数了,再思考其性质,发现若一个数被计K次,那么其他所有的数都被计了K次,又可以发现K是原字串的循环数,再用普通KMP解决就好了。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int lens,lent,Q,p,cas;
int f[maxn],r[maxn];
char s[maxn],t[maxn];
struct ExKmp{
void Init(){
memset(f,,sizeof(f));
memset(r,,sizeof(r));
s[lens+]='%';t[lent+]='&';
} void Get_Fail(){
f[]=lent;
while(t[+f[]]==t[+f[]])
f[]+=;p=;
for(int i=;i<=lent;i++){
int k=p+f[p]-,L=f[i-p+];
if(i+L-<k)f[i]=L;
else{
f[i]=max(,k-i+);
while(t[+f[i]]==t[i+f[i]])
f[i]+=;p=i;
}
}
} void Exkmp(){
Init();Get_Fail();
while(s[+r[]]==t[+r[]])
r[]++;p=;
for(int i=;i<=lens;i++){
int k=p+r[p]-,L=f[i-p+];
if(i+L-<k)r[i]=L;
else{
r[i]=max(,k-i+);
while(t[+r[i]]==s[i+r[i]])
r[i]+=;p=i;
}
}
}
}kmp; int fail[maxn];
int Get_Rnd(){
fail[]=fail[]=;
for(int i=;i<=lent;i++){
int j=fail[i];
while(j!=&&t[i]!=t[j])j=fail[j];
fail[i+]=t[i]==t[j]?j+:;
}
if(lent-fail[lent]==||lent%(lent-fail[lent]))
return ;
else
return lent/(lent-fail[lent]);
} void Solve(){
kmp.Exkmp();
int rnd=Get_Rnd();
int ans1=,ans2=,ans3=;
for(int i=;i<=lent;i++){
if(r[i]==lent)ans2++;
else if(s[i+r[i]]<t[r[i]+])ans1++;
else if(s[i+r[i]]>t[r[i]+])ans3++;
}
printf("%d %d %d\n",ans1/rnd,ans2/rnd,ans3/rnd);
} int main(){
scanf("%d",&Q);
while(Q--){
scanf("%s",t+);
lent=strlen(t+);
for(int i=;i<=lent;i++)
s[i]=s[lent+i]=t[i];
printf("Case %d:",++cas);
lens=lent*;Solve();
}
return ;
}