码迷,mamicode.com
首页 > 其他好文 > 详细

扩展KMP,附上例题(HDU - 4333 Revolving Digits)

时间:2017-09-13 15:20:57      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:col   equal   tom   xtend   void   amp   strcpy   diff   line   

给出模板串S和串T,长度分别为Slen和Tlen,在线性时间内,对于每个S[i](0<=i<Slen),求出S[i..Slen-1]与T的

最长公共前缀长度,记为extend[i],extend[i]存放s[i]开始与T的最长公共前缀长度。

例子

    a a a a a a a b b b

    a a a a a c

extend 5 4 3 2 1 0 0 0 0 0

HDU - 4333 Revolving Digits

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26531    Accepted Submission(s): 5860

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
题解:给一个数,讲这个数的最后一位放在最前,求这个新的数跟原来的比较大小,最后输出大的,等于的,小的数量
比如有个数字123,先将123两个结合在一起变成123123,进行扩展kmp算法,然后比较y[i+extend[i]] == x[extend[i]]

if(extend[i] >= m) equ++;
else if(y[i+extend[i]] < x[extend[i]]) small++;
else big++;

最后要注意有几个循环结点,除以循环结点

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 void pre_EKMP(char x[], int m, int next[]) {
10     next[0] = m;
11     int j = 0;
12     while(j+1<m && x[j] == x[j+1]) j++;
13     next[1] = j;
14     int k = 1;
15     for(int i = 2; i< m; i++) {
16         int p = next[k] + k -1;
17         int L = next[i-k];
18         if(i+L < p+1) next[i] = L;
19         else {
20             j = max(0, p-i+1);
21             while(i+j < m && x[i+j] == x[j]) j++;
22             next[i] = j;
23             k = i;
24         }
25     }
26 }
27 
28 void EKMP(char x[], int m, char y[],int n, int next[], int extend[]) {
29     pre_EKMP(x,m,next);
30     int j = 0;
31     while(j <n && j <m && x[j] == y[j]) j++;
32     extend[0] = j;
33     int k = 0;
34     for(int i = 1;i < n ;i++) {
35         int p = extend[k]+k-1;
36         int L=next[i-k];
37         if(i+L < p+1) extend[i] = L;
38         else {
39             j = max(0,p-i+1);
40             while(i+j < n && j < m && y[i+j] == x[j]) j++;
41             extend[i] = j;
42             k = i;
43         }
44     }
45 }
46 int main()
47 {
48     int T;
49     char x[200010],y[200010];
50     int next[200010], extend[200010];
51     int k = 1;
52     scanf("%d",&T);
53     while(T--) {
54         int equ = 0, small = 0, big = 0;
55         scanf("%s",x);
56         strcpy(y,x);
57         strcat(y,x);
58         int m = strlen(x), n = strlen(y);
59         EKMP(x,m,y,n,next,extend);
60         int cnt = 0;
61         for(int i = 0; i < m;i++)//此循环用于判断有几个循环结
62             if(extend[i] == extend[0])
63                 cnt++;
64         for(int i = 0; i < m; i++) {//数值比较大小
65             if(extend[i] >= m) equ++;
66             else if(y[i+extend[i]] < x[extend[i]]) small++;
67             else big++;
68         }
69         printf("Case %d: ",k++);
70         printf("%d %d %d\n",small/cnt,equ/cnt,big/cnt);
71 
72     }
73     return 0;
74 }

 

扩展KMP,附上例题(HDU - 4333 Revolving Digits)

标签:col   equal   tom   xtend   void   amp   strcpy   diff   line   

原文地址:http://www.cnblogs.com/creativepower/p/7514547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!