看了扩展KMP的思路,然后来写这题,没有看别人怎么写的,自己写的想死,各种纠结的小细节,而且感觉这个东西自己想是很难想到。
终于知找到一道我用kmp无法解决的题目了,只知道用扩展kmp可以搞定。
Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1111 Accepted Submission(s): 319
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
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <math.h> #include <map> #include <queue> #include <sstream> #include <iostream> using namespace std; #define INF 0x3fffffff #define N 100100 //直接用kmp有问题 char g[2*N]; int len; int next[N]; int sum[N]; //求每个点与目标串的最大前缀 void build() { memset(next,0,sizeof(next)); int a=0,p=0; next[0]=len; for(int i=1;i<len;i++) { if(next[i-a]+i-1<p ) // 很好啊,如果p比i小的话 { next[i]=next[i-a];// 当小于上界的时候,就直接等于 } else { //if(p<0) p=0; //还要加这种东西,有初始化的作用吧 int k = p-i;//表示已经匹配好了的位置 while(p+1<len&&g[p+1]==g[k+1])//会不会超出 { p++; k++; } p=max(p,i); next[i]=k+1; //这个是表示数量的 a=i; } } } void extend_kmp() //为什么总给我一种很坑爹的感觉,好好整理一下 { memset(sum,0,sizeof(sum)); int p=0,a=0; for(int i=0;i<len;i++) { if(next[i-a]+i-1 < p) // 很好啊,如果p比i小的话 { sum[i]=next[i-a];// 当小于上界的时候,就直接等于 } else { //if(p<0) p=0; //还要加这种东西,有初始化的作用吧 int k = p-i;//表示已经匹配好了的位置 while(k+1<len && g[p+1]==g[k+1])//会不会超出 { p++; k++; } p=max(p,i); sum[i]=k+1; //这个是表示数量的 a=i; } } } //才发现题意又看错了,cacaca! int main() { //freopen("//home//chen//Desktop//ACM//in.text","r",stdin); //freopen("//home//chen//Desktop//ACM//out.text","w",stdout); int T; int tt=1; scanf("%d",&T); while(T--) { scanf("%s",g); len=strlen(g); build(); for(int i=0;i<len;i++) g[len+i]=g[i]; extend_kmp(); int a=0,b=0,c=0; int k=len; for(int i=1;i<len;i++) { if(i+next[i]-1==len-1) { if( len%(len-next[i])==0 ) { k=len-next[i]; break; } } } for(int i=0;i<k;i++) { if(sum[i]==len) b++; else { if(g[i+sum[i]]>g[ sum[i] ]) a++; else c++; } } printf("Case %d: %d %d %d ",tt++,c,b,a); /* for(int i=0;i<len;i++) printf("%d ",sum[i]); printf(" ");*/ } return 0; }