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]双倍回文——回文自动机
题目描述 输入 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. 输出 输出文件只有一行,即:输入数据中字符串的最长双倍回文子串的长度,如果双倍回文 ...
随机推荐
- 【2019年05月13日】A股ROE最高排名
个股滚动ROE = 最近4个季度的归母净利润 / ((期初归母净资产 + 期末归母净资产) / 2). 查看更多个股ROE最高排名. 兰州民百(SH600738) - 滚动ROE:86.45% - 滚 ...
- cad.net 读取pc3,pmp 读取pc3打印机文件
修改pc3文件还没做好..大家先look look怎么读.... 首先弄一个控制台程序, 然后去下载 Ionic.Zip 这个东西...载到控制台...都很简单... 然后就是复制下面代码,看控制台显 ...
- Haskell-chp01
-- 函数名首字母必须小写,可以包含 '来表示该函数严格求值版本(与惰性求值相对) doubleMe x = x + x doubleUs x y = doubleMe x + doubleMe y ...
- 使用Prometheus监控snmp
获取snmp信息 首先获取需要监控的snmp的基本信息,假设基本信息如下: snmp服务IP: 1.1.1.1 snmp community: public snmp exportor部署地址: 2. ...
- 040 RabbitMq及数据同步02
1.Spring AMQP (1)简介 Spring有很多不同的项目,其中就有对AMQP的支持: Spring AMQP的页面:http://spring.io/projects/spring-amq ...
- 解决ajax跨域请求问题
自己做网站的时候,经常遇到跨域问题,下面是平时多次实践总结出的解决方法,大家有什么更好的思路,可以相互交流下~ XMLHttpRequest cannot load http://www.imooc. ...
- 不能随便用get和set
有些对象呢,保存一半.如果你只提供get和set,那么备份不了数据. previousState的get和set还是最新的 wtforms InputRequired: DataRequired: i ...
- FusionInsight大数据开发---Hive应用开发
Hive应用开发 了解Hive的基本架构原理 掌握JDBC客户端开发流程 了解ODBC客户端的开发流程 了解python客户端的开发流程 了解Hcatalog/webHcat开发接口 掌握Hive开发 ...
- BAPI_TRANSACTION_COMMIT
通过NCO执行SAP里面的 BAPI_TRANSACTION_COMMIT 并不能直接生效,类似SQL 里面的事物一样,需要有开始与结束,正确的方式如下: RfcSessionManager.Begi ...
- 使用SqlBulkCopy将DataTable百万级数据瞬间入库
#region 使用SqlBulkCopy将DataTable中的数据批量插入数据库中 /// <summary> /// 注意:DataTable中的列需要与数据库表中的列完全一致.// ...