「学习笔记」回文树/回文自动机(Palindromic Tree)

引入

有时候题目要求一些这样的问题
1. 求以串 s s 本质不同的回文串个数(即长度不同或长度相同且至少有一个字符不相同的字符串)
2. 求以位置i结尾的回文串个数。

这时候使用Manacher显然有点力不从心,我们可以使用一种比较新颖的字符串处理工具回文树(Palindromic Tree)。

回文树的结构

回文树其实是由两棵树组成的森林,第一棵树的根节点是 odd o d d ,第二棵树的根节点是 even e v e n 。每个森林中的节点其实是原串中的一个回文串。

每个节点保存以下信息:

  1. len l e n 表示当前节点代表的回文串的长度。特别的, lenodd=1 l e n o d d = − 1 , leneven=1 l e n e v e n = 1
  2. fail f a i l 表示当前节点失配以后可能匹配的最长回文串(即当前节点的最长回文后缀),特别的 failodd=odd f a i l o d d = o d d , faileven=even f a i l e v e n = e v e n
  3. ch c h 表示当前节点的孩子,其中 cha c h a 表示在当前字符串前后接上字符 a a 所形成的新回文串。

示例:下图是字符串babbab的回文树(实线表示 ch c h ,虚线表示 fail f a i l )

Markdown

其实就是论文1里的图啦
不难看出 odd o d d 的子树保存的都是奇数长度的回文串,而 even e v e n 的子树中保存的都是偶数长度的回文串。

回文树的构造

回文树的构造采用增量构造。
假设我们已经构造串 s s 的回文树。现在要求出在s后添加字符 c c 的回文树。

定理:以新加入的字符为结尾的,且未在s中出现的回文字符串最多有 1 1 个,且必为新串的最长回文后缀。

所以只需要求出新串的最长回文后缀即可。不妨设原串s的最长回文后缀为 si..|s| s i . . | s | ,那么只要 si1=c s i − 1 = c ,则新串的最长回文后缀一定为 si1..|s|+1 s i − 1.. | s | + 1 ,否则转移到 failsi..|s| f a i l s i . . | s | ,继续之前的操作。

如果新串的最长回文后缀没有在回文树中,则新建一个节点并找出它的 fail f a i l ,方法同上面类似。

回文树的复杂度

可以证明一个串 s s 本质不同的回文串不超过|s|个,所以状态数为 O(|s|) O ( | s | )

而通过势能分析可以证明前文所述的方法时间复杂度为 O(|s|log) O ( | s | l o g ∑ ) (使用平衡树或c++中的map表示 ch c h )或 O(|s|) O ( | s | ) (使用数组表示 ch c h )。其中 为字符集大小。

回文树的一些扩展

  1. 从前端插入([HDU5241]Victor and String)
  2. 前后端插入,删除字符
  3. 可持久化回文树

老实说,除了第一个我都不会我还是太菜了。但论文里有详细讲解。

模板题

[APIO2014]Palindrome

Description

考虑一个只包含小写字母的字符串 s s ,定义一个回文串的出现值为t s s 中的出现次数与其长度的乘积。求s的所有回文字串中的最大出现值。

Solution

首先建出 s s 的回文树,定义vt=i=1|s|[ts1..i]。可以证明最终的出现次数就是所有儿子(包括自身) v v 值的和。那么只需要求出v即可。方法是每次添加一个字符进回文串时给新串的 v v +1

#include <bits/stdc++.h>
using namespace std;

const int maxn = 300005;
int ch[maxn][26], f[maxn], v[maxn], l[maxn], n, la, tot;
char s[maxn];
long long ans;

inline void add(int c, int n)
{
    int x = la;
    while(s[n - l[x] - 1] != s[n]) x = f[x];
    if(!ch[x][c]) {
        int v = ++tot, k = f[x]; l[v] = l[x] + 2;
        while(s[n - l[k] - 1] != s[n]) k = f[k];
        f[v] = ch[k][c]; ch[x][c] = v;
    }
    v[la = ch[x][c]]++; return;
}

int main()
{
    freopen("palindrome.in", "r", stdin);
    freopen("palindrome.out", "w", stdout);

    scanf("%s", s + 1); n = strlen(s + 1);
    l[++tot] = -1; f[0] = f[1] = 1;
    for(int i = 1; i <= n; ++i) add(s[i] - 97, i);
    for(int i = tot; i; --i) ans = max(ans, 1ll * l[i] * v[i]), v[f[i]] += v[i];
    printf("%lld\n", ans);
    return 0;
}

  1. 参考资料:国家集训队2017论文集《回文树及其应用》——翁文涛。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 编写非递归遍历算法 对于二叉的遍历,常用的非递归方法有使用栈和Morris遍历。这里我们使用栈实现非递归遍历。 先序遍历: ```python def preorder(root): stack = [root] res = [] while stack: node = stack.pop() if node: res.append(node.val) stack.append(node.right) stack.append(node.left) return res ``` 中序遍历: ```python def inorder(root): stack = [] node = root res = [] while stack or node: while node: stack.append(node) node = node.left node = stack.pop() res.append(node.val) node = node.right return res ``` 后序遍历: ```python def postorder(root): stack = [(root, False)] res = [] while stack: node, visited = stack.pop() if node: if visited: res.append(node.val) else: stack.append((node, True)) stack.append((node.right, False)) stack.append((node.left, False)) return res ``` 2. 实现给定一棵二叉的先序遍历序列和中序遍历序列,创建这棵。 我们可以使用递归的方式进行构建。先序遍历的第一个节点必定是根节点,然后在中序遍历中找到这个节点,左边的是左子,右边的是右子。 代码实现如下: ```python def buildTree(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] root = TreeNode(root_val) idx = inorder.index(root_val) root.left = buildTree(preorder[1:idx+1], inorder[:idx]) root.right = buildTree(preorder[idx+1:], inorder[idx+1:]) return root ``` 3. 输出二叉的后序遍历结点序列 后序遍历的顺序是左右根,可以使用递归的方式实现: ```python def postorderTraversal(root): if not root: return [] return postorderTraversal(root.left) + postorderTraversal(root.right) + [root.val] ``` 也可以使用非递归的方式实现,类似于非递归的先序遍历: ```python def postorderTraversal(root): if not root: return [] stack = [(root, False)] res = [] while stack: node, visited = stack.pop() if node: if visited: res.append(node.val) else: stack.append((node, True)) stack.append((node.right, False)) stack.append((node.left, False)) return res ``` 4. 输出二叉的叶子结点 叶子结点是指没有左右子的节点,在遍历的过程中,遇到叶子结点就把它加到结果中即可。可以使用递归的方式实现。 ```python def getLeaves(root): if not root: return [] if not root.left and not root.right: return [root.val] return getLeaves(root.left) + getLeaves(root.right) ``` 也可以使用非递归的方式实现,使用栈来记录遍历的节点: ```python def getLeaves(root): if not root: return [] stack = [root] res = [] while stack: node = stack.pop() if node: if not node.left and not node.right: res.append(node.val) else: stack.append(node.right) stack.append(node.left) return res ``` 5. 统计二叉的结点个数 二叉的结点个数包括根节点、左子的结点个数和右子的结点个数。可以使用递归的方式实现。 ```python def countNodes(root): if not root: return 0 return countNodes(root.left) + countNodes(root.right) + 1 ``` 也可以使用非递归的方式实现,使用栈来记录遍历的节点: ```python def countNodes(root): if not root: return 0 stack = [root] count = 0 while stack: node = stack.pop() if node: count += 1 stack.append(node.left) stack.append(node.right) return count ``` 6. 求二叉的深度 二叉的深度等于左右子深度的最大值加1。可以使用递归的方式实现。 ```python def maxDepth(root): if not root: return 0 return max(maxDepth(root.left), maxDepth(root.right)) + 1 ``` 也可以使用非递归的方式实现,使用队列来记录遍历的节点,并记录每个节点的深度: ```python def maxDepth(root): if not root: return 0 queue = [(root, 1)] depth = 0 while queue: node, cur_depth = queue.pop(0) if node: depth = max(depth, cur_depth) queue.append((node.left, cur_depth+1)) queue.append((node.right, cur_depth+1)) return depth ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值