由样例可知,题目中求的回文串数量,其实是本质不同的回文串数量,这个可以直接用回文树来做。

考虑前半段是回文串这个限制,这个东西回文树不好做,可以再套一个马拉车,然后记录一下插入到回文树的节点中最后一个字符的位置,使用马拉车快速判断这一段的前半段是不是回文串

#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 = ;
ll read()
{
ll x = , f = ;
char ch = getchar();
while (!(ch >= '' && ch <= ''))
{
if (ch == '-')
f = -;
ch = getchar();
};
while (ch >= '' && ch <= '')
{
x = x * + (ch - '');
ch = getchar();
};
return x * f;
} char Ma[maxn * ];
int Mp[maxn * ];
void Manacher(char s[], int len)
{
int l = ;
Ma[l++] = '$';
Ma[l++] = '#';
for (int i = ; i < len; i++)
{
Ma[l++] = s[i];
Ma[l++] = '#';
}
Ma[l] = ;
int mx = , id = ;
for (int i = ; i < l; i++)
{
Mp[i] = mx > i ? min(Mp[ * id-i], mx-i) : ;
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 = , S = ;
int all, son[N][S], fail[N], cnt[N], len[N], text[N], pos[N], last, tot;
int newnode(int l)
{
for (int i = ; i < S; i++)
son[tot][i] = ;
cnt[tot] = , len[tot] = l;
return tot++;
}
void init()
{
last = tot = all = ;
newnode(), newnode(-);
text[] = -, fail[] = ;
}
int getfail(int x)
{
while (text[all - len[x] - ] != 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] + );
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 - ; ~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+;
if(len==)return true;
r = (l+r)>>;
len = r-l+;
if(len&){
int t = l + (len>>);
t *= ;
return Mp[t]- >= len;
}else{
int t = l + (len >> );
t = t*-;
return Mp[t]- >= len;
}
}
void dfs(int u, int d)
{
fo(i, , )
{
if (son[u][i])
{
s2[d + ] = i;
dfs(son[u][i], d + );
}
}
if (d)
{
int lenu = d + d - flag;
int rp = pos[u];
int lp = pos[u]-lenu+;
if(check(lp,rp))ans[d + d - flag] += cnt[u];
}
}
int main()
{
int tt = ;
int T;
while (scanf("%s", s + ) != EOF)
{
init();
memset(ans, , sizeof(ans));
slen = strlen(s + );
fo(i, , slen)
{
add(s[i] - 'a',i);
}
count();
Manacher(s+,slen);
flag = ;
dfs(, );
flag = ;
dfs(, );
printf("%lld", ans[]);
fo(i, , slen)
{
printf(" %lld", ans[i]);
}
putchar('\n');
}
return ;
}

hdu6599 I Love Palindrome String的更多相关文章

  1. [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]也满足 ...

  2. HDU-6599 I Love Palindrome String(回文自动机+字符串hash)

    题目链接 题意:给定一个字符串\(|S|\le 3\times 10^5\) 对于每个 \(i\in [1,|S|]\) 求有多少子串\(s_ls_{l+1}\cdots s_r\)满足下面条件 \( ...

  3. I Love Palindrome String

    I Love Palindrome String 时间限制: 2 Sec  内存限制: 128 MB 题目描述 You are given a string S=s1s2..s|S| containi ...

  4. 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 ...

  5. hdu多校第二场1009 (hdu6599) I Love Palindrome String 回文自动机/字符串hash

    题意: 找出这样的回文子串的个数:它本身是一个回文串,它的前一半也是一个回文串 输出格式要求输出l个数字,分别代表长度为1~l的这样的回文串的个数 题解: (回文自动机和回文树是一个东西) 首先用回文 ...

  6. I Love Palindrome String HDU - 6599 回文树+hash

    题意: 输出每个长度下的回文串(这些回文串的左半边也需要是回文串) 题解: 直接套上回文树,然后每找到一个回文串就将他hash. 因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半 ...

  7. HDU 6599 I Love Palindrome String (回文树+hash)

    题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...

  8. 杭电多校HDU 6599 I Love Palindrome String (回文树)题解

    题意: 定义一个串为\(super\)回文串为: \(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\) \(\bullet\ ...

  9. 【HDOJ6599】I Love Palindrome String(PAM,manacher)

    题意:给出一个由小写字母组成的长为n的字符串S,定义他的子串[L,R]为周驿东串当且仅当[L,R]为回文串且[L,(L+R)/2]为回文串 求i=[1,n] 所有长度为i的周驿东串的个数 n<= ...

随机推荐

  1. css3之新增伪类

    css3新增了许多伪类,但是IE8以及更低版本的IE浏览器不支持css3伪类,所以在使用时要是涉及到布局等意象全局的样式,应该多考虑一下. 1.elem:nth-child(n) 这个伪类选中父元素下 ...

  2. ms17_0199样本测试

    一大早就各种消息弹框,于是就来测试一波 https://github.com/nixawk/metasploit-framework/blob/8ab0b448fdce15999f155dfd7b22 ...

  3. Linux--磁盘管理--04

    1.磁盘的工作原理: 磁道.磁头.扇区.柱面 2.磁盘分类: 机械盘: 串行:SCSI.iSCSI.SATA 并行:ATA 固态盘:HDD 3.文件系统: Windows :fat32  ntfs  ...

  4. sql 占位符及部分时间函数

    Mysql 预处理占位符 %s -- 表示字段串 %d -- 表示整形数字 %f -- 表示浮点数 (UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL jp_days D ...

  5. IDEA部署Spring-boot到Docker容器

    一.准备工作 idea安装Docker插件 centos7系统安装docker 二.打开docker远程端口 1. 编辑docker.service文件 vim /usr/lib/systemd/sy ...

  6. 一个web应用的诞生(6)

    之前登录注册的功能都已经完成,但是登录成功回到首页发现还是白茫茫的一片,对的,title一直都写得博客,那么最终目的也是写出一个轻博客来,但是,在发表文章之前是不是要先记录一下登录状态呢? 用户登录 ...

  7. web性能优化--算法优化(四)

    避免for-in 把数组长度保存在局部变量中 较少迭代次数(Duffs Device) 基于函数的循环比基于循环的迭代消耗性能更多 优化if-else,一般switch比if-else速度快(hash ...

  8. 在Rails中最方便集成使用Bootstrap的方式

    创建项目 rails new BootstrapProject 创建模型 rails g scaffold xxx --skip-stylesheets 运行迁移 rake db:migrate -- ...

  9. 【NOIP2016提高A组模拟10.15】算循环

    题目 分析 一步步删掉循环, 首先,原式是\[\sum_{i=1}^n\sum_{j=1}^m\sum_{k=i}^n\sum_{l=j}^m\sum_{p=i}^k\sum_{q=j}^l1\] 删 ...

  10. jsp三种注释方法

    HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...