I Love Palindrome String HDU - 6599 回文树+hash
题意:
输出每个长度下的回文串(这些回文串的左半边也需要是回文串)
题解:
直接套上回文树,然后每找到一个回文串就将他hash。
因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半边相同,
自己画个图很容易发现的
每次hash判断一下就好了
1 #include <set> 2 #include <map> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstring> 11 #include <iostream> 12 #include <algorithm> 13 #include <unordered_map> 14 15 #define pi acos(-1.0) 16 #define eps 1e-9 17 #define fi first 18 #define se second 19 #define rtl rt<<1 20 #define rtr rt<<1|1 21 #define bug printf("******\n") 22 #define mem(a, b) memset(a,b,sizeof(a)) 23 #define name2str(x) #x 24 #define fuck(x) cout<<#x" = "<<x<<endl 25 #define sfi(a) scanf("%d", &a) 26 #define sffi(a, b) scanf("%d %d", &a, &b) 27 #define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c) 28 #define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d) 29 #define sfL(a) scanf("%lld", &a) 30 #define sffL(a, b) scanf("%lld %lld", &a, &b) 31 #define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c) 32 #define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d) 33 #define sfs(a) scanf("%s", a) 34 #define sffs(a, b) scanf("%s %s", a, b) 35 #define sfffs(a, b, c) scanf("%s %s %s", a, b, c) 36 #define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d) 37 #define FIN freopen("../in.txt","r",stdin) 38 #define gcd(a, b) __gcd(a,b) 39 #define lowbit(x) x&-x 40 #define IO iOS::sync_with_stdio(false) 41 42 43 using namespace std; 44 typedef long long LL; 45 typedef unsigned long long ULL; 46 const ULL seed = 13331; 47 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; 48 const int maxn = 1e6 + 7; 49 const int maxm = 8e6 + 10; 50 const int INF = 0x3f3f3f3f; 51 const int mod = 1e9 + 7; 52 char s[maxn]; 53 ULL p[maxn], HA[maxn]; 54 int ans[maxn]; 55 56 ULL get_HASH(int l, int r) { 57 return HA[r] - HA[l - 1] * p[r - l + 1]; 58 } 59 60 struct Palindrome_Automaton { 61 int len[maxn], next[maxn][26], fail[maxn], cnt[maxn]; 62 int num[maxn], str[maxn], sz, n, last; 63 int vis[maxn]; 64 65 int newnode(int l) { 66 for (int i = 0; i < 26; ++i)next[sz][i] = 0; 67 cnt[sz] = num[sz] = 0, len[sz] = l; 68 return sz++; 69 } 70 71 void init() { 72 sz = n = last = 0; 73 newnode(0); 74 newnode(-1); 75 str[0] = -1; 76 fail[0] = 1; 77 mem(vis, 0); 78 } 79 80 int get_fail(int x) { 81 while (str[n - len[x] - 1] != str[n])x = fail[x]; 82 return x; 83 } 84 85 void add(int c, int pos) { 86 c -= 'a'; 87 str[++n] = c; 88 int cur = get_fail(last); 89 if (!next[cur][c]) { 90 int now = newnode(len[cur] + 2); 91 fail[now] = next[get_fail(fail[cur])][c]; 92 next[cur][c] = now; 93 num[now] = num[fail[now]] + 1; 94 int temp = (len[now] + 1) / 2; 95 // fuck(n - len[now] + 1),fuck(n - len[now] + temp),fuck(n - temp),fuck(n); 96 if (len[now] == 1 || 97 get_HASH(n - len[now] + 1, n - len[now] + temp) == get_HASH(n - temp+1, n)) 98 vis[now] = 1; 99 else vis[now] = 0; 100 } 101 last = next[cur][c]; 102 cnt[last]++; 103 } 104 105 void count()//统计本质相同的回文串的出现次数 106 { 107 for (int i = sz - 1; i >= 0; --i) { 108 cnt[fail[i]] += cnt[i]; 109 if (vis[i]) ans[len[i]] += cnt[i]; 110 } 111 //逆序累加,保证每个点都会比它的父亲节点先算完,于是父亲节点能加到所有子孙 112 } 113 } pam; 114 115 int main() { 116 // FIN; 117 p[0] = 1; 118 for (int i = 1; i < maxn; ++i) p[i] = p[i - 1] * seed; 119 while (~sfs(s + 1)) { 120 int n = strlen(s + 1); 121 pam.init(); 122 for (int i = 1; i <= n; ++i) { 123 HA[i] = HA[i - 1] * seed + s[i]; 124 pam.add(s[i], i); 125 ans[i] = 0; 126 } 127 pam.count(); 128 for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], (i == n ? '\n' : ' ')); 129 } 130 return 0; 131 }
View Code
转载于:https://www.cnblogs.com/qldabiaoge/p/11403639.html
I Love Palindrome String HDU - 6599 回文树+hash相关推荐
- 2019HDU多校 I Love Palindrome String HDU - 6599 回文树
题目链接:https://cn.vjudge.net/problem/HDU-6599 题解:输出每个长度下的回文串 题解:其实就是对于每一个本质不同的回文串,先判断他是不是符合条件(左一半是不是也是 ...
- hdu 6599(回文树+hash)
题目 思路:先建好一棵回文树,然后问题就是如何快速判断一个结点代表的回文串是不是题目要求的回文串,暴力判断就是顺着后缀链接找,然后就T了 emmm.而实际上在插入一个结点时,这个代表的回文串我们可以通 ...
- HDU - 6599 I Love Palindrome String (回文树+Manacher、回文树+hash)
题目链接 题意 一个长度为3e5的字符串,求长度为iii的字符串满足字符是回文串而且字符串的前一半也是回文串的个数 思路 回文数求出所有的回文字符串,然后用Manacher或者Hash判断是否符合条件 ...
- 2019牛客暑期多校训练营(第四场)I - String (后缀自动机+回文树)
题目链接 题意 给一个字符串,求字符串子串的最大集合,集合中字符串互不相等,相等的定义为:a≠ba =\not ba≠b 而且 a≠rev(b)a =\not rev(b)a≠rev(b) ...
- HDU2019多校第二场 1009(HDU 6599) I Love Palindrome String(回文树(自动机)+manacher)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 解题思路: 回文自动机求每个本质不同的子串出现的次数,同时记录每个节点i代表的回文串第一次出现的 ...
- HDU6599I Love Palindrome String 回文树+哈希
点我看题 题意: 给出一个长度为N的字符串,要求输出一个长度为N的数组A, A[i]表示长度为i的good substring的数量 good substring 的定义是 该子串是回文串,且该子串的 ...
- HDU 5157(回文树)
传送门 题面: Harry got a string T, he wanted to know the number of T's disjoint palindrome substring pair ...
- 回文串问题的克星——Palindrome Tree(回文树)/Palindrome Automaton(回文自动机)学习小记
前言 最近B组有一道我不会的题,赶紧学习. 简介 我们知道,Manacher算法可以在 O(n) O ( n ) O(n)的时间内求出以每个位置为中心的最长回文串(虽然我昨天还不知道Manacher算 ...
- C. Palindrome Basis 完全背包 回文串
链接:https://codeforces.com/contest/1673/problem/C 对于每个回文串,都有取和不取两种作法,而且数量不做限制,故用完全背包. 设i为回文串,则有状态转移方程 ...
最新文章
- 挨踢项目求生法则-团队建设篇
- 【Qt】一个使用QEventLoop时,遇到的教训
- 22.CSS边框与背景【上】
- winForm调用HTTP短信接口
- 公有云账单:忽略这四项成本,后果很严重!
- Convolutional Neural Networks for Sentence Classification
- nginx 同一个IP上配置多个HTTPS主机
- 面向对象基础及UML建模语言
- LeetCode 124. Binary Tree Maximum Path Sum
- Android应用内嵌cocos2dx游戏项目
- C#仿win10计算器
- 纯CSS实现数据上报和HTML验证
- 全国计算机等级考试二级Python(2021年9月)备考笔记 第十五天
- 设备安全——防火墙j基础策略实验【华为NSP】
- IDEA一致卡在build时间过长问题处理
- 基于ZFAKA二次开发,添加PayJS支付渠道
- 选择短信平台请注意以下几点:
- linux 防火墙 超时时间,linux – TCP Keepalive和防火墙杀死空闲会话
- python 基础知识点 (一)
- osg学习(四十八)Windows Error #2000
热门文章
- 职称计算机在线模拟考试,2017职称计算机考试Windows模拟试题
- 使用psql运行.sql文件
- 导入.sql 文件 报 SP2-0310: 无法打开文件的问题解决
- Mysql 中 MUL,UNI,MUL 简单释义
- linux卸载metasploit,Linux安装Metasploit
- 任正非:过去3年,华为已完成1.3万个美国制裁器件的国产替代!(附:最新讲话实录)...
- 中国商业发展的方法论,都藏在叶茂中的广告中
- 洛谷1846 游戏 dp
- 应用层网络协议熟知端口号
- 移动端 芝麻信用评分接入实践详解