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. Android 学习 (持续添加与更新)

    N.GitHub 最受欢迎的开源项目 http://www.csdn.net/article/2013-05-03/2815127-Android-open-source-projects 六.and ...

  2. 上海第三产业增加值 占比GDP首破七成

    上海第三产业增加值 占比GDP首破七成 2016年08月16日08:10  来源:新闻晨报 分享到:     不久前结束的ChinaJoy上,一家名为HYPEREAL的VR公司展台前,体验者的热情程度 ...

  3. Chrome快捷键, Mac 下 Chrome 浏览器 快捷键

    Chrome窗口和标签页快捷键:Ctrl+N 打开新窗口 Ctrl+T 打开新标签页 Ctrl+Shift+N 在隐身模式下打开新窗口 Ctrl+O,然后选择文件 在谷歌浏览器中打开计算机上的文件 按 ...

  4. samba初级使用记录

    首先安利一下什么是samba: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在 ...

  5. Spring框架使用ByName自动注入同名问题剖析

    问题描述   我们在使用spring框架进行项目开发的时候,为了配置Bean的方便经常会使用到Spring当中的Autosire机制,Autowire根据注入规则的不同又可以分为==ByName==和 ...

  6. elasticsearch 中文API 记数(八)

    计数API 计数API允许开发者简单的执行一个查询,返回和查询条件相匹配的文档的总数.它可以跨多个索引以及跨多个类型执行. import static org.elasticsearch.index. ...

  7. 嘴巴题7 BZOJ1426: 收集邮票

    Time Limit: 1 Sec Memory Limit: 162 MB Submit: 546 Solved: 455 [Submit][Status][Discuss] Description ...

  8. [Swoole系列入门教程 1] CentOs 上的Swoole安装

    如果已经用宝塔安装好了PHP, 开启相应的扩展 一.编译安装git clone https://github.com/swoole/swoole-src.git cd swoole-src/ phpi ...

  9. mysql复制关系

    一旦建立好主从复制关系后就不要在从库上执行任何dml和ddl操作,包括创建用户也不行. 那么万一在从库上执行了dml或者ddl操作了,会有何影响,以及如何恢复? slave同步状态中出现Slave_S ...

  10. Delphi 设计模式:《HeadFirst设计模式》Delphi代码---工厂模式之抽象工厂[转]

     1  2 {<HeadFirst设计模式>工厂模式之抽象工厂 }  3 { 抽象工厂的产品                       }  4 { 编译工具:Delphi7.0     ...