HDU6599 (字符串哈希+回文自动机)
题意:
求有多少个回文串的前⌈len/2⌉个字符也是回文串。(两组解可重复)
将这些回文串按长度分类,分别输出长度为1,2,...,n的合法串的数量。
题解:https://www.cnblogs.com/Cwolf9/p/11253106.html
我们使用回文自动机可以知道本质回文窜的个数与长度,我们在添加一个数组id[i], 记录第i个回文窜的结束位置就可以知道这个回文窜在原窜的区间[L,R];
我们在用字符串哈希就可以快速的判断前⌈len/2⌉个字符是不是回文了
,因为他本身是回文串,因此就是判断前后两部分是否相同
#include "bits/stdc++.h" using namespace std;
const double eps = 1e-;
#define reg register
#define lowbit(x) x&-x
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fi first
#define se second
#define makp make_pair int dcmp(double x) {
if (fabs(x) < eps) return ;
return (x > ) ? : -;
} typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = ;
const ull hash2 = ;
const int N = + ;
const int M = + ;
const int inf = 0x3f3f3f3f;
const ll mod = ;
ll ret[N];
ull ha[N], pp[N]; ull getha(int l, int r) {
if (l == ) return ha[r];
return ha[r] - ha[l - ] * pp[r - l + ];
} bool check(int l, int r) {
int len = r - l + ;
int mid = (l + r) >> ;
if (len & ) return getha(l, mid) == getha(mid, r);
else return getha(l, mid) == getha(mid + , r);
} struct Palindromic_Tree {
int nxt[N][], fail[N], cnt[N];
int num[N], len[N], s[N], id[N];
int last, n, p; int newnode(int l) {
memset(nxt[p], , sizeof(nxt[p]));
cnt[p] = num[p] = ;
len[p] = l;
return p++;
} void init() {
p = ;
newnode(), newnode(-);
last = n = ;
s[] = -;
fail[] = ;
} int get_fail(int x) {
while (s[n - len[x] - ] != s[n]) x = fail[x];
return x;
} void add(int c) {
c -= 'a';
s[++n] = c;
int cur = get_fail(last);
if (!nxt[cur][c]) {
int now = newnode(len[cur] + );
fail[now] = nxt[get_fail(fail[cur])][c];
nxt[cur][c] = now;
num[now] = num[fail[now]] + ;
}
last = nxt[cur][c];
cnt[last]++, id[last] = n;
} ll Count() {
for (int i = p - ; i >= ; i--) cnt[fail[i]] += cnt[i];
for (int i = ; i < p; i++) {
///cout << id[i] - len[i] << " " << id[i] - 1 << endl;
if (check(id[i] - len[i], id[i] - 1)) {
ret[len[i]] += cnt[i];
}
}
return ;
}
} pam; char str[N]; int main() {
pp[] = ;
for (int i = ; i < N; i++) {
pp[i] = hash1 * pp[i - ];
}
while (~scanf("%s", str)) {
memset(ret, , sizeof(ret));
pam.init();
int len = strlen(str);
ha[] = str[];
for (int i = ; i < len; i++) {
pam.add(str[i]);
}
for (int i = ; i < len; i++) {
ha[i] = ha[i - ] * hash1 + str[i];
}
pam.Count();
printf("%lld", ret[]);
for (int i = ; i <= len; i++) {
printf(" %lld", ret[i]);
}
printf("\n");
}
return ;
}
HDU6599 (字符串哈希+回文自动机)的更多相关文章
- hdu多校第二场1009 (hdu6599) I Love Palindrome String 回文自动机/字符串hash
题意: 找出这样的回文子串的个数:它本身是一个回文串,它的前一半也是一个回文串 输出格式要求输出l个数字,分别代表长度为1~l的这样的回文串的个数 题解: (回文自动机和回文树是一个东西) 首先用回文 ...
- HDU-6599 I Love Palindrome String(回文自动机+字符串hash)
题目链接 题意:给定一个字符串\(|S|\le 3\times 10^5\) 对于每个 \(i\in [1,|S|]\) 求有多少子串\(s_ls_{l+1}\cdots s_r\)满足下面条件 \( ...
- 字符串数据结构模板/题单(后缀数组,后缀自动机,LCP,后缀平衡树,回文自动机)
模板 后缀数组 #include<bits/stdc++.h> #define R register int using namespace std; const int N=1e6+9; ...
- [2019杭电多校第二场][hdu6599]I Love Palindrome String(回文自动机&&hash)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足 ...
- 2019 Multi-University Training Contest 2 I.I Love Palindrome String(回文自动机+字符串hash)
Problem Description You are given a string S=s1s2..s|S| containing only lowercase English letters. F ...
- 回文树(回文自动机PAM)小结
回文树学习博客:lwfcgz poursoul 边写边更新,大概会把回文树总结在一个博客里吧... 回文树的功能 假设我们有一个串S,S下标从0开始,则回文树能做到如下几点: 1.求串S前缀0~ ...
- URAL 2040 (回文自动机)
Problem Palindromes and Super Abilities 2 (URAL2040) 题目大意 给一个字符串,从左到右依次添加,询问每添加一个字符,新增加的回文串数量. 解题分析 ...
- 后缀自动机/回文自动机/AC自动机/序列自动机----各种自动机(自冻鸡) 题目泛做
题目1 BZOJ 3676 APIO2014 回文串 算法讨论: cnt表示回文自动机上每个结点回文串出现的次数.这是回文自动机的定义考查题. #include <cstdlib> #in ...
- [模板] 回文树/回文自动机 && BZOJ3676:[Apio2014]回文串
回文树/回文自动机 放链接: 回文树或者回文自动机,及相关例题 - F.W.Nietzsche - 博客园 状态数的线性证明 并没有看懂上面的证明,所以自己脑补了一个... 引理: 每一个回文串都是字 ...
随机推荐
- Mybatis-学习笔记(7)缓存机制
1.一级缓存 SqlSession级别的缓存,使用HashMap存储缓存数据,不同的SqlSession之间的缓存数据区域(HashMap)互不影响. 一级缓存的作用域是SqlSession范围(强调 ...
- tarjan算法应用 割点 桥 双连通分量
tarjan算法的应用. 还需多练习--.遇上题目还是容易傻住 对于tarjan算法中使用到的Dfn和Low数组. low[u]:=min(low[u],dfn[v])--(u,v)为后向边,v不是u ...
- 如何使用js在移动端和PC端居中
在手机移动端和PC端控制居中是一个很蛋痛的问题,因为屏幕宽度在变化,所以就不要写死样式,那么我想用JS来控制,灵活的控制宽度,需要注意这三个时候: (1)首先需要在页面刚加载的时候就调用此函数, (2 ...
- npm学习(一)之安装、更新以及管理npm版本
安装npm 安装前须知: npm是在Node中编写的,因此需要安装Node.js才能使用npm.可以通过Node.js网站安装npm,或者安装节点版本管理器NVM. 如果只是想开始探索npm,使用No ...
- linux的管道 |和grep命令以及一些其他命令(diff,echo,cat,date,time,wc,which,whereis,gzip,zcat,unzip,sort)
linux提供管道符号“|”,作用是命令1的输出内容作为命令2的输入内容.通常与grep命令一起使用. 格式:命令1 |命令2 grep命令:全称为global regular expression ...
- 简单的物流项目实战,WPF的MVVM设计模式(一)
新建一个WPF项目,命名为WMS 然后分别新建文件夹,Data,Models,Views,ViewModels,Services,如下图所示 然后通过NuGet安装连个Nuget包,分别为SQLite ...
- 使用Tensorflow搭建回归预测模型之一:环境安装
方法1:快速包安装 一.安装Anaconda 1.官网地址:https://www.anaconda.com/distribution/,选择其中一个版本下载即可,最好安装3.7版本,因为2.7版本2 ...
- SpringBoot项目优化和Jvm调优
https://www.cnblogs.com/jpfss/p/9753215.html 项目调优 作为一名工程师,项目调优这事,是必须得熟练掌握的事情. 在SpringBoot项目中,调优主要通过配 ...
- 1134. Vertex Cover (25)
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...
- CSS3边框 圆角效果 border-radius
border-radius是向元素添加圆角边框 使用方法: border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border-radius: 5px 5px 5px ...