那么这样的话,是会超时的,我们可以通过记忆化,如果在 i 时,nxt_i 不为 0,我们就可以将其赋值给 nxt_i,避免下次再跑
细节:
## 代码:
# CODE
```cpp
#include <iostream>
#include <cstring>
#include <cstdio>
#define ll long long
const int N = 1e6 + 7;
using namespace std;
char s[N];
int len;
ll ans;
int nxt[N];
inline void pre() {
int j = 0;
for (int i = 2; i <= len; i ++) {
while (j && s[j + 1] != s[i])
j = nxt[j];
if (s[j + 1] == s[i])
j ++;
nxt[i] = j;
}
}
int main() {
cin >> len;
scanf("%s", s + 1);
pre();
for (int i = 2; i <= len; i ++) {
int j = i;
while (nxt[j])
j = nxt[j];
if (nxt[i])
nxt[i] = j;
ans += i - j;
}
cout << ans << endl;
return 0;
}
```