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

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

#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. HTTP 缓存简单了解

    HTTP 缓存简单了解.文章整理了相关资料,记录了部分实践.方便大家轻松了解缓存.能回答上三个问题,HTTP缓存就算理解呢.能否缓存?缓存是否过期?协商缓存? 概要: web缓存 缓存的处理 前端解决 ...

  2. 2019-11-29-dotnet-使用-Qpush-快速从电脑到手机推送文字

    title author date CreateTime categories dotnet 使用 Qpush 快速从电脑到手机推送文字 lindexi 2019-11-29 08:58:57 +08 ...

  3. centos7搭建docker+k8s集成

    1. 关闭防火墙 # systemctl stop firewalld # systemctl disable firewalld # setenforce 2. 使用yum安装etcd和kubern ...

  4. discuz论坛后台部分设置更改之后,清除了缓存网站前台不更新不生效的解决办法

    discuz论坛后台部分设置更改之后,清除了缓存但网站前台不更新不生效的解决办法 在config/config_global.php  把  $_config['memory']['eaccelera ...

  5. 什么是lease机制?

    分布式系统理论之租约机制学习 一,租约机制介绍 在分布式系统中,往往会有一个中心服务器节点.该节点负责存储.维护系统中的元数据.如果系统中的各种操作都依赖于中心服务器上的元数据,那么中心服务器很容易成 ...

  6. 如何用redis正确实现分布式锁?

    先把结论抛出来:redis无法正确实现分布式锁!即使是redis单节点也不行!redis的所谓分布式锁无法用在对锁要求严格的场景下,比如:同一个时间点只能有一个客户端获取锁. 首先来看下单节点下一般r ...

  7. big.js常用方法

    官网api:http://mikemcl.github.io/big.js/ +:minus.minus(n) ⇒ Big  %:mod.mod(n) ⇒ Big -:plus.plus(n) ⇒ B ...

  8. Vue框架使用sass

    引入: cnpm install node-sass --save-dev //安装node-sass cnpm install sass-loader@7.3.1 --save-dev cnpm i ...

  9. 【leetcode】1209. Remove All Adjacent Duplicates in String II

    题目如下: Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from ...

  10. mybatis config 配置设置说明

    <!– 配置设置 –> 2.           <settings> 3.               <!– 配置全局性 cache 的 ( 开 / 关) defau ...