拓展KMP求回文串
题目:hdu3613;
题意:有26字母对应的价值,然后给出以个串,把它分成两段字串,如果字串是回文串,串的价值就是每个字符和,不是就为0。求最大价值。
分析:拓展KMP的应用求回文字串。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
const int max_=5e6+;
int extend1[max_],extend2[max_],v[],w[max_], nex[max_];
void getnext(string str)
{
int a=,p=;
int len=str.size(); nex[]=len;
for(int i=;i<len;i++)
{
if(i>=p||i+nex[i-a]>=p)
{
if(i>=p)
p=i;
while(str[p-i]==str[p]&&p<len)
p++;
nex[i]=p-i;
a=i;
}
else
nex[i]=nex[i-a];
}
}
void getextend(string str,string mo,int *ex)
{
int a=,p=;
getnext(mo);
int m=mo.size();
int len=str.size();
for(int i=;i<len;i++)
{
if(i>=p||i+nex[i-a]>=p)
{
if(i>=p)
p=i;
while(str[p]==mo[p-i]&&p<len&&p-i<m)
p++;
ex[i]=p-i;
a=i;
}
else
ex[i]=nex[i-a];
}
}
int main()
{
ios_base::sync_with_stdio();
cin.tie();
int t;
string S,T;
scanf("%d",&t);
while(t--)
{
int max_sum=-1e8;
for(int i=;i<;i++)
{
scanf("%d",&v[i]);
}
cin>>S;
int len=S.size();
for(int i=;i<len;i++)
{
if(i==)
w[i]=v[S[i]-'a'];
else
w[i]=w[i-]+v[S[i]-'a'];
}
T=S;
reverse(T.begin(),T.end());
getextend(S,T,extend1);
getextend(T,S,extend2);
for(int i=;i<len;i++)
{
int sum=;
if(i+extend1[i]==len)
sum+=w[len-]-w[i-];
if(len-i+extend2[len-i]==len)
sum+=w[i-];
max_sum=max(max_sum,sum);
}
printf("%d\n",max_sum);
}
}
拓展KMP求回文串的更多相关文章
- HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )
题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...
- Palindrome - URAL - 1297(求回文串)
题目大意:RT 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
- 马拉车,O(n)求回文串
马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
- 3676: [Apio2014]回文串 求回文串长度与出现次数的最大值
「BZOJ3676」[Apio2014] 回文串 Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所 ...
- manacher算法,求回文串
用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...
- hdu 3294 manacher 求回文串
感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...
- hihocoder 1032 manachar 求回文串O(n)
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
随机推荐
- c# dotNetBar symbol属性代码动态设置方法
C#: button.Symbol = "\uf060"; VB: button.Symbol = ChrW("&Hf060") Since we in ...
- python获取港股通每日成交信息
接口:ggt_daily 描述:获取港股通每日成交信息,数据从2014年开始 限量:单次最大1000,总量数据不限制 积分:用户积2000积分可调取,5000积分无限制,请自行提高积分,具体请参阅本文 ...
- mac 卸载编辑器卸不干净
Configuration ~/Library/Preferences/ Caches ~/Library/Caches/ Plugins ~/Library/Application Support/ ...
- MyBatis操作数据库(基本增删改查)
一.准备所需工具(jar包和数据库驱动) 网上搜索下载就可以 二.新建一个Java project 1.将下载好的包导入项目中,build path 2.编写MyBatis配置文件:主要填写prope ...
- mysql导入.csv文件出错
1.报错信息 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cann ...
- 关于linux centos7 vmware 和windows7 文件共享笔记
本方法是以win7,VMware9.0.1 ,centos6.4为基础实验的. 对于linux的初级使用阶段,都会Windows中使用linux虚拟机VMWare或者其它的.在Windows与linu ...
- nginx自动切割日志脚本
#!/bin/bash savepath_log='/data/logs' nglogs='/data/logs' mkdir -p $savepath_log/$(date +%Y)/$(date ...
- Nginx---配置详解(转发)
转载于 http://baijiahao.baidu.com/s?id=1604485941272024493&wfr=spider&for=pc 1.概述 Nginx配置文件的整体 ...
- ApiCloud如何一键真机测试
首先假设你已经有了APICloud账号,并创建了App项目. 需要注意的是,手机和电脑需要连接在同一wifi环境下. 第一步 下载自定义Loader 进入“开发控制台” 点击我们的项目 选择模块,再选 ...
- mysql-一行分隔成多行数据
mysql将某个字段有分隔符号分隔成多行数据 SELECT a.id, a. NAME, substring_index( substring_index( a.name, ',', b.help_t ...