Codechef Palindromeness 和 SHOI2011 双倍回文
Palindromeness
Let us define the palindromeness of a string in the following way:
- If the string is not a palindrome, its' palindromeness is zero.
- The palindromeness of an one-letter string is 1.
- The palindromness of a string S of the length greater than one is 1 + "palindromeness of the string that is formed by the first [|S|/2] symbols of S".
You are given a string S. Find the sum of the palindromenesses of all the non empty substrings of S (i.e. S[i..j], where i <= j). In other words, you have to calculate the sum of palindromenesses of N * (N + 1) / 2 substrings of S, where N is the length of S.
- 1 ≤ |S| ≤ 105。
题解
翁文涛《回文树及其应用》。

\(len_i=1\) 时特殊处理 \(half_i=even\)。
CO int N=100000+10;
namespace PAM{
int str[N],n;
int last,tot;
int ch[N][26],fa[N],len[N];
int half[N],val[N],siz[N];
void init(){
memset(str,-1,sizeof str),n=0;
last=tot=1;
memset(ch,0,sizeof ch);
fa[0]=fa[1]=1,len[0]=0,len[1]=-1;
memset(siz,0,sizeof siz);
}
int get_fail(int x){
while(str[n-len[x]-1]!=str[n]) x=fa[x];
return x;
}
void extend(int c){
int p=get_fail(last);
if(!ch[p][c]){
int cur=++tot;
len[cur]=len[p]+2;
fa[cur]=ch[get_fail(fa[p])][c];
ch[p][c]=cur;
if(len[cur]==1) half[cur]=0;
else{
int q=half[p];
while(str[n-len[q]-1]!=str[n] or
2*(len[q]+2)>len[cur]) q=fa[q];
half[cur]=ch[q][c];
}
val[cur]=1+(len[cur]/2==len[half[cur]]?val[half[cur]]:0);
}
last=ch[p][c];
++siz[last];
}
LL main(){
for(int i=tot;i>=2;--i) siz[fa[i]]+=siz[i];
LL ans=0;
for(int i=tot;i>=2;--i) ans+=(LL)siz[i]*val[i];
return ans;
}
}
char str[N];
void real_main(){
scanf("%s",str+1);
int n=strlen(str+1);
PAM::init();
for(int i=1;i<=n;++i){
PAM::str[++PAM::n]=str[i]-'a';
PAM::extend(str[i]-'a');
}
printf("%lld\n",PAM::main());
}
int main(){
for(int T=read<int>();T--;) real_main();
return 0;
}
双倍回文
如果 s 能够写成 wwRwwR 的形式,则称 s 是双倍回文。
s 的长度是 4 的倍数,前后两半都是相同的回文。
对于给定的字符串,计算它的最长双倍回文串的长度。
N<=500000
yyb的题解
发现这个长度为自身一半的的回文串是可以方便地转移的。
对于每个节点,我们维护一个half来表示长度最长的、不超过它长度一半的那个祖先节点
这样子只需要判断一下当前点的half长度是否是一半,并且当前串的长度是四的倍数就好了
找 half 就用 p 的 half 来跳就行了。
co int N=500000+10;
char s[N];
int last=1,tot=1;
int ch[N][26],fa[N]={1,1},len[N]={0,-1},half[N];
int get_fa(int x,int i){
while(s[i-len[x]-1]!=s[i]) x=fa[x];
return x;
}
void extend(int i){
int p=get_fa(last,i);
int x=ch[p][s[i]-'a'];
if(!x){
x=++tot;
fa[x]=ch[get_fa(fa[p],i)][s[i]-'a'];
len[x]=len[p]+2;
ch[p][s[i]-'a']=x;
if(len[x]==1) half[x]=0;
else{
int q=half[p];
while(s[i-len[q]-1]!=s[i]||(len[q]+2)<<1>len[x]) q=fa[q];
half[x]=ch[q][s[i]-'a'];
}
}
last=x;
}
int main(){
int n=read<int>();
scanf("%s",s+1);
for(int i=1;i<=n;++i) extend(i);
int ans=0;
for(int i=1;i<=tot;++i)
if(len[half[i]]<<1==len[i]&&len[i]%4==0)
ans=max(ans,len[i]);
printf("%d\n",ans);
return 0;
}
Codechef Palindromeness 和 SHOI2011 双倍回文的更多相关文章
- BZOJ2342: [Shoi2011]双倍回文
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 923 Solved: 317[Submit][Status ...
- 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...
- BZOJ 2342: [Shoi2011]双倍回文 马拉车算法/并查集
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1123 Solved: 408 题目连接 http://w ...
- [SHOI2011]双倍回文 manacher
题面: 洛谷:[SHOI2011]双倍回文‘ 题解: 首先有一个性质,本质不同的回文串最多O(n)个. 所以我们可以对于每个i,求出以这个i为结尾的最长回文串,然后以此作为长串,并判断把这个长串从中间 ...
- bzoj 2342: [Shoi2011]双倍回文 -- manacher
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符 ...
- BZOJ2342 Shoi2011 双倍回文 【Manacher】
BZOJ2342 Shoi2011 双倍回文 Description Input 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. Output 输 ...
- Manacher || BZOJ 2342: [Shoi2011]双倍回文 || Luogu P4287 [SHOI2011]双倍回文
题面:[SHOI2011]双倍回文 题解:具体实现时,就是在更新mr时维护前半段是回文串的最长回文串就好了 正确性的话,因为到i时如果i+RL[i]-1<=mr,那么答案肯定在i之前就维护过了: ...
- [SHOI2011]双倍回文
Description Input 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. Output 输出文件只有一行,即:输入数据中字符串的最长 ...
- BZOJ2342[Shoi2011]双倍回文——回文自动机
题目描述 输入 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. 输出 输出文件只有一行,即:输入数据中字符串的最长双倍回文子串的长度,如果双倍回文 ...
随机推荐
- 【C/C++开发】c++ 工具库 (zz)
下面是收集的一些开发工具包,主要是C/C++方面的,涉及图形.图像.游戏.人工智能等各个方面,感觉是一个比较全的资源.供参考! 原文的出处:http://www.codemonsters.de/ho ...
- linux 手动源码安装lnmp(亲测)
linux 手动源码安装lnmp笔记(亲测)<pre>先安装这2个yum install gccyum install g++</pre><pre>先在linux ...
- kafka参数解析+启动参数解析
Kafka参数详解 每个kafka broker中配置文件server.properties默认必须配置的属性如下: broker.id=0 num.network.threads=2 num.io. ...
- PHP生成正则表达式的类
正则表达式,写起来还是比较费劲的,于是封装个类,初稿,抛砖引玉. 关于正则,建议参考 https://github.com/CyC2018/CS-Notes/blob/master/notes/%E6 ...
- Spring Boot 2整合Redis做缓存
既然是要用Redis做缓存,自然少不了安装了.但是本文主要讲Spring Boot与Redis整合.安装教程请另行百度! 1.首先是我们的Redis配置类 package com.tyc; impor ...
- [转帖]如何获得一个Oracle RAC数据库(从Github - oracle/vagrant-boxes) --- 暂时未测试成功 公司网络太差了..
如何获得一个Oracle RAC数据库(从Github - oracle/vagrant-boxes) 2019-11-20 16:40:36 dingdingfish 阅读数 5更多 分类专栏: 如 ...
- 概率dp - Uva 10900 So you want to be a 2n-aire?
So you want to be a 2n-aire? Problem's Link Mean: 玩一个答题赢奖金的游戏,一开始有1块钱,玩n次,每次赢的概率为t~1之间的某个实数. 给定n和t,求 ...
- 图论 --- 骑士周游问题,DFS
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28630 Accepted: ...
- sitecore-多变量测试与A / B测试概念论述
sitecore-多变量检验和A / B检验的区别 A / B测试和多变量测试有什么区别?让我们来看看这些测试方法的方法,常见用途,优点和局限性. sitecore中A / B测试 A / B测试(您 ...
- Mysql系列(四) —— MySQL的Charset和Collation
本文转载自:再见乱码:5分钟读懂MySQL字符集设置 一.内容概述 在MySQL的使用过程中,了解字符集.字符序的概念,以及不同设置对数据存储.比较的影响非常重要.不少同学在日常工作中遇到的" ...