[2017.7.6]回文树

 1 const int MAXN = 100005 ;
 2 const int N = 26 ;
 3
 4 struct Palindromic_Tree{
 5     int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
 6     int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
 7
 8     int cnt[MAXN] ;
 9     int num[MAXN] ;
10     int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
11     int S[MAXN] ;//存放添加的字符
12     int last ;//指向上一个字符所在的节点,方便下一次add
13     int n ;//字符数组指针
14     int p ;//节点指针
15
16     int newnode ( int l ) {//新建节点
17     for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
18     cnt[p] = 0 ;
19     num[p] = 0 ;
20     len[p] = l;
21     return p ++ ;
22     }
23
24     void init () {//初始化
25     p = 0;
26     newnode ( 0 ) ;
27     newnode ( -1 ) ;
28     last = 0;
29     n = 0;
30     S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
31     fail[0] = 1 ;
32     }
33
34     int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
35     while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;
36     return x;
37     }
38
39     void add ( int c ) {
40     c -= ‘a‘ ;
41     S[++ n] = c;
42     int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
43     if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
44         int now = newnode ( len[cur] + 2 ) ;//新建节点
45         fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
46         next[cur][c] = now ;
47         num[now] = num[fail[now]] + 1;
48     }
49     last = next[cur][c];
50     cnt[last] ++ ;
51     }
52     void count () {
53     for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
54     //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的回文子串!
55     }
56 };

From:blog.csdn.net/u013368721/article/details/42100363

时间: 2024-08-07 01:16:05

[2017.7.6]回文树的相关文章

回文树

(没有坑怎么填?) 最近膜了一些关于回文串的题目,感到非常有意思,遂开篇记录. 在逛UOJ的题目时发现了vfk添上了新题,APIO 2014的题目.本身是一件很正常的事,而它事实上也没有变成什么了不得的事.我看到了Palindrome这个标题---回文串已经烂大街了,没什么新意.不过我很早就向学习回文树这东西了,久仰其大名而未尝真正去了结果它,于是我就顺手撸了一把豪哥的论文,发现他讲解的实在是晦涩难懂---论文的通病,就是虽然表述没有歧义,但是难以理解.嘛,然后我就找了几个标程,发现回文树这东西

回文树或者回文自动机,及相关例题

回文树简述 在大部分说法中,回文树与回文自动机指的是一个东西: 回文树是对一个字符串,基于自动机思想构建的处理回文问题的树形结构: 回文树是对着一个单串建立的: 于是他主要用于计数(回文子串种类及个数) 基本建立思路是先建立其前缀的回文树,然后每加上一个字符,统计影响: 回文树存在fail指针但一般不承接字符串匹配问题: (回文树大概可以判定一个回文串是不是一个串的子串,但KMP之类的可以做得更好) 构建好的回文树,是这样的: (好难看) 可看出: 存在两个树结构,分别记录奇数|偶数长度的回文:

bzoj3676: [Apio2014]回文串 回文树

回文树的裸题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=500100; const int INF=1e9+10; struct PalinTree { int ch[maxn][26],f[maxn]; int

HDU3948 &amp; 回文树模板

Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Created time: 2016-04-20 20:55 # Filename: hdu3948.cpp # Description: =================================*/ #define me AcrossTheSky&HalfSummer11 #include &l

hdu5658 CA Loves Palindromic 回文树

回文树在处理回文方面真的比manacher要好用得多... #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; t

HDU 5157 Harry and magic string(回文树)

Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 223    Accepted Submission(s): 110 Problem Description Harry got a string T, he wanted to know the number of T's disjoint

CodeForces 17E Palisection(回文树)

E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard input output standard output In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remin

BZOJ 3676 [Apio2014]回文串(回文树)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3676 [题目大意] 考虑一个只包含小写拉丁字母的字符串s. 我们定义s的一个子串t的"出现值"为t在s中的出现次数乘以t的长度. 求s的所有回文子串中的最大出现值. [题解] 我们对给出串建立回文树,统计每个回文串出现次数和长度,相乘取组大即可 [代码] #include <cstdio> #include <algorithm> #include

HYSBZ 3676 回文串 (回文树)

3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 1680  Solved: 707 [Submit][Status][Discuss] Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的"出 现值"为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行,为一个只包含小写字母(a -z)的非空字符串s. Output