题目分析
除去kmp外;x的值实际上为 L - next[L];
代码示例
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
char s[N];
int next1[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int l; cin >> l >> s;
int i = 1, j = 0;
next1[0] = next1[1] = 0;
while (i < l) {
if (s[i] == s[j]) next1[++ i] = ++j;
else if (j == 0) next1[++ i] = 0;
else j = next1[j];
}
cout << (l - next1[l]) << '\n';
}