I Love Palindrome String

时间限制: 2 Sec  内存限制: 128 MB

题目描述

You are given a string S=s1s2..s|S| containing only lowercase English letters. For each integer i∈[1,|S|] , please output how many substrings slsl+1...srsatisfy the following conditions:
 ∙ r−l+1 equals to i.
 ∙ The substring slsl+1...sr is a palindrome string.
 ∙ slsl+1...s⌊(l+r)/2⌋ is a palindrome string too.
|S| denotes the length of string S.
A palindrome string is a sequence of characters which reads the same backward as forward, such as madam or racecar or abba. 

输入

There are multiple test cases.
Each case starts with a line containing a string S(1≤|S|≤3×105) containing only lowercase English letters.
It is guaranteed that the sum of |S| in all test cases is no larger than 4×106.

输出

For each test case, output one line containing |S| integers. Any two adjacent integers are separated by a space.

样例输入

abababa

样例输出

7 0 0 0 3 0 0

题意:求有多少个字符串为回文串,且它的前一半也是回文串。
回文树+马拉车。先用回文树求出全部的本质不一样(就是长度不一样,或者长度一样内容不一样)的字符串,然后用马拉车快速判断该串是不是符合题意。
#include<bits/stdc++.h>
using namespace std;
//https://www.xuebuyuan.com/3184341.html
const int MAXN = ;
const int N = ; int Palindromic_Length[MAXN*];
int ans[MAXN]; struct Palindromic_Tree
{
int nxt[MAXN][],f[MAXN],cnt[MAXN],num[MAXN],len[MAXN],c[MAXN],last,n,L; int l[MAXN],r[MAXN]; inline int newnode(int x)
{
for(register int i=; i<; ++i)
nxt[L][i]=;
cnt[L]=;
len[L]=x;
return L++;
}
void init()
{
L=;
newnode();
newnode(-);
last=;
n=;
c[n]=-;
f[]=;
}
inline int getf(int x)
{
while(c[n-len[x]-]!=c[n])
x=f[x];
return x;
}
inline void add(int x)
{
x-='a';
c[++n]=x;
int cur=getf(last);
if(!nxt[cur][x])
{
int now=newnode(len[cur]+); l[now]=n-len[cur]-;
r[now]=n; f[now]=nxt[getf(f[cur])][x];
nxt[cur][x]=now;
}
++cnt[last=nxt[cur][x]];
}
void count()
{
for(register int i=L-; i>=; --i)
cnt[f[i]]+=cnt[i];
} void getans()
{
count();
for(int i=;i<L;i++)
{
int l1=l[i],r1=r[i];
r1=(l1+r1)/; r1*=;
l1*=; int mid=(r1+l1)/; if(mid-Palindromic_Length[mid]+<=(l1))
{
ans[len[i]]+=cnt[i];
}
}
} } PT; string Manacher(string s)
{
/*改造字符串*/
string res="$#";
for(int i=; i<s.size(); ++i)
{
res+=s[i];
res+="#";
} /*数组*/
vector<int> P(res.size(),);
int mi=,right=; //mi为最大回文串对应的中心点,right为该回文串能达到的最右端的值
int maxLen=,maxPoint=; //maxLen为最大回文串的长度,maxPoint为记录中心点 for(int i=; i<res.size(); ++i)
{
P[i]=right>i ?min(P[*mi-i],right-i):; //关键句,文中对这句以详细讲解 while(res[i+P[i]]==res[i-P[i]])
++P[i]; if(right<i+P[i]) //超过之前的最右端,则改变中心点和对应的最右端
{
right=i+P[i];
mi=i;
} if(maxLen<P[i]) //更新最大回文串的长度,并记下此时的点
{
maxLen=P[i];
maxPoint=i;
}
Palindromic_Length[i]=P[i]-; // printf("%d %d %c\n",i,P[i]-1,res[i]); }
return s.substr((maxPoint-maxLen)/,maxLen-);
} Palindromic_Tree tree;
int main()
{
char s[MAXN]; while(scanf("%s",s)==)
{
string a(s);
tree.init(); int len=strlen(s);
for(int i=;i<len;i++)tree.add(s[i]),ans[i+]=;
Manacher(a);
tree.getans();
for(int i=;i<=len;i++)printf("%d%c",ans[i],i==len ? '\n' : ' ');
}
//for(int i=0;i<a.size();i++)tree.add(a[i]);
return ; }

I Love Palindrome String的更多相关文章

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

  2. hdu6599 I Love Palindrome String

    由样例可知,题目中求的回文串数量,其实是本质不同的回文串数量,这个可以直接用回文树来做. 考虑前半段是回文串这个限制,这个东西回文树不好做,可以再套一个马拉车,然后记录一下插入到回文树的节点中最后一个 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t[转]

    菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...

  2. javaweb的几种开发模式

    1.MVC模式基础 1.1.MVC模式简介 MVC是一种架构型模式,它本身并不引入新的功能,只是用来指导我们改善应用程序的架构,使得应用的模型和视图相分离,从而达到更好的开发和维护效率.在MVC模式中 ...

  3. hibernate抓取策略

    抓取策略(fetching strategy) 是指:当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候, Hibernate如何获取关联对象的策略.抓取策略可以在O/R映射的 ...

  4. 06_jQuery对象初识(四)文档处理

    1. 案例:在ul中添加li标签. append在最后添加 prepend在最前面添加 <ul id="ul"> <li>1</li> < ...

  5. <数据链接>常用网站收集

    1.互联网数据指数 百度指数:http://index.baidu.com/ 阿里指数:http://index.1688.com/ TBI腾讯浏览指数:http://tbi.tencent.com/ ...

  6. hdfs写并发问题

    hdfs文件写入不支持多个进程同时写入一个文件,每次只能一个FS挟持对象的人写入

  7. Find- Linux必学的60个命令

    1.作用 find命令的作用是在目录中搜索文件,它的使用权限是所有用户. 2.格式 find [path][options][expression] path指定目录路径,系统从这里开始沿着目录树向下 ...

  8. mysql三表联合查询,结果集合并

    参考: mysql 结果集去重复值并合并成一行 SQL 三表联查 数据库三表连接查询怎么做 合并: MySQL中group_concat函数 完整的语法如下: group_concat([DISTIN ...

  9. 跟我一起学koa之在koa中使用mongoose(四)

    第一步安装mongoose,创建数据库文件夹 第二步引入mongoose,连接数据库 第三步运行项目 这个报错 只需要将es6写法变成es5写法即可 我们连接数据库,并且以post请求的方式插入数据 ...

  10. PHP--自动回调接口,分批修改数据

    /** * 修复 a表 生日格式问题 * @author qin */ public function update_birthday_one() { $this->load->model ...