字符串(扩展KMP):HDU 4333 Revolving Digits

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.
 
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.
 
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
 
  这道题就是求一个数字循环移位后和原数字大小比对结果的计数,求小于原数的,等于原数的,大于原数的不同的由原数循环移位得到的数的个数。
  由于每个位置只有一个数,考虑对于每个位置O(1)出解。那么对于比较大小的性质,可以发现:一定会有头一段相等,然后接着开始不同,所以可以求LCP。
  然后可以用后缀数组,把原数复制一遍拼接到后面,找到原数的位置,再枚举每个循环移位后的位置,用RMQ求出LCP,然后统计答案;这里我用的扩展KMP,一样求LCP,但感觉更适合这道题,比后缀数组完美些。
  然而还没完,因为要找出不同的数,上面的方法可能重复计数了,再思考其性质,发现若一个数被计K次,那么其他所有的数都被计了K次,又可以发现K是原字串的循环数,再用普通KMP解决就好了。
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=200010;
 6 int lens,lent,Q,p,cas;
 7 int f[maxn],r[maxn];
 8 char s[maxn],t[maxn];
 9 struct ExKmp{
10     void Init(){
11         memset(f,0,sizeof(f));
12         memset(r,0,sizeof(r));
13         s[lens+1]='%';t[lent+1]='&';
14     }
15     
16     void Get_Fail(){
17         f[1]=lent;
18         while(t[2+f[2]]==t[1+f[2]])
19             f[2]+=1;p=2;
20         for(int i=3;i<=lent;i++){
21             int k=p+f[p]-1,L=f[i-p+1];
22             if(i+L-1<k)f[i]=L;
23             else{
24                 f[i]=max(0,k-i+1);
25                 while(t[1+f[i]]==t[i+f[i]])
26                     f[i]+=1;p=i;
27             }
28         }    
29     }
30     
31     void Exkmp(){
32         Init();Get_Fail();
33         while(s[1+r[1]]==t[1+r[1]])
34             r[1]++;p=1;
35         for(int i=2;i<=lens;i++){
36             int k=p+r[p]-1,L=f[i-p+1];
37             if(i+L-1<k)r[i]=L;
38             else{
39                 r[i]=max(0,k-i+1);
40                 while(t[1+r[i]]==s[i+r[i]])
41                     r[i]+=1;p=i;
42             }
43         }    
44     }
45 }kmp;
46 
47 int fail[maxn];
48 int Get_Rnd(){
49     fail[1]=fail[2]=1;
50     for(int i=2;i<=lent;i++){
51         int j=fail[i];
52         while(j!=1&&t[i]!=t[j])j=fail[j];
53         fail[i+1]=t[i]==t[j]?j+1:1;
54     }
55     if(lent-fail[lent]==0||lent%(lent-fail[lent]))
56         return 1;
57     else 
58         return lent/(lent-fail[lent]);
59 }
60 
61 void Solve(){
62     kmp.Exkmp();
63     int rnd=Get_Rnd();
64     int ans1=0,ans2=0,ans3=0;
65     for(int i=1;i<=lent;i++){
66         if(r[i]==lent)ans2++;
67         else if(s[i+r[i]]<t[r[i]+1])ans1++;
68         else if(s[i+r[i]]>t[r[i]+1])ans3++;
69     }
70     printf("%d %d %d
",ans1/rnd,ans2/rnd,ans3/rnd);
71 }
72 
73 int main(){
74     scanf("%d",&Q);
75     while(Q--){
76         scanf("%s",t+1);
77         lent=strlen(t+1);
78         for(int i=1;i<=lent;i++)
79             s[i]=s[lent+i]=t[i];
80         printf("Case %d:",++cas);
81         lens=lent*2;Solve();
82     }    
83     return 0;
84 }
原文地址:https://www.cnblogs.com/TenderRun/p/5698568.html