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. SSM项目实现连接两个mysql数据库

    最近做项目需要用到另一个数据库的内容,多方查找终于实现了功能. 我们都知道,在SSM框架中,我们在applicationContext.xml配置文件中添加数据源就可以实现数据库增删改查,但是只能连接 ...

  2. 2016年深圳市服务业占GDP比重首次突破六成

    2016年深圳市服务业占GDP比重首次突破六成 中商产业研究院 中商情报网 2017-01-12 11:08 分享:     中商情报网讯 1月10日,深圳市财政委员会召开新闻发布会,就深圳市2016 ...

  3. 菜鸟nginx源码剖析数据结构篇(十) 自旋锁ngx_spinlock[转]

    菜鸟nginx源码剖析数据结构篇(十) 自旋锁ngx_spinlock Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...

  4. php面向对象的初认识

    面向对象的基本概念 面向对象的三大特征:继承 封装 多态 类和对象: 类是一个抽象的概念 对象是一个具体的实例 张三是一个对象,李四也是一个对象.王五同样是一个对象..... 他们都隶属于“人”这个“ ...

  5. PAT甲级——A1003Emergency

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  6. OSG在VS2008下的配置安装

    一.准备工作 下载相关的工具软件: 1, 最新版的OSG库:OpenSceneGraph-2.8.2.zip. 2, 安装源代码所需要的工具:cmake-2.6.4-win32-x86.zip 3,  ...

  7. JS倒计时两种种实现方式 很不错

    最近做浏览器界面倒计时,用js就实现,两种方式: 一:设置时长,进行倒计时.比如考试时间等等 代码如下: <html> <head> <meta charset=&quo ...

  8. Windows API 第17篇 GetLogicalDriveStrings 获取本机所有逻辑驱动器,以根目录的形式表示

    函数原型:DWORD GetLogicalDriveStrings(  DWORD nBufferLength,  // size of buffer                          ...

  9. Windows API 第六篇 GetLocalTime

    GetLocalTime获取系统时间信息.函数原型:VOID   WINAPI  GetLocalTime(    __out LPSYSTEMTIME lpSystemTime    ); 先来看S ...

  10. ODOO 新API修饰符

    Odoo8中,API接口分为traditaional style和record style两种类型: traditional style指的就是我们在7.0中使用的类型,def(self,cr,uid ...