Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1143 Accepted Submission(s): 335
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
341
Sample Output
Case 1: 1 1 1
Source
题目大意:把一个数的后面一位数字已到前面,如此循环统计比它本身小的,相等和比它大的数目。注意这样的数字必须是不相同的,也就是说如果这个数字是有周期的那种,只能算最小周期内的数,比如505050,我们只需要算50就可以
第一次接触扩展kmp,说实话现在连kmp用的都不怎么熟,这个题也是,还没搞懂呢,只是知道了原理,但是字符串操作下标准确性很重要,是否加1等很纠结啊
我觉得这个和一般的扩展kmp有点区别,它这个并没有求extend数组,它这里的next就是extend
关于扩展kmp我觉得百度文库的这个ppt还可以,适合新手看http://wenku.baidu.com/view/fc9d8970f46527d3240ce072.html
#include<stdio.h>
#include<string.h> char s[200010];
int next[100005];
int len; void getnext()
{
int i,j,k,a=0,p=0,L;
next[0]=len;
while(a<len-1&&s[a]==s[a+1])
a++;
next[1]=a;
a=1;
for(k=2;k<len;k++)
{
p=a+next[a]-1;//求最远匹配距离
L=next[k-a];//k-a的匹配长度
if(k+L<=p)//是否小于最远距离
{
next[k]=L;//小于的话就是L
}
else
{
j=p-k+1>0?p-k+1:0;//接着p-k+1或从头匹配
while(k+j<len&&s[k+j]==s[j])//这里就是匹配了,直到匹配失败
j++;
next[k]=j;
a=k;//按道理说应该是和原来的比较一下选最大值的啊,可是加判断时间还变长了
}
}
}
int main()
{
int i,j,k,n,t,cas=1,less,e,m;
scanf("%d",&t);
getchar();
while(t--)
{
less=0;
e=0;
m=0;
for(i=0;i<len;i++)
{
s[len+i]=s[i];
}
s[len+i]='\0';
getnext();
for(i=1;i<=len;i++)//貌似读错题了,看了好多解题报告都带了这句话,坑爹啊,循环的只算一次吗
{
if(i+next[i]>=len)
{
k=len%i?len:i;
break;
}
}
for(i=0;i<k;i++)
{
if(next[i]>=len)//相等的,感觉就一种情况啊
e++;
else if(next[i]>=0)
{
if(s[i+next[i]]>s[next[i]])//next[i]表示模式串下标,i+next[i]就表示s的下标
m++;
else less++;
}
}
printf("Case %d: %d %d %d\n",cas++,less,e,m);
}
return 0;
}