回文树 学习

回文树学习

代码是模仿ctl学长的。ctlnb!!!
https://blog.csdn.net/u013368721/article/details/42100363 推荐博客

/*
  user: sky48
  language: c++
  for dream for young
*/
#include<iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define N 300005
typedef long long ll;
using namespace std;
int cnt[N],len[N],fail[N],s[N],to[N][30];
ll ans;
char ch[N];
int tot,n,L,last;
int gets()
{
	int v=0; char ch;bool f=0;
	while(!isdigit(ch=getchar())) if(ch=='-') f=1; v=ch-48;
	while( isdigit(ch=getchar())) v=(v<<1)+(v<<3)+ch-48;
	return f?-v:v;
}
void add(int c){
	s[++n]=c;
	int cur,tmp,now;
	for(cur=last;s[n-len[cur]-1]!=s[n];cur=fail[cur]);
	if(!to[cur][c]){
		len[++tot]=len[cur]+2; now=tot;
		for(tmp=fail[cur];s[n-len[tmp]-1]!=s[n];tmp=fail[tmp]);
		fail[now]=to[tmp][c]; 
		to[cur][c]=now;
		
	}last=to[cur][c];
	cnt[last]++; 
	
}
int main ()
{
//  freopen("tt.txt","r",stdin);
	scanf("%s",ch); L=strlen(ch);
	len[tot]=0, len[++tot]=-1;
	fail[0]=1, fail[1]=1, s[0]=-1; 
	rep(i,0,L-1) add(ch[i]-'a'); 
	for(int i=tot;i>=2;i--) cnt[fail[i]]+=cnt[i];
	rep(i,2,tot) ans=max(ans,(ll)cnt[i]*len[i]);
	printf("%lld\n",ans);
	
	return 0;
}

首先,记住,to[x][c]这个是构成树,是用来统计答案的。
而fail[x]是失配以后跳的
now是树上的节点编号。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值