https://www.luogu.com.cn/problem/P3435
从当前字符串末尾的next数组的数开始递归,比如长度是8,next[8]是5,递归到5,next[5]是2,递归到2,假如next[2]是0,则当前字符长度为8的最短匹配长度是2
const int N = 1000000 + 50;
string s = " ";
string ss;
int ne[N];
void solve()
{
int n;
cin >> n;
cin >> ss;
s += ss;
for (int i = 2, j = 0; i <= n; i++)
{
while(j && s[i] != s[j + 1]) j = ne[j];
if (s[i] == s[j + 1]) j++;
ne[i] = j;
}
ll ans = 0;
for (int i = 1; i <= n; i++)
{
if (ne[i] != 0)
{
int j = ne[i];
while (ne[j]) j = ne[j];
if (ne[i]) ne[i] = j;
ans += i - j;
}
}
cout << ans << '\n';
}