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 - 博客园 状态数的线性证明 并没有看懂上面的证明,所以自己脑补了一个... 引理: 每一个回文串都是字 ...
随机推荐
- Highways POJ-1751 最小生成树 Prim算法
Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...
- 洛谷 P1631 序列合并(优先队列)
传送门 解题思路 首先读入a.b数组后,sort一遍(从小到大),然后把a[1]+b[1],a[2]+b[1],a[3]+b[1]……a[n]+b[1]全部加入一个优先队列q(小根堆). 然后从一到n ...
- [LeetCode] 154. 寻找旋转排序数组中的最小值 II
题目链接 : https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目描述: 假设按照升序排序的数组在预 ...
- 排列perm HYSBZ - 1072(状压dp/暴力)
Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为2的有30种,末位为4的有60种. Input ...
- 使用 ref 和 out 传递数组注意事项
1.与所有的 out参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由被调用方为其赋值 示例 :在此例中,在调用方(Main 方法)中声明数组 theArray,并在 FillArra ...
- 剑指offer-平衡二叉树-python
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 思路: 平衡二叉树 (AVL)平衡二叉树是一种二叉排序树,其中每个结点的左子树和右子树的高度差至多等于1.它是一种高度平衡的二叉排序树.意思是 ...
- C# 中的DevExpress控件的使用
使用教程:http://training.evget.com/video/5110 C# System 程序集https://msdn.microsoft.com/zh-cn/library/mt4 ...
- C# List<Object>值拷贝
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- PAT Basic 1036 跟奥巴马一起编程 (15 分)
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014 年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个 ...
- PAT Advanced 1065 A+B and C (64bit) (20 分)(关于g++和clang++修改后能使用)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...