caioj1462: 【EXKMP】回文串
不得不说这是一道好题(前排膜拜灯教授),其实这道题如果不说是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】回文串的更多相关文章
- Extend to Palindrome UVA - 11475(补成回文串)
题意: 就是用最少的字符把原字符串补成回文串 解析: emm/.../网上都是用kmp和后缀数组做的 我没想到这俩的思路...emmm... 想到了exkmp的 就是原串和逆串匹配一下 注意要保证 ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- bzoj 3676 回文串 manachar+hash
考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...
- BZOJ 3676: [Apio2014]回文串
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2013 Solved: 863[Submit][Status ...
- SPOJ - PLSQUARE Palin Squar(hash+回文串)
题意:给你一个n*n (n<=200)的字符串矩阵,问你每行每列都是回文串的最大的m*m的矩阵是多少 题解:首先答案不满足单调性,即m成立而m-1与m+1都却不一定成立,所以必须枚举答案确定现在 ...
- 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...
随机推荐
- RAD6.0+EJB+WEBSPHERE+JNDI转eclipse+TOMCAT7+JDK1.7+JNDI+SPRING修改总计
##########################1.去除ejb################################################################### ...
- Codeforces Round #268 (Div. 2) D. Two Sets [stl - set + 暴力]
8161957 2014-10-10 06:12:37 njczy2010 D - Two Sets GNU C++ A ...
- 学习技术的三部曲:WHAT、HOW、WHY
★第一步:WHAT 所谓的“WHAT”也就是“What is it?”——这是最简单的层次.在这个层次,你要搞清楚某个东东是[什么]样子的?有[什么]用处?有[什么]特性?有[什么]语法?...... ...
- 数学知识巧学JCF(Java Collections framework)
不知你是否还记得高中我们学过的集合,映射,函数,数学确实很牛逼,拿它来研究java集合类,轻而易举的就把知识理解了.本篇文章适合初学java集合类的小白,也适合补充知识漏缺的学习者,同时也是面试者可以 ...
- Spring基于构造函数的依赖注入(DI)
以下内容引用自http://wiki.jikexueyuan.com/project/spring/dependency-injection/spring-constructor-based-depe ...
- SpringMVC拦截器详解[附带源码分析](转)
本文转自http://www.cnblogs.com/fangjian0423/p/springMVC-interceptor.html 感谢作者 目录 前言 重要接口及类介绍 源码分析 拦截器的配置 ...
- java 使用POI读取excel数据
原文:http://doc.okbase.net/0201zcr/archive/161440.html 一.定义 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Ja ...
- 从SOA到BFV【普元的一份广告文章】
人类对美好生活的追求是一切技术进步的原动力. 简便.快捷.联结……,这些移动互联的价值让它正成为最贴近消费者的力量.人和设备,设备和设备,人和服务,人和企业,企业和企业都发生了连接.诸如微信.携程.大 ...
- js -- 监听窗口的大小变化
- 全能无线渗透测试工具,一个LAZY就搞定了
近来一直在研究无线安全方面的东西,特别是在无线渗透测试这块,每次渗透测试时总要来回不停的切换操作和挑选利器,很是麻烦.就想看看是否可以有一款功能全面的集合型工具. 正所谓功夫不负有心人,还真有这么一个 ...