当前位置:网站首页>PAM Palindromic Automata
PAM Palindromic Automata
2022-08-01 22:07:00 【ThXe】
性质
1.本质不同(The location can be the same)The number of palindromic strings is equal to the number of nodes in the palindromic automaton − 2 ;
2.The number of occurrences of a palindrome is equal to the number of nodes in the subtree rooted at it lastsum of times;
3.The number of palindromes with different positions is equal to each node(除 even 和 odd)The sum of the occurrences of the corresponding palindrome;
功能
1.Find the number of essentially distinct palindromes
2.Find the number of times a string appears as a palindrome
3.以iThe sign character is the number of ending palindrome strings
#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]为iNo. node is the end
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]++ Required to count the number of occurrences of a string
}
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:Count the number of all palindrome strings
for (int i = 1; i <= pam.size; i++)
ans += pam.cnt[i];
printf("%d", ans);
//op2:输出iThe number of palindromes at the end
for (int i = 0; i < len; i++) {
pam.insert(str[i] - 'a' + 1, i);
printf("%d ", pam.cnt[pam.last]);
}
//op3:How many times a string appears,需要在buildwhen maintaining information
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;
}
边栏推荐
猜你喜欢

_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined

SOM网络2: 代码的实现

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

Homework 8.1 Orphans and Zombies

No more rolls!After joining ByteDance for a week, he ran decisively.

Delicious this year

xctf攻防世界 Web高手进阶区 web2

Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
![[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道](/img/6b/d4ff120493e878fcf5c9aa728eced7.png)
[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道

Based on php tourism website management system acquisition (php graduation design)
随机推荐
小程序容器+自定义插件,可实现混合App快速开发
SAP ABAP OData 服务如何支持删除(Delete)操作试读版
小程序中的多表联合查询
Flutter基础学习(一)Dart语言入门
(*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
render-props and higher order components
[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道
SAP Spartacus NgExpressEngineDecorator 的工作原理
0DFS Medium LeetCode6134. Find the closest node to the given two nodes
高等代数_证明_矩阵的任意特征值的代数重数大于等于其几何重数
Uses of Anacoda
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
递归(各经典例题分析)
越长大越孤单
感觉自己好傻
【C语言实现】整数排序-四种方法,你都会了吗、
Recycling rental system 100% open source without encryption Mall + recycling + rental
入门数据库Days4
scikit-learn no moudule named six