不得不说这是一道好题(前排膜拜灯教授),其实这道题如果不说是EXKMP,很容易就想到Manacher(好像也可以这样做)

回到这道题,这样只有一个字符串,还要求回文?立刻想到了将这个串和它的反串跑EXKMP,举个例子:
假设字符串s[0]是acacac,那它的反串s[1]就是cacaca,互相跑EXKMP就有:
ex[0]={0,0,5,0,3,0,1}//这里的定义是以s[0]为模版串
ex[1]={0,0,5,0,3,0,1}
然后就可以枚举断的地方,假设a|cacac i=2
那定义一个j等于len-(i-1)+1就指向cacaca的最后一个a,等于6,然后得到ex[1][6]有多少个匹配的,当然了,6+ex[1][6]-1要等于len才行,不然这两个串就不是完全匹配的了。同理i后面的cacac也是这样搞,(当然你可以像灯教授和肉丝鸡掌一样搞个前缀和什么的省时间 %%%%%)

然而昨天灯教授故意卡了我,搞得我现在又要改成用前缀和了。。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[];
char s[][];
int p[][],ex[][];
void exkmp(int len,int w)
{
int x,k;
p[-w][]=len;
x=;while(s[-w][x]==s[-w][x+]&&x<=len)x++;
p[-w][]=x-;k=;
for(int i=;i<=len;i++)
{
int P=k+p[-w][k]-,L=p[-w][i-k+];
if(i-k+L<P-k+)p[-w][i]=L;
else
{
int j=max(P-i+,);
while(s[-w][+j]==s[-w][i+j]&&i+j<=len)j++;
p[-w][i]=j;k=i;
}
} x=;while(s[w][x]==s[-w][x]&&x<=len)x++;
ex[w][]=x-;k=;
for(int i=;i<=len;i++)
{
int P=k+ex[w][k]-,L=p[-w][i-k+];
if(i-k+L<P-k+)ex[w][i]=L;
else
{
int j=max(P-i+,);
while(s[-w][+j]==s[w][i+j]&&i+j<=len)j++;
ex[w][i]=j;k=i;
}
}
}
int qz[][];
void getsum(int len)
{
int ans=,ss,x,tp;
for(int i=;i<=len;i++)
{
ss=;int j=len-(i-)+;
if(j+ex[][j]-==len)ss+=qz[][ex[][j]];
if(i+ex[][i]-==len)ss+=qz[][ex[][i]];
if(ss>ans)ans=ss;
}
printf("%d\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=;i<=;i++)scanf("%d",&a[i]);
scanf("%s",s[]+);int len=strlen(s[]+);
for(int i=;i<=len;i++)s[][i]=s[][len-i+];
qz[][]=;qz[][]=;
for(int i=;i<=len;i++)
{
qz[][i]=qz[][i-]+a[s[][i]-'a'+];
qz[][i]=qz[][i-]+a[s[][i]-'a'+];
} exkmp(len,);exkmp(len,);
getsum(len);
}
return ;
}

caioj1462: 【EXKMP】回文串的更多相关文章

  1. Extend to Palindrome UVA - 11475(补成回文串)

    题意: 就是用最少的字符把原字符串补成回文串 解析: emm/.../网上都是用kmp和后缀数组做的 我没想到这俩的思路...emmm... 想到了exkmp的  就是原串和逆串匹配一下  注意要保证 ...

  2. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  3. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  6. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. bzoj 3676 回文串 manachar+hash

    考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...

  8. BZOJ 3676: [Apio2014]回文串

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2013  Solved: 863[Submit][Status ...

  9. SPOJ - PLSQUARE Palin Squar(hash+回文串)

    题意:给你一个n*n (n<=200)的字符串矩阵,问你每行每列都是回文串的最大的m*m的矩阵是多少 题解:首先答案不满足单调性,即m成立而m-1与m+1都却不一定成立,所以必须枚举答案确定现在 ...

  10. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

随机推荐

  1. Spoj-NETADMIN Smart Network Administrator

    The citizens of a small village are tired of being the only inhabitants around without a connection ...

  2. JSP学习笔记(七十八):struts2中s:select标签的使用

    1.第一个例子: <s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00& ...

  3. 转 DOS 8.3 文件名命名规则

    http://www.360doc.com/content/10/0813/14/73007_45757514.shtml DOS 8.3 文件名命名规则 经常看到命令行或者其它软件在显示目录的时候出 ...

  4. MBP清除NVRAM和PRAM

    Mac 会将某些设置存储在特定的存储区中,即使关机这些设置也不会丢失.在基于 Intel 的 Mac 上,存储位置是称为 NVRAM 的内存:而在基于 PowerPC 的 Mac 上,存储位置则是称为 ...

  5. ZOJ 3717 二分+2-sat判定。

    好久没有2-sat了,此题当复习之用,二分求最大值+2-sat判断可行,此题主要跪于题意:The results should be rounded to three decimal places. ...

  6. android studio AndroidManifest

    一.目录结构 1. AndroidManifest.xml 它是一个清单文件,提供应用的基本信息 <?xml version="1.0" encoding="utf ...

  7. Gradle讲解

    简介: Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置. ...

  8. 【westorm系列之二】配置格式化

    1.执行格式化后,给句尾加分号

  9. 【RESTful】1.理解REST和RESTful

    简记:一套设计良好的RESTful可以帮助互联网产品支持[单个服务端+多个客户端]的场景!!!

  10. Intent传递简单对象与集合

    我们在Intent传递传递对象.能够有三种方式,实现Serializable接口.实现Parcelable接口,使用json格式序列化与反序列化. 在此我们使用第二方式,现实Parcelable接口, ...