求助,蒟蒻六个点TLE
查看原帖
求助,蒟蒻六个点TLE
236416
_stOrz_楼主2021/7/5 16:36
#include<bits/stdc++.h>
using namespace std;

char s[1000005];
int n,next[1000005];


int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;
	long long sum = 0,j = 0;
	next[0] = next[1] = 0;
	for(int i = 1;i < n; i++)
	{
		while(j and s[i] != s[j]) j = next[j];
		if(s[i] == s[j]) j++;
		next[i + 1] = j;
	}
	for(int i = 1;i <= n; i++)
	{
		j = i;
		while(next[j]) j = next[j];
		if(next[j]) next[i] = j;
		sum += i - j;
	}
	cout << sum << "\n";
}
2021/7/5 16:36
加载中...