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 = ;
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的更多相关文章
- [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]也满足 ...
- HDU-6599 I Love Palindrome String(回文自动机+字符串hash)
题目链接 题意:给定一个字符串\(|S|\le 3\times 10^5\) 对于每个 \(i\in [1,|S|]\) 求有多少子串\(s_ls_{l+1}\cdots s_r\)满足下面条件 \( ...
- I Love Palindrome String
I Love Palindrome String 时间限制: 2 Sec 内存限制: 128 MB 题目描述 You are given a string S=s1s2..s|S| containi ...
- 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 ...
- hdu多校第二场1009 (hdu6599) I Love Palindrome String 回文自动机/字符串hash
题意: 找出这样的回文子串的个数:它本身是一个回文串,它的前一半也是一个回文串 输出格式要求输出l个数字,分别代表长度为1~l的这样的回文串的个数 题解: (回文自动机和回文树是一个东西) 首先用回文 ...
- I Love Palindrome String HDU - 6599 回文树+hash
题意: 输出每个长度下的回文串(这些回文串的左半边也需要是回文串) 题解: 直接套上回文树,然后每找到一个回文串就将他hash. 因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半 ...
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- 杭电多校HDU 6599 I Love Palindrome String (回文树)题解
题意: 定义一个串为\(super\)回文串为: \(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\) \(\bullet\ ...
- 【HDOJ6599】I Love Palindrome String(PAM,manacher)
题意:给出一个由小写字母组成的长为n的字符串S,定义他的子串[L,R]为周驿东串当且仅当[L,R]为回文串且[L,(L+R)/2]为回文串 求i=[1,n] 所有长度为i的周驿东串的个数 n<= ...
随机推荐
- JavaScript中实现li向上轮播
在网上找了很久,没有找到合适的模板,其实我这个也是公司用的,希望以后也能复用,节省时间 function scrollAuto(scrollBox, list){//两个参数分别填列表的ul的clas ...
- Java Script语法
JavaScript 语法 JavaScript 是一个程序语言.语法规则定义了语言结构. JavaScript 语法 JavaScript 是一个脚本语言. 它是一个轻量级,但功能强大的编程语言. ...
- 一、Ubuntu16.04 安装
第一步:系统安装 https://yuedu.baidu.com/ebook/c44183ed4128915f804d2b160b4e767f5acf80fb?pn=1&rf=https%3A ...
- SpringMVC 向前台页面传值-ModelAndView
ModelAndView 该对象中包含了一个model属性和一个view属性 model:其实是一个ModelMap类型.其实ModelMap是一个LinkedHashMap的子类 view:包含了一 ...
- Python修炼之路-文件操作
Python编程之文件操作 文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 每次文件打开.读取信息时,Python自动记录所达到的位置,好比一个书签,之后每一次 ...
- Vue优化首页加载速度 CDN引入
https://blog.csdn.net/blueberry_liang/article/details/80134563
- 设计模式-Interpreter(行为模式) 使用解释器给用户提供一个一门定义语言的语法表示的解释器,通过该解释器解释语言中的句子。
//以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Context.h #pragma once class Context { public: Context(); ~ ...
- 【leetcode】LCP 3. Programmable Robot
题目如下: 力扣团队买了一个可编程机器人,机器人初始位置在原点(0, 0).小伙伴事先给机器人输入一串指令command,机器人就会无限循环这条指令的步骤进行移动.指令有两种: U: 向y轴正方向移动 ...
- JS定时循环
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 解析binlog生成MySQL回滚脚本
如果数据库误操作想恢复数据.可以试试下面这个脚本.前提是执行DML操作. #!/bin/env python #coding:utf-8 #Author: Hogan #Descript : 解析bi ...