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. WPF时间长度自定义选择控件TimeSpanBox

    以下控件采用https://www.cnblogs.com/cssmystyle/archive/2011/01/17/1937361.html部分代码 以下控件采用https://www.cnblo ...

  2. Server 2012 R2 Standard 安装运行PCS7时出现“无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll”解决方法

    网上看到了这篇文章https://www.jianshu.com/p/21f4bb8b5502,根据思路自己尝试,解决了丢失的问题.提示[计算机中丢失api-ms-win-crt-runtime-l1 ...

  3. 【JS学习】for-in与for-of

    前言:本博客系列为学习后盾人js教程过程中的记录与产出,如果对你有帮助,欢迎关注,点赞,分享.不足之处也欢迎指正,作者会积极思考与改正. 总述: 名称 遍历 适用 for-in 索引 主要建议白能力对 ...

  4. 浅谈Go中的time.After

    go的一条哲学是 不要通过共享来实现通信,而是通信来实现共享 多协程之间通过 channel 来实现通信,而普遍会遇到的问题是,如何进行超时控制,资料一查询,需要配置select和time.After ...

  5. .NET 云原生架构师训练营(模块二 基础巩固 Scrum 核心)--学习笔记

    2.7.2 Scrum 核心 3个工件 5个会议 5个价值观 3个工件 产品待办列表(Product Backlog) Sprint 待办列表(Sprint Backlog) 产品增量(Product ...

  6. 计算起始车站车费问题-JavaScript数组对象写法

    计算起始站车费 题目:深圳--60--广州--50-虎门--40- -中山--36-珠海一34-澳门一89一香港以上车票费用计算,如坐车深圳到广州60元,广州到虎门50元,深圳到虎门就是60+50-1 ...

  7. 【Linux】使用 iperf 测试 Linux 服务器带宽

    iperf 简介 iperf 是一个用于测试网络带宽的命令行工具,可以测试服务器的网络吞吐量.目前发现两个很实用的功能: 测试服务器网络吞吐量:如果我们需要知道某台服务器的「最大」网络带宽,那么最好在 ...

  8. 【Linux】postfix大坑笔记

    由于需要,想弄一个自动发送邮件的mailx或者sendmail 但是执行 echo "test" | mail -s "Worning mail !" xxxx ...

  9. Python-Flask搭建Web项目

    最近因项目需要,学习了用flask搭建web项目,以下是自己的使用感悟 Flask框架结构 static:存储一些静态资源 templates:存储对应的view app.py:涉及到页面的跳转,以及 ...

  10. Ajax中的同源政策

    Ajax中的同源政策 Ajax请求限制 Ajax只能向自己的服务器发送请求.比如现在有一个A网站.有一个B网站,A网站中的HTML文件只能向A网站服务器中发送Ajax请求,B网站中的HTML文件只能向 ...