[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:



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 ***/

 

转载于:https://www.cnblogs.com/Railgun000/p/11306744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值