当前位置:网站首页>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;
}
边栏推荐
- 第3讲:MySQL数据库中常见的几种表字段数据类型
- Safe fifth after-school exercise
- 基于php动漫周边商城管理系统(php毕业设计)
- KMP 字符串匹配问题
- SOM Network 1: Principles Explained
- Scala practice questions + answers
- render-props and higher order components
- leetcode 204. Count Primes 计数质数 (Easy)
- File operations of WEB penetration
- Based on php online music website management system acquisition (php graduation design)
猜你喜欢

seaborn笔记:可视化统计关系(散点图、折线图)

Pagoda application experience

2022 edition of MySQL tutorial, top collection good, take your time

The must-have "fishing artifact" for programmers is here!

工程建筑行业数据中台指标分析

【开源】Sentinel高性能高可用集群限流解决方案

shell规范与变量

Mini Program--Independent Subcontracting & Subcontracting Pre-download

Based on php online examination management system acquisition (php graduation design)

Still struggling with reporting tool selection?To take a look at this
随机推荐
Flink cluster construction
SOM Network 1: Principles Explained
Port protocol for WEB penetration
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation
Getting Started Database Days4
ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
第3讲:MySQL数据库中常见的几种表字段数据类型
SOM Network 2: Implementation of the Code
Implementation principle of VGUgarbage collector (garbage collector)
Based on php online examination management system acquisition (php graduation design)
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
基于php影视资讯网站管理系统获取(php毕业设计)
Image fusion GANMcC study notes
Dichotomy Medium LeetCode6133. Maximum Number of Groups
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
Uses of Anacoda