Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25296 Accepted Submission(s): 5530
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
Source
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you:
4332
4335
4334
4336
4337
题意:每次将一个字符串的每一位移动到最前面,和原串比较大小。并且相同的串只算一个。
将原串复制一下,然后这个作为主串,原串作为模式串跑扩展kmp。
扩展kmp可以求出每个主串的后缀和模式串的最长公共前缀。
显然最长公共前缀大于等于len的和原串相等,接来下的只要比较下一位就行了。
去重的时候只要除以下循环节就好了,循环节为len%len-next【len】==0?len/(len-next[len]):1
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
const int MAXN=2000010;
int Next[MAXN];
int extend[MAXN];
void EKMP(char s[],char t[])//s为主串,t为模板串
{
int i,j,p,L;
int lens=strlen(s);
int lent=strlen(t);
Next[0]=lent;
j=0;
while(j+1<lent && t[j]==t[j+1])j++;
Next[1]=j;
int a=1;
for(i=2; i<lent; i++)
{
p=Next[a]+a-1;
L=Next[i-a];
if(i+L<p+1)Next[i]=L;
else
{
j=max(0,p-i+1);
while(i+j<lent && t[i+j]==t[j])j++;
Next[i]=j;
a=i;
}
}
j=0;
while(j<lens && j<lent && s[j]==t[j])j++;
extend[0]=j;
a=0;
for(i=1; i<lens; i++)
{
p=extend[a]+a-1;
L=Next[i-a];
if(L+i<p+1)extend[i]=L;
else
{
j=max(0,p-i+1);
while(i+j<lens && j<lent && s[i+j]==t[j])j++;
extend[i]=j;
a=i;
}
}
}
void getNext(char T[],int len)
{
int j,k;
j=0;
k=-1;
Next[0]=-1;
while(j<len)
{
if(k==-1 || T[j]==T[k])
Next[++j]=++k;
else k=Next[k];
}
}
char str1[MAXN],str2[MAXN];
int main()
{
int T;
int iCase=0;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%s",str1);
int len=strlen(str1);
strcpy(str2,str1);
strcat(str2,str1);
EKMP(str2,str1);
int cnt1=0,cnt2=0,cnt3=0;
for(int i=0; i<len; i++)
{
if(extend[i]>=len)cnt2++;
else
{
if(str2[i+extend[i]]<str1[extend[i]])cnt1++;
else cnt3++;
}
}
getNext(str1,len);
int t=len-Next[len];
int tol=1;
if(len%t==0)tol=len/t;
printf("Case %d: %d %d %d\n",iCase,cnt1/tol,cnt2/tol,cnt3/tol);
}
return 0;
}