[ex-kmp] HDU 2019 Multi-University Training Contest 5-string matching

string matching

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3131    Accepted Submission(s): 724


Problem Description
String matching is a common type of problem in computer science. One string matching problem is as following:

Given a string  s[0len1], please calculate the length of the longest common prefix of s[ilen1] and s[0len1] for each i>0.

I believe everyone can do it by brute force.
The pseudo code of the brute force approach is as the following:

<center> </center>

We are wondering, for any given string, what is the number of compare operations invoked if we use the above algorithm. Please tell us the answer before we attempt to run this algorithm.
 

 

Input
The first line contains an integer  T, denoting the number of test cases.
Each test case contains one string in a line consisting of printable ASCII characters except space.

1T30

* string length 106 for every string
 

 

Output
For each test, print an integer in one line indicating the number of compare operations invoked if we run the algorithm in the statement against the input string.
 

 

Sample Input
3 _Happy_New_Year_
ywwyww
zjczzzjczjczzzjc
 

 

Sample Output
17
7
32

题意:

给一个字符串s[0...len-1],计算它 s[i…len−1] 和 s[0…len−1] 最长公共前缀的长度之和,如果最后一个匹配的不是串中的最后一个,则要额外加1

思路:

用ex-kmp求解

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1e6+6;
 4 char a[amn],str1[amn],str2[amn];
 5 int pos[amn],ex[amn],alen,blen;
 6 void f(){
 7     pos[1]=alen;
 8     int x=1;
 9     while(str2[x]==str2[x+1]&&x+1<=blen)x++;
10     pos[2]=x-1;
11     int k=2;
12     for(int i=3;i<=alen;i++){
13         int p=k+pos[k]-1,le=pos[i-k+1];
14         if(i+le<p+1)pos[i]=le;
15         else{
16             int j=p-i+1;
17             if(j<0)j=0;
18             while(str2[j+1]==str2[i+j]&&i+j<=blen)j++;
19             pos[i]=j;
20             k=i;
21         }
22     }
23     x=1;
24     while(str1[x]==str2[x]&&x<=blen)x++;
25     ex[1]=x-1;
26     k=1;
27     for(int i=2;i<=alen;i++){
28         int p=k+ex[k]-1,le=pos[i-k+1];
29         if(i+le<p+1)ex[i]=le;
30         else{
31             int j=p-i+1;
32             if(j<0)j=0;
33             while(str2[j+1]==str1[i+j]&&i+j<=alen&&j<=blen)j++;
34             ex[i]=j;
35             k=i;
36         }
37     }
38 }
39 int main(){
40     int T;
41     long long ans;
42     scanf("%d",&T);
43     while(T--){
44         scanf("%s",a);
45         alen=blen=strlen(a);
46         strcpy(str1+1,a);
47         strcpy(str2+1,a);
48         f();
49         ans=0;
50         for(int i=2;i<=alen;i++){
51             if(alen-i+1>ex[i]){
52                 ans+=ex[i]+1;
53             }
54             else ans+=ex[i];
55         }
56         printf("%lld\n",ans);
57     }
58 }
59 /***
60 给一个字符串s[0...len-1],计算它 s[i…len−1] 和 s[0…len−1] 最长公共前缀的长度之和,如果最后一个匹配的不是串中的最后一个,则要额外加1
61 用扩展kmp求解
62 ***/

 

全部评论

相关推荐

头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务