Problem Description
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...sr satisfy 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. 
 
Input
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.
 
Output
For each test case, output one line containing |S| integers. Any two adjacent integers are separated by a space.
 
Sample Input
abababa
 
Sample Output
7 0 0 0 3 0 0
 
题意:给你一个字符串 要你输出长度分别为1~len的good回文串的个数(good回文串定义为 当前是回文串 且他的一半也是回文串)
思路:我们首先用回文自动机把回文串都记录下来 然后字符串hash判断是否两半是否相等
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
const int inf = 0x3f3f3f3f;
const int N = 4e5+7;
const ll mod = 998244353;
using namespace std;
ull hash1=233;
ull ha[N],pp[N];
ull getha(int l,int r){
if(l==0) return ha[r];
return ha[r]-ha[l-1]*pp[r-l+1];
}
struct Palindromic_Tree{
int next[N][30]; //节点之间连边
int fail[N]; //适配指针 表示当前回文串的最长回文后缀
int len[N]; //当前回文串的长度
int cnt[N]; //回文串的个数
int id[N]; //回文串的右端点
int S[N];
int last,n,p;
int newnode(int l){//新建节点
for(int i=0;i<26;i++) next[p][i]=0;//新建的节点为p,先消除它的子节点
cnt[p]=0;
len[p]=l;
return p++;//勿打成++p,因为此节点为p,我们应返回p
}
void init(){
last=n=p=0;
newnode(0); newnode(-1);
S[0]=-1; fail[0]=1;
}
int get_fail(int x){
while(S[n-len[x]-1]!=S[n]) x=fail[x];
return x;
}
void add(int c){
c-='a';
S[++n]=c;
int po=get_fail(last);
if(!next[po][c]){
int now=newnode(len[po]+2);
fail[now]=next[get_fail(fail[po])][c];
next[po][c]=now;
}
last=next[po][c];
cnt[last]++;
id[last]=n;
}
void count(){
for(int i=p-1;i>=0;i--)
cnt[fail[i]]+=cnt[i];
}
}pat;
ll ans[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
pp[0]=1;
for(int i=1;i<N;i++) {
pp[i]=hash1*pp[i-1];
}
string s;
while(cin>>s){
memset(ans,0,sizeof(ans));
pat.init();
int len=s.length();
for(int i=0;i<len;i++){
pat.add(s[i]);
}
ha[0]=s[0];
for(int i=1;i<len;i++)
ha[i]=ha[i-1]*hash1+s[i];
pat.count();
for(int i=2;i<pat.p;i++){
int l=pat.id[i]-pat.len[i];
int r=pat.id[i]-1;
int mid=(l+r)>>1;
if((r-l+1)&1){
if(getha(l,mid)==getha(mid,r)){
ans[pat.len[i]]+=pat.cnt[i];
}
}else{
if(getha(l,mid)==getha(mid+1,r)){
ans[pat.len[i]]+=pat.cnt[i];
}
}
}
for(int i=1;i<=len;i++){
if(i==1) cout<<ans[i];
else cout<<" "<<ans[i];
}
cout<<endl;
}
return 0;
}

2019 Multi-University Training Contest 2 I.I Love Palindrome String(回文自动机+字符串hash)的更多相关文章

  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. 2019牛客暑期多校训练营(第六场)C - Palindrome Mouse (回文自动机)

    https://ac.nowcoder.com/acm/contest/886/C 题意: 给出一个串A , 集合S里面为A串的回文字串 , 现在在集合S里面找出多少对(a,b),b为a的字串 分析: ...

  3. 2015 Multi-University Training Contest 6 hdu 5362 Just A String

    Just A String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  4. 2019 Nowcoder Multi-University Training Contest 4 E Explorer

    线段树分治. 把size看成时间,相当于时间 $l$ 加入这条边,时间 $r+1$ 删除这条边. 注意把左右端点的关系. #include <bits/stdc++.h> ; int X[ ...

  5. 2019 Nowcoder Multi-University Training Contest 1 H-XOR

    由于每个元素贡献是线性的,那么等价于求每个元素出现在多少个异或和为$0$的子集内.因为是任意元素可以去异或,那么自然想到线性基.先对整个集合A求一遍线性基,设为$R$,假设$R$中元素个数为$r$,那 ...

  6. 2019 Multi-University Training Contest 2 - 1009 - 回文自动机

    http://acm.hdu.edu.cn/showproblem.php?pid=6599 有好几种实现方式,首先都是用回文自动机统计好回文串的个数. 记得把每个节点的cnt加到他的fail上,因为 ...

  7. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  8. The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)

    这题建立一棵回文树,然后用dfs搜索答案,但是有一点需要注意,就是打vis的标记时,如果标记为1,那么在好几个节点都对同一个字符i打过标记,此时的搜索从字符i点回溯,回到它的父亲节点,搜索其它的字符, ...

  9. 回文自动机 + DFS --- The 2014 ACM-ICPC Asia Xi’an Regional Contest Problem G.The Problem to Slow Down You

    The Problem to Slow Down You Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.actio ...

随机推荐

  1. Databricks 第6篇:Spark SQL 维护数据库和表

    Spark SQL 表的命名方式是db_name.table_name,只有数据库名称和数据表名称.如果没有指定db_name而直接引用table_name,实际上是引用default 数据库下的表. ...

  2. 【C++】《C++ Primer 》第三章

    第三章 字符串.向量和数组 一.命名空间的using声明 使用某个命名空间:例如 using std::cin表示使用命名空间std中的名字cin. 头文件的代码一般不应该使用using声明,这是因为 ...

  3. 如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

    如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

  4. 超精讲-逐例分析CS:LAB2-Bomb!(上)

    0. 环境要求 关于环境已经在lab1里配置过了这里要记得安装gdb 安装命令 sudo yum install gdb 实验的下载地址 http://csapp.cs.cmu.edu/3e/labs ...

  5. Vijos-P1103题解【线段树】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: https://www.vijos.org/p/1103 题目描述: 一条马路从数轴0到L,每 ...

  6. Markdown里常用的HTML元素

    转义:\ 换行:<br/> 红色文字:<font color=#FF0000>字体改成红色了</font> A标签 新窗口:<a href="xxx ...

  7. 六个你不知道的PR快捷键,拯救你的剪辑效率

    5G时代到来,会剪辑视频的人,无论在校园还是未来步入职场都很吃香.对于普通人来说,视频处理也成为了一个通用技能.PR是我们大多数人剪辑中,经常会用到的剪辑工具,之前的文章中已经给大家总结了pr的一些提 ...

  8. apk开发环境!多亏这份《秋招+金九银十-腾讯面试题合集》跳槽薪资翻倍!再不刷题就晚了!

    开头 最近很多网友反馈:自己从各处弄来的资料,过于杂乱.零散.碎片化,看得时候觉得挺有用的,但过个半天,啥都记不起来了.其实,这就是缺少系统化学习的后果. 为了提高大家的学习效率,帮大家能快速掌握An ...

  9. 日志采集技术分析 Inode Inotify

    日志采集技术分析[阿里] - 新手学习导向 - 中国红客联盟 - Powered by HUC http://www.cnhonkerarmy.com/thread-236973-1-1.html

  10. (转载)微软数据挖掘算法:Microsoft 神经网络分析算法原理篇(9)

    前言 本篇文章继续我们的微软挖掘系列算法总结,前几篇文章已经将相关的主要算法做了详细的介绍,我为了展示方便,特地的整理了一个目录提纲篇:大数据时代:深入浅出微软数据挖掘算法总结连载,有兴趣的童鞋可以点 ...