题目
对于两个串A和B ,给出一种算法使得能够保证正确地求出最大的L值,使得A串中长为L的前缀与B串中长为L的后缀相等,并分析该算法的复杂度。
思路和复杂度分析
-
对于本题,将串s1与串s2合并得到字符串P,通过求串P的前缀与后缀相同的最大长度,得到题解。
-
求串P的前缀与后缀相同的最大长度进一步转换为在KMP算法中所求的next数组的值
-
但是要注意调整的是:要求next数组长度是比字符串长度P多一位,next最后一位的值就是最大的L值
在KMP算法中,求next数组是在一次循环中完成,对于每个位置的next值,都是根据前几个已知的值求得,因此其时间复杂度为O(n)O(n)O(n),空间复杂度也为O(n)O(n)O(n) 。
C++代码
#include<iostream>
#include <algorithm>
#include <string>
using namespace std;
int* findNext(string P) {
int i, k;
int m = P.length(); // m为模式P的长度
int* next = new int[m]; // 动态存储区开辟整数数组
next[0] = -1;
i = 0; k = -1;
while (i < m ) { // 若写成 i < m 会越界
while (k >= 0 && P[k] != P[i]) // 采用 KMP 找最大首尾子串
k = next[k]; // k 递归地向前找
i++;
k++;
next[i] = k;
}
return next;
}
int main() {
string s1, s2;
cin >> s1 >> s2;
string P = s1 + s2;
int* next = new int[100];
next = findNext(P);
cout << next[P.length()];
}