当前位置:网站首页>PAM 回文自动机
PAM 回文自动机
2022-08-01 21:59:00 【ThXe】
性质
1.本质不同(位置可以相同)的回文串数量等于回文自动机的节点数 − 2 ;
2.一个回文串出现的次数等于以之为根的子树的各节点作为 last的次数之和;
3.位置不同的回文串的数量等于各个节点(除 even 和 odd)对应的回文串出现的次数之和;
功能
1.求本质不同的回文串数量
2.求某个字符串作为回文串出现的次数
3.以i号字符为结尾的回文串个数
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
char str[maxn];
struct PAM {
int size, last, r0, r1;
int trie[maxn][26], fail[maxn], len[maxn], cnt[maxn];//cnt[last]为i号节点为结尾
PAM() {
r0 = size++, r1 = size++; last = r1;
len[r0] = 0, fail[r0] = r1;
len[r1] = -1, fail[r1] = r1;
}
void insert(int ch, int idx) {
int u = last;
while (str[idx] != str[idx - len[u] - 1])u = fail[u];
if (!trie[u][ch]) {
int cur = ++size, v = fail[u];
len[cur] = len[u] + 2;
for (; str[idx] != str[idx - len[v] - 1]; v = fail[v]);
fail[cur] = trie[v][ch]; trie[u][ch] = cur;
cnt[cur] = cnt[fail[cur]] + 1;
}
last = trie[u][ch];
//cnt[last]++ 统计某个串出现次数时需要
}
void build(char* str) {
int len = strlen(str);
for (int i = 0; i < len; i++)
insert(str[i] - 'a' + 1, i);
}
}pam;
int main() {
scanf("%s", str);
pam.build(str);
int len = strlen(str);
long long ans = 0;
//op1:统计所有回文串个数
for (int i = 1; i <= pam.size; i++)
ans += pam.cnt[i];
printf("%d", ans);
//op2:输出i号为结尾的回文串个数
for (int i = 0; i < len; i++) {
pam.insert(str[i] - 'a' + 1, i);
printf("%d ", pam.cnt[pam.last]);
}
//op3:某个串出现多少次,需要在build的时候维护信息
for (int i = pam.size; i >= 0; i--) {
pam.cnt[pam.fail[i]] += pam.cnt[i];
ans = max(ans, 1ll * pam.cnt[i] * pam.len[i]);
}
return 0;
}
边栏推荐
- Spark cluster construction
- [Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
- 【移动Web】移动端适配
- Spark练习题+答案
- Spark shuffle调优
- 求解多元多次方程解的个数
- 漫长的投资生涯
- LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
- SOM网络1:原理讲解
- Based on php hotel online reservation management system acquisition (php graduation project)
猜你喜欢
今年的很美味
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
基于php酒店在线预定管理系统获取(php毕业设计)
Getting Started Database Days4
Based on php online examination management system acquisition (php graduation design)
基于php在线学习平台管理系统获取(php毕业设计)
Pagoda application experience
基于php旅游网站管理系统获取(php毕业设计)
模拟数据之mockjs
基于php影视资讯网站管理系统获取(php毕业设计)
随机推荐
SOM Network 2: Implementation of the Code
Small program -- subcontracting
第一讲 测试知多少
第3讲:MySQL数据库中常见的几种表字段数据类型
基于php在线音乐网站管理系统获取(php毕业设计)
C语言必杀技3行代码把运行速度提升4倍
求解多元多次方程解的个数
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
Unity Shader general lighting model code finishing
KMP 字符串匹配问题
SAP Spartacus Accessibility E2E 端到端测试
render-props and higher order components
No more rolls!After joining ByteDance for a week, he ran decisively.
Based on php online learning platform management system acquisition (php graduation design)
Yizhou Financial Analysis | The intelligent transformation of bank ATM machines is accelerated; the new Internet loan regulations bring challenges
LeetCode952三部曲之二:小幅度优化(137ms -> 122ms,超39% -> 超51%)
Centos7--MySQL的安装
小程序中的多表联合查询
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined