题解:P3375 【模板】KMP

· · 题解

此题解下标统一从 1 开始。

前置芝士

举个栗子,对于字符串 abcabcd:

$a[2]=0$,因为 `ab` 无相等的真前缀和真后缀。 $a[3]=0$,因为 `abc` 无相等的真前缀和真后缀。 $a[4]=1$,因为 `abca` 相等的真前缀和真后缀最长为 `a`,长度为 $1$。 $a[5]=2$,因为 `abcab` 相等的真前缀和真后缀最长为 `ab`,长度为 $2$。 $a[6]=3$,因为 `abcabc` 相等的真前缀和真后缀最长为 `abc`,长度为 $3$。 $a[7]=0$,因为 `abcabcd` 无相等的真前缀和真后缀。 同理,字符串 `aabaaab` 的前缀函数为 `[0,1,0,1,2,2,3]`。 根据定义,我们可以写一个朴素做法,直接暴力求: ```cpp #include<bits/stdc++.h> using namespace std; const int N=1e6+10; int a[N]; int main() { string s; cin>>s; int n=s.size(); s=" "+s; for (int i=1; i<=n; i++) { for (int j=i-1;j>=0;j--) { if (s.substr(1,j)==s.substr(i-j+1,j)) { a[i]=j; break; } } } for (int i=1; i<=n; i++) { cout<<a[i]<<" "; } return 0; } ``` 可是显然,此代码时间复杂度为 $O(n^3)$,特别慢。 考虑进行优化: 对栗子中的前缀函数进行观察,可以发现,**相邻的前缀函数最多增加 $1$**。并且只在 $s_{i+1}=s_{a_i}$ 时 $a_{i+1}=a_i+1$。 优化后的代码: ```cpp #include<bits/stdc++.h> using namespace std; const int N=1e6+10; int a[N]; int main() { string s; cin>>s; int n=s.size(); s=" "+s; for (int i=1; i<=n; i++) { for(int j=min(a[i-1]+1,i-1); j>=0; j--) { if (s.substr(1,j)==s.substr(i-j+1,j)) { a[i]=j; break; } } } for (int i=1; i<=n; i++) { cout<<a[i]<<" "; } return 0; } ``` 时间复杂度为 $O(n^2)$。 在上一个优化中,我们知道了在且只在 $s_{i+1}=s_{a_i}$ 时前缀函数才会增加 $1$,接下来,我们考虑其他情况时的取值。 ![](https://cdn.luogu.com.cn/upload/image_hosting/vwxfp74u.png) 举个栗子,上图中 $a_n=5$,我们加入第 $n+1$ 位: ![](https://cdn.luogu.com.cn/upload/image_hosting/mfh7ii2n.png) 首先,我们先让 $s_{n+1}$ 与 $s_{a_n}$ 进行比较,发现不相等。 接下来,我们肯定要考虑仅次于 $a_n$ 的第二长度 $tmp$,使其满足前缀性质,也就是 $s[1...tmp]=s[n-tmp+1...n]$。 ![](https://cdn.luogu.com.cn/upload/image_hosting/1sgri8fj.png) 如果找到了这样的 $tmp$,就比较 $s_{tmp+1}$ 与 $s_{a_n}$。如果相等,那么$a_{n+1}=tmp$,否则再重复上面的操作。 ![](https://cdn.luogu.com.cn/upload/image_hosting/hkymcvtr.png) 我们可以发现,其实 $tmp$ 就是 $a_{a_n}$。因为 $s[a[n]-tmp+1...a[n]]=s[n-tmp+1...n]$,所以只在 $tmp=a_{a_n}$ 时,才有最大的 $tmp$ 使 $s[1...tmp]=s[a[n]-tmp+1...a[n]]=a[n-tmp+1...n]$。 因此,我们可以对代码再次进行优化: ```cpp #include<bits/stdc++.h> using namespace std; const int N=1e6+10; int a[N]; int main() { string s; cin>>s; int n=s.size(); s=" "+s; for (int i=2; i<=n; i++) { int tmp=a[i-1]+1; while(tmp>1&&s[i]!=s[tmp]) { tmp=a[tmp-1]+1; } if(s[i]!=s[tmp]) { tmp--; } a[i]=tmp; } for (int i=1; i<=n; i++) { cout<<a[i]<<" "; } return 0; } ``` 此时代码时间复杂度已经来到了 $O(n)$。 ~~好的所以这跟 kmp 有什么关系呢?~~ 设我们想在长度为 $m$ 的字符串 $s_1$ 中找到长度为 $n$ 的字符串 $s_2$ 的所以出现位置。 我们考虑构造一个字符串 $s_2+$`#`$+s_1$,其中 `#` 是一个不出现在 $s_1$ 也不出现在 $s_2$ 的字符。现在对此字符串跑一边前缀函数。可以发现,如果有 $k$ 使 $a_k=n$,$s_2$ 就在 $s_1$ 的 $k-(n+1)-n+1$ 处出现了一次。这样我们就在 $O(n+m)$ 的时间内解决该问题了。 ![~~用一下oiwiki的图~~](https://oi-wiki.org/string/images/strstr_kmp_indices.svg) ### 代码 ```cpp #include<bits/stdc++.h> using namespace std; const int N=1e6+10; int a[N<<1]; vector<int> ans; int main() { string s1,s2; cin>>s1>>s2; int n=s2.size(); string k=s2+'#'+s1; int t=k.size(); s1=" "+s1; s2=" "+s2; k=" "+k; for (int i=2; i<=t; i++) { int tmp=a[i-1]+1; while(tmp>1&&k[i]!=k[tmp]) { tmp=a[tmp-1]+1; } if(k[i]!=k[tmp]) { tmp--; } a[i]=tmp; if(a[i]==n) { ans.push_back(i-2*n); } } for(auto x:ans) { cout<<x<<'\n'; } for(int i=1; i<=n; i++) { cout<<a[i]<<' '; } return 0; } ``` 其实因为 kmp 是在线算法,我们可以省去 $k$ 的空间。 ```cpp #include<bits/stdc++.h> using namespace std; const int N=1e6+10; string s2,s1; int a[N]; int main() { int n,m; cin>>s1>>s2; n=s2.size(); m=s1.size(); s2=' '+s2; s1=' '+s1; for(int i=2,tmp;i<=n;i++) { tmp=a[i-1]+1; while(tmp>1&&s2[i]!=s2[tmp]) { tmp=a[tmp-1]+1; } if(s2[i]!=s2[tmp]) { tmp--; } a[i]=tmp; } for(int i=1,tmp=1;i<=m;i++) { while(tmp>1&&s1[i]!=s2[tmp]) { tmp=a[tmp-1]+1; } if(s1[i]!=s2[tmp]) { tmp--; } if(tmp==n) { cout<<i-n+1<<"\n"; tmp=a[tmp]; } tmp++; } for(int i=1;i<=n;i++) { cout<<a[i]<<" "; } return 0; } ```