Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26406 Accepted Submission(s): 5821
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.
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
题意;给一个字符串,每次可以将这个串最后一个字母移到开头,求所有出现的不同的字符串 比他大 相等 小的数量
解:将字符串复制两遍代替每次的操作,将此串作为文本串,原串作为模式串,通过extend相同前缀数组 快速找到第一个不同的字符比较大小,当出现和原串相同时
一定是其中一个循环节移到了开头,不然就不可能相同;
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N= 1e6+10;
char str[N];
string s;
char s1[N], s2[N];
int v[30], sum[N], extend[N], nex[N];
void getnext(char a[],int len)
{
int k=0, i=1;
nex[0]=len;
while(k+1<len&&a[k]==a[k+1])++k;
nex[1]=k;
k=1;
while(++i<len)
{
int p=k+nex[k]-1;
nex[i]=min(nex[i-k],max(p-i+1,0));
while(i+nex[i]<len&&a[nex[i]]==a[i+nex[i]]) ++nex[i];
if(i+nex[i]>k+nex[k]) k=i;
}
return ;
}
void exkmp(char a[],char b[],int extend[],int len,int len2)
{
getnext(a,len);
int k=0, i=0;
while(k<len2&&a[k]==b[k]) ++k;
extend[0]=k;
k=0;
while(++i<len2)
{
int p=k+extend[k]-1;
extend[i]=min(nex[i-k],max(p-i+1,0));
while(i+extend[i]<len2&&a[extend[i]]==b[i+extend[i]]) ++extend[i];
if(i+extend[i]>k+extend[k]) k=i;
}
return ;
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
scanf("%s",s1);
strcpy(s2,s1);
strcat(s2,s1);
int len=strlen(s1),len2=len*2;
exkmp(s1,s2,extend,len,len2);
int l=0, mid=1, r=0;
for(int i=1;i<len;i++)
{
if(extend[i]==len) break;
if(s1[extend[i]]>s2[i+extend[i]]) l++;
else r++;
}
printf("Case %d: %d %d %d\n",ncase++,l,mid,r);
}
return 0;
}