I Love Palindrome String(回文树+manacher)

 

http://acm.hdu.edu.cn/showproblem.php?pid=6599

 

Problem Description

You are given a string S=s1s2..s|S| containing only lowercase English letters. For each integer i∈[1,|S|] , please output how many substrings slsl+1...sr satisfy the following conditions:

∙ r−l+1 equals to i .

∙ The substring slsl+1...sr is a palindrome string.

∙ slsl+1...s⌊(l+r)/2⌋ is a palindrome string too.

|S| denotes the length of string S .

A palindrome string is a sequence of characters which reads the same backward as forward, such as madam or racecar or abba .

 

 

Input

There are multiple test cases.

Each case starts with a line containing a string S(1≤|S|≤3×105) containing only lowercase English letters.

It is guaranteed that the sum of |S| in all test cases is no larger than 4×106 .

 

 

Output

For each test case, output one line containing |S| integers. Any two adjacent integers are separated by a space.

 

 

Sample Input

 

abababa

 

 

Sample Output

 

7 0 0 0 3 0 0

 

 

 

用回文树找出不同本质的回文串,然后用Manacher判断一下是否符合题意,符合就加上该回文串的数目

 

初学,理解的也不到位,就不多说了

名词介绍https://blog.csdn.net/qq_30974369/article/details/79349235 

讲解https://blog.csdn.net/liujianfei526/article/details/47702005

 怎么用https://blog.csdn.net/Quack_quack/article/details/50375306

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+5;
char s[N],a[N<<1];
int mp[N<<1];
void manacher(char s[],int len,int p[]){
	int l=0;
	a[l++]='$';
	a[l++]='#';
	for(int i=0;i<len;i++){
		a[l++]=s[i];
		a[l++]='#';
	}
	a[l]=0;
	int mx=0,id=0;
	for(int i=0;i<l;i++){
		p[i]=mx>i?min(p[2*id-i],mx-i):1;
		while(a[i+p[i]]==a[i-p[i]]) p[i]++;
		if(i+p[i]>mx){
			mx=i+p[i];
			id=i;
		}
	}
}
struct Palindromic_Tree{
	int next[N][26];//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
	int fail[N];//fail指针,失配后跳转到fail指针指向的节点     最长回文后缀 
	int len[N];//len[i]表示节点i表示的回文串的长度  
	int S[N];//存放添加的字符
	ll cnt[N];//结点表示的本质不同的回文串的个数(调用count()后)
	int num[N];//结点表示的最长回文串的最右端点为回文串结尾的回文串个数
	int last;;//指向上一个字符所在的节点,方便下一次add 
	int n;//字符数组指针  
    int p;//节点指针
    
	int begin[N],end[N];
	
	int newnode(int x){//新建节点 
		memset(next[p],0,sizeof(next[p]));
		cnt[p]=0;
		num[p]=0;
		len[p]=x;
		return p++;
	}
	void init(){
		p=0;
		newnode(0);
		newnode(-1);
		last=0;
		n=0;
		S[0]=-1;//开头放一个字符集中没有的字符,减少特判 
		fail[0]=1;
	}
	int get_fail(int x){//和KMP一样,失配后找一个尽量最长的  
		while(S[n-len[x]-1]!=S[n]) x=fail[x];
		return x;
	} 
	void add(int c){
		c-='a';
		S[++n]=c;
		int cur=get_fail(last);//通过上一个回文串找这个回文串的匹配位置
		if(!next[cur][c]){//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串  
			int now=newnode(len[cur]+2);//新建节点
			fail[now]=next[get_fail(fail[cur])][c];//和AC自动机一样建立fail指针,以便失配后跳转  
			num[now]=num[fail[now]]+1;
			next[cur][c]=now;
		} 
		last=next[cur][c];
		cnt[last]++;
		
		
		end[last]=n;
		begin[last]=end[last]-len[last]+1;
		
	}
	void count(){
		for(int i=p-1;i>=0;i--) cnt[fail[i]]+=cnt[i];
		//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
	}
	
	ll ans[N];
	void solve(int l,int r,ll cnt){
		--r,--l;
		int m=(l+r)>>1;
		int x=m-l+1;
		int L=(l<<1)+2;
		int R=(m<<1)+2;
		int pos=(L+R)>>1;
		if(mp[pos]-1>=x) ans[r-l+1]+=cnt;
    }
    void solve(){
    	for(int i=1;i<=n;i++) ans[i]=0;
    	for(int i=2;i<p;i++) solve(begin[i],end[i],cnt[i]);
    	for(int i=1;i<=n;i++) printf("%lld%c",ans[i]," \n"[i==n]);
	}
}pam;
int main(){
    while(~scanf("%s",s)){
    	pam.init();
    	int len=strlen(s);
    	manacher(s,len,mp);
    	for(int i=0;i<len;i++) pam.add(s[i]);
    	pam.count();
    	pam.solve();
	}
	return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值