为什么多写一个sho数组记录就T了呢?求dalao指点
查看原帖
为什么多写一个sho数组记录就T了呢?求dalao指点
265978
Retired楼主2021/5/14 18:36
// Problem: P3435 [POI2006]OKR-Periods of Words
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3435
// Memory Limit: 125 MB
// Time Limit: 1000 ms

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back 
#define in insert
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;

template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}

typedef pair<int,int>PII;
typedef pair<long,long>PLL;

typedef long long ll;
typedef unsigned long long ull; 
const int N=2e6+10,M=1e9+7;
ll n,m,_;
char p[N];
ll ne[N];//求串的最短前缀,后缀匹配
ll sho[N];//多写几遍sho竟然直接T了
void solve()
{
	cin>>n;cin>>p+1;
	for(int i=2,j=0;i<=n;i++)
	{
		while(j&&p[i]!=p[j+1])j=ne[j];
		if(p[i]==p[j+1])j++;
		ne[i]=j;
	}
	ll sum=0;	//求每个字符串的最短匹配长度,sum+=n-short(i);
	
	// for(int i=2;i<=n;i++)
	// {
		// int j=i;
		// while(ne[j])sho[i]=ne[j],j=ne[j];
		// if(sho[i])
			// sum+=i-sho[i];
// 	
	// }
	for(int i=2,j=2;i<=n;i++,j=i)
	{
		while(ne[j])j=ne[j];
		if(ne[i])ne[i]=j;
		sum+=i-j;
	}
	cout<<sum<<endl;
}

int main()
{
	solve();
	return 0;
}
2021/5/14 18:36
加载中...