题目: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求回文串的更多相关文章

  1. HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )

    题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...

  2. Palindrome - URAL - 1297(求回文串)

    题目大意:RT   分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的.   线段数+后缀数组代码如下: ...

  3. 马拉车,O(n)求回文串

    马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...

  4. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  5. 3676: [Apio2014]回文串 求回文串长度与出现次数的最大值

    「BZOJ3676」[Apio2014] 回文串   Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所 ...

  6. manacher算法,求回文串

    用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...

  7. hdu 3294 manacher 求回文串

    感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...

  8. hihocoder 1032 manachar 求回文串O(n)

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  9. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

随机推荐

  1. Linux NIO 系列(04-3) epoll

    目录 一.why epoll 1.1 select 模型的缺点 1.2 epoll 模型优点 二.epoll API 2.1 epoll_create 2.2 epoll_ctl 2.3 epoll_ ...

  2. leetcode.双指针.633平方数之和-Java

    1. 具体题目 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a^2 + b^2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 注 ...

  3. 利用单选框的单选特性作tab切换

    <RadioGroup v-model="selectType" type="button" @onchange="selectTypeChan ...

  4. spring基于注解的事务控制

    pom配置: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  5. PAT 乙级练习题1002. 写出这个数 (20)

    1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式 ...

  6. Ti 949 配置 948 i2c

    如果不想用 远端 slave,只访问948   选 i2c pass through all =1  :i2c pass through =0/1 0x0c 0x17 0x9e 如果想用远端  sla ...

  7. transformer模型计算图

    参考了这篇文章:http://nlp.seas.harvard.edu/2018/04/03/attention.html 结合代码和图,能更加清楚的了解transformer中的一些原理(ps,等下 ...

  8. 【LeetCode】Math

    [263] Ugly Number [Easy] 一个数的质因子只有2,3,5就叫丑数,写个函数判断丑数. //Author: Wanying //注意 0 和 1 的corner case, 你居然 ...

  9. mysql的事务四个特性以及 事务的四个隔离级别

    一.事务四大属性 分别是原子性.一致性.隔离性.持久性. 1,原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,因此事务的操作如果成功就必须要完全应用到数据库, ...

  10. PHP面向对象简易验证码类

    PHP简易验证码类 <?php class authCode { private static $instance = null; #实例对象 private $width = 120; #图片 ...