应该是数据过水?
查看原帖
应该是数据过水?
553035
封禁用户楼主2024/12/20 18:59

rt 如下代码可以通过 :

#include<bits/stdc++.h>
const int QWQ=2e6+5;
using namespace std;
using LL=long long;
inline int read(){
	int x=0,f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())
		if (ch=='-') f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())
		x=(x<<1)+(x<<3)+(ch^48);
	return x*f;
}
int to[QWQ][26],len[QWQ],Fa[QWQ];
char c[QWQ]; int n,Snd,las,w[QWQ];
vector<int> E[QWQ]; LL res;
inline void insert(int c){
	int cur=++Snd,p,q; len[cur]=len[las]+1,w[cur]=1;
	for (p=las;~p&&!to[p][c];p=Fa[p]) to[p][c]=cur;
	if (!~p) return (las=cur,Fa[cur]=0),void(); q=to[p][c];
	if (len[q]==len[p]+1) return (las=cur,Fa[cur]=q),void();
	int copy=++Snd; Fa[copy]=Fa[q],Fa[q]=copy;
	len[copy]=len[p]+1,Fa[cur]=copy,las=cur;
	for (int i=0;i<26;i++) to[copy][i]=to[q][i];
	for (;~p&&to[p][c]==q;p=Fa[p]) to[p][c]=copy;
}
void dfs(int u){
	for (int v:E[u]) dfs(v),w[u]+=w[v];
	res+=(LL)w[u]*(n-w[u])*(len[u]-len[Fa[u]]);
}
signed main(){
	scanf("%s",c+1),n=strlen(c+1),Fa[0]=-1;
	for (int i=1;i<=n;i++) insert(c[i]-'a');
	for (int i=1;i<=Snd;i++) E[Fa[i]].push_back(i);
	dfs(0),printf("%lld",res);
	return 0;
}

重点是并没有建反串? 这是为什么?

2024/12/20 18:59
加载中...