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 双倍回文的更多相关文章

  1. BZOJ2342: [Shoi2011]双倍回文

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 923  Solved: 317[Submit][Status ...

  2. 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...

  3. BZOJ 2342: [Shoi2011]双倍回文 马拉车算法/并查集

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1123  Solved: 408 题目连接 http://w ...

  4. [SHOI2011]双倍回文 manacher

    题面: 洛谷:[SHOI2011]双倍回文‘ 题解: 首先有一个性质,本质不同的回文串最多O(n)个. 所以我们可以对于每个i,求出以这个i为结尾的最长回文串,然后以此作为长串,并判断把这个长串从中间 ...

  5. bzoj 2342: [Shoi2011]双倍回文 -- manacher

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符 ...

  6. BZOJ2342 Shoi2011 双倍回文 【Manacher】

    BZOJ2342 Shoi2011 双倍回文 Description Input 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. Output 输 ...

  7. Manacher || BZOJ 2342: [Shoi2011]双倍回文 || Luogu P4287 [SHOI2011]双倍回文

    题面:[SHOI2011]双倍回文 题解:具体实现时,就是在更新mr时维护前半段是回文串的最长回文串就好了 正确性的话,因为到i时如果i+RL[i]-1<=mr,那么答案肯定在i之前就维护过了: ...

  8. [SHOI2011]双倍回文

    Description   Input 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. Output 输出文件只有一行,即:输入数据中字符串的最长 ...

  9. BZOJ2342[Shoi2011]双倍回文——回文自动机

    题目描述 输入 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. 输出 输出文件只有一行,即:输入数据中字符串的最长双倍回文子串的长度,如果双倍回文 ...

随机推荐

  1. windows下的文件管理工具--total commander

    https://www.ghisler.com/ http://www.guyiren.com/archives/1647

  2. dockerfile 命令

    FROM 功能为指定基础镜像,并且必须是第一条指令. 如果不以任何镜像为基础,那么写法为:FROM scratch. 同时意味着接下来所写的指令将作为镜像的第一层开始 语法: FROM <ima ...

  3. Go学习笔记之Map

    Go学习笔记之Map Map 引用类型,哈希表.map的key必须可以比较相等,除了slice, map, function的内建类型都可以作为key.struct类型不包含上述字段,也可作为key. ...

  4. 百度前端技术学院-task1.3源代码

    因为其中有图片,所以就给有图片的位置加了边框和设置了大小,这样哪怕图片不显示也可以知道在哪里. <!DOCTYPE html> <html> <head> < ...

  5. Windows和Linux简单命令的总结

    MS-DOS 命令提示符(cmd) 启动:                      Win+R,输入cmd回车 切换盘符            盘符名称: 进入文件夹              cd ...

  6. golang ---rune与byte

    golang内置类型有rune类型和byte类型. rune类型的底层类型是int32类型,而byte类型的底层类型是int8类型,这决定了rune能比byte表达更多的数. 在unicode中,一个 ...

  7. VS2019+EF6连接Mysql

    开发环境:Win10 + VS2019Mysql服务器版本:8.0.16 一.下载并安装插件(必备) MySQL-Connector-net-6.9.12  链接https://cdn.mysql.c ...

  8. DevExpress之GridControl控件小知识

    DevExpress之GridControl控件小知识 一.当代码中的DataTable中有建数据关系时,DevExpress 的 GridControl 会自动增加一个子视图 .列名也就是子表的字段 ...

  9. WPF解决WindowsFormsHost背景透明

    项目案例:WPF使用WindowsFormsHost播放视频,视频上显示边框.字幕等特效: 难点问题 1.WindowsFormsHost不支持背景透明: 2.WPF Panel.ZIndex无效,W ...

  10. scxml 图像展示器 (基于C++ MFC GDI tinyxpath的实现)

    以前的时候学习新东西没有总结的习惯,周末把以前研究的东西翻了翻,稍微总结下. Scxml是w3c出来的基于状态机的对话脚本语言标准,具体内容可以谷歌到,这里讲述自己开发的一个把scxml转化为可交互图 ...