Palindromes and Super Abilities (回文树-本质不同的回文串的种数)

https://cn.vjudge.net/contest/314885#problem/B

After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Misha has got an unusual ability. Now, when he reads a word, he can mentally count the number of unique nonempty substrings of this word that are palindromes.

Dima wants to test Misha’s new ability. He adds letters s 1, ..., s n to a word, letter by letter, and after every letter asks Misha, how many different nonempty palindromes current word contains as substrings. Which n numbers will Misha say, if he will never be wrong?

Input

The only line of input contains the string s 1... s n, where s i are small English letters (1 ≤ n ≤ 10 5).

Output

Output n numbers separated by whitespaces, i-th of these numbers must be the number of different nonempty substrings of prefix s 1... s i that are palindromes.

Example

input output
aba
1 2 3

题意:按照顺序每插入一个字符,输出其本质不同的回文串的数目

思路:即回文树的节点个数-2(减去那两个根) 

#include
using namespace std;
typedef long long ll;
const int N=1e5+10;
char s[N];
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 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]++;		
	}
	void count(){
		for(int i=p-1;i>=0;i--) cnt[fail[i]]+=cnt[i];
		//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
	}
}pam;
int main(){
    while(~scanf("%s",s)){
    	pam.init();
    	int len=strlen(s); 
    	for(int i=0;i

 

你可能感兴趣的:(回文树/回文自动机)