题目描述
String matching is a common type of problem in computer science. One string matching problem is as following:
Given a string s[0…len−1], please calculate the length of the longest common prefix of s[i…len−1] and s[0…len−1] 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.
输入
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.
* 1≤T≤30
* string length ≤106 for every string
输出
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.
样例输入
复制样例数据
3 _Happy_New_Year_ ywwyww zjczzzjczjczzzjc
样例输出
17 7 32 拓展kmp学习https://segmentfault.com/a/1190000008663857
问题定义:给定两个字符串S和T(长度分别为n和m),下标从0开始,定义extend[i]
等于S[i]...S[n-1]
与T的最长相同前缀的长度,求出所有的extend[i]
。
- next[i]:
T[i]...T[m - 1]
与T的最长相同前缀长度; - extend[i]:
S[i]...S[n - 1]
与T的最长相同前缀长度。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
char s[maxn];
int Next[maxn];//T[i]...T[m - 1]与T的最长相同前缀长度;
int extend[maxn];//S[i]...S[n - 1]与T的最长相同前缀长度。
/*求解T中Next[],注释参考GetExtend()*/
void GetNext(char T[],int &m,int Next[]){
int a=0,p=0;
Next[0]=m;
for(int i=1;i<m;i++){
if(i>=p||i+Next[i-a]>=p){
if(i>=p)
p=i;
while(p<m&&T[p]==T[p-i])
p++;
Next[i]=p-i;
a=i;
}else
Next[i]=Next[i-a];
}
}
/*求解extend[]*/
void GetExtend(char S[],char T[],int extend[],int Next[]){
int a=0,p=0;
int n=strlen(s);
int m=strlen(T);
GetNext(T,m,Next);
for(int i=0;i<n;i++){
if(i>=p||i+Next[i-a]>=p){//i>=p的作用:举个典型例子,S和T无一字符相同
if(i>=p)
p=i;
while(p<n&&p-i<m&&S[p]==T[p-i])
p++;
extend[i]=p-i;
a=i;
}else
extend[i]=Next[i-a];
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s);
GetExtend(s+1,s,extend,Next);
int len=strlen(s);
len--;
ll ans=0;
for(int i=0;i<len;i++){
if(i+extend[i]<len) ans+=extend[i]+1;
else ans+=extend[i];
}
printf("%lld\n",ans);
}
return 0;
}