hdu6599 I Love Palindrome String
由样例可知,题目中求的回文串数量,其实是本质不同的回文串数量,这个可以直接用回文树来做。
考虑前半段是回文串这个限制,这个东西回文树不好做,可以再套一个马拉车,然后记录一下插入到回文树的节点中最后一个字符的位置,使用马拉车快速判断这一段的前半段是不是回文串
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <vector> #define fo(i, l, r) for (int i = l; i <= r; i++) #define fd(i, l, r) for (int i = r; i >= l; i--) #define mem(x) memset(x, 0, sizeof(x)) #define ll long long using namespace std; const int maxn = 300050; ll read() {ll x = 0, f = 1;char ch = getchar();while (!(ch >= '0' && ch <= '9')){if (ch == '-')f = -1;ch = getchar();};while (ch >= '0' && ch <= '9'){x = x * 10 + (ch - '0');ch = getchar();};return x * f; }char Ma[maxn * 2]; int Mp[maxn * 2]; void Manacher(char s[], int len) {int l = 0;Ma[l++] = '$';Ma[l++] = '#';for (int i = 0; i < len; i++){Ma[l++] = s[i];Ma[l++] = '#';}Ma[l] = 0;int mx = 0, id = 0;for (int i = 0; i < l; i++){Mp[i] = mx > i ? min(Mp[2 * id-i], mx-i) : 1;while (Ma[i + Mp[i]] == Ma[i-Mp[i]])Mp[i]++;if (i + Mp[i] > mx){mx = i + Mp[i];id = i;}} }const int N = 300010, S = 26; int all, son[N][S], fail[N], cnt[N], len[N], text[N], pos[N], last, tot; int newnode(int l) {for (int i = 0; i < S; i++)son[tot][i] = 0;cnt[tot] = 0, len[tot] = l;return tot++; } void init() {last = tot = all = 0;newnode(0), newnode(-1);text[0] = -1, fail[0] = 1; } int getfail(int x) {while (text[all - len[x] - 1] != text[all])x = fail[x];return x; } void add(int w,int p) {text[++all] = w;int x = getfail(last);if (!son[x][w]){int y = newnode(len[x] + 2);fail[y] = son[getfail(fail[x])][w];son[x][w] = y;pos[y] = p;}cnt[last = son[x][w]]++; } void count() {for (int i = tot - 1; ~i; i--)cnt[fail[i]] += cnt[i]; } char s[maxn]; int slen, s2[maxn]; ll ans[maxn]; int flag; inline bool check(int l,int r){int len = r-l+1;if(len==1)return true;r = (l+r)>>1;len = r-l+1;if(len&1){int t = l + (len>>1);t *= 2;return Mp[t]-1 >= len;}else{int t = l + (len >> 1);t = t*2-1;return Mp[t]-1 >= len;} } void dfs(int u, int d) {fo(i, 0, 25){if (son[u][i]){s2[d + 1] = i;dfs(son[u][i], d + 1);}}if (d){int lenu = d + d - flag;int rp = pos[u];int lp = pos[u]-lenu+1;if(check(lp,rp))ans[d + d - flag] += cnt[u];} } int main() {int tt = 0;int T;while (scanf("%s", s + 1) != EOF){init();memset(ans, 0, sizeof(ans));slen = strlen(s + 1);fo(i, 1, slen){add(s[i] - 'a',i);}count();Manacher(s+1,slen);flag = 0;dfs(0, 0);flag = 1;dfs(1, 0);printf("%lld", ans[1]);fo(i, 2, slen){printf(" %lld", ans[i]);}putchar('\n');}return 0; }
转载于:https://www.cnblogs.com/hyfer/p/11575134.html
hdu6599 I Love Palindrome String相关推荐
- 2019暑期杭电多校HDU6599 I Love Palindrome String
2019杭电多校HDU6599 I Love Palindrome String 题目链接 I Love Palindrome String Time Limit: 4000/2000 MS (Jav ...
- HDU-6599 I Love Palindrome String 杭电第二次多校赛(Manacher+回文自动机)
HDU-6599 I Love Palindrome String 杭电第二次多校赛(Manacher+回文自动机) 我的博客:https://acmerszq.cn 原题链接:http://acm. ...
- 「HDU6599 I Love Palindrome String」 - 回文自动机
HDU 6599 I Love Palindrome String tags:回文自动机 题意 让你求有多少个 \([l,r]\) 满足 \(s[l,r]\) 和 \(s\left[l,\frac{l ...
- [hdu6599]I Love Palindrome String
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 分数:回文自动机初见,2500 P ...
- HDU-6599 I Love Palindrome String(回文自动机+字符串hash)
题目链接 题意:给定一个字符串\(|S|\le 3\times 10^5\) 对于每个 \(i\in [1,|S|]\) 求有多少子串\(s_ls_{l+1}\cdots s_r\)满足下面条件 \( ...
- 【题解】HDU6599 I Love Palindrome String 回文自动机
补一下多校里碰到的字符串题. 来源:2019 HDU Multi-University Training Contest 2 - 09 给定一个字符串S,对于每个i,输出有多少个子串s[l,r]满足以 ...
- HDU6599 I Love Palindrome String(PAM)
链接 题意: 首先定义好的串:自身是回文串,且前一半也是回文串.前一半的定义为[l,(l+r)/2]除号为整除 给出一个字符串,求各种长度的好的串分别有几个 思路: 利用回文树求得各种不同本质的回文串 ...
- leetcode题库:5.最长回文子串Longest Palindrome string
题目描述: /** 题目地址:https://leetcode-cn.com/problems/longest-palindromic-substring/description/ * 题目:最长回 ...
- HDU 6599:I Love Palindrome String Manacher+回文自动机
题意 Problem Description You are given a string S=s1s2-s|S| containing only lowercase English letters. ...
最新文章
- kettle连接不上es7_kettle8.2连接ElasticSearch7
- argz_create_sep函数
- ( )不是html的布局标签,不要使用的HTML标签(WEB标准网页布局)
- this指针_C++:07this指针
- jgit_JGit身份验证说明
- 我要带徒弟学写JAVA架构,引路架构师之路(Jeecg开源社区)
- ORACLE(Linux版本)实时同步数据到MYSQL(Linux版本)解决方案:OGG
- ecshop退出登录会清空购物车的bug优化,最完美解决方法
- [转] WPF TextBox控件中文字实现垂直居中
- Java学到什么程度可以找工作?
- JAVA图像相似度识别器
- matlab 数字波束合成,dbf数字波束合成
- CAN应用层常用协议
- mysql 1698 错误
- 在百度实习的100天
- doctrine2 mysql_doctrine2到底是个什么玩意
- php怎么实现拼图功能,照片拼图效果怎么做 将一张照片制作成拼图的效果
- 模拟信号数字化的过程(一)——采样和量化
- 每台计算机的ip地址解析,ip地址是什么?【详解】
- EasyGBS国标视频云服务平台可以获取录像却无法播放是什么原因?