http://acm.hdu.edu.cn/showproblem.php?pid=3613

Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1210    Accepted Submission(s): 495

Problem Description
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him with honor and treasures for his great exploit.

One of these treasures is a necklace made up of 26 different kinds of gemstones, and the length of the necklace is n. (That is to say: n gemstones are stringed together to constitute this necklace, and each of these gemstones belongs to only one of the 26 kinds.)

In accordance with the classical view, a necklace is valuable if and only if it is a palindrome - the necklace looks the same in either direction. However, the necklace we mentioned above may not a palindrome at the beginning. So the head of state decide to cut the necklace into two part, and then give both of them to General Li.

All gemstones of the same kind has the same value (may be positive or negative because of their quality - some kinds are beautiful while some others may looks just like normal stones). A necklace that is palindrom has value equal to the sum of its gemstones' value. while a necklace that is not palindrom has value zero.

Now the problem is: how to cut the given necklace so that the sum of the two necklaces's value is greatest. Output this value.

 
Input
The first line of input is a single integer T (1 ≤ T ≤ 10) - the number of test cases. The description of these test cases follows.

For each test case, the first line is 26 integers: v1, v2, ..., v26 (-100 ≤ vi ≤ 100, 1 ≤ i ≤ 26), represent the value of gemstones of each kind.

The second line of each test case is a string made up of charactor 'a' to 'z'. representing the necklace. Different charactor representing different kinds of gemstones, and the value of 'a' is v1, the value of 'b' is v2, ..., and so on. The length of the string is no more than 500000.

 
Output
Output a single Integer: the maximum value General Li can get from the necklace.
 
Sample Input
2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
aba
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
acacac
 
Sample Output
1
6
 
给你一个字符串,每个字符有它们各自的价值,把这个串分成两部分(随意分割),如果这部分是回文串的话计算它的价值,否则价值为0, 计算它能得到的最大价值

/**
s[]         先存原字符串,后存扩展后的字符串
p[]        p[i] 表示以i为中心的回文串有多长(只记录一边的长度)
sum[]    sum[i]表示前i个字符的总价值和
Left[]    Left[i] 表示前缀长度为 i 的串是否是回文串
Right[]  Right[i] 表示后缀长度为 i 的串是否是回文串
**/

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std;
#define INF 0x3f3f3f3f
#define N 1000007 char s[N];
int Left[N], Right[N], sum[N], p[N]; void Manacher()
{
int len = strlen(s); int index = , MaxLen = , i; for(i=; i<len; i++)
{
if(MaxLen>i) p[i] = min(p[index*-i], MaxLen-i);
else p[i] = ; while( s[i-p[i]]==s[i+p[i]]) p[i]++; if(p[i]+i>MaxLen)
{
MaxLen = p[i] + i;
index = i;
} if(p[i]==i)
Left[p[i]-] = true;
if(p[i]+i==len)
Right[p[i]-] = true;
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int a[], i; memset(Left, , sizeof(Left));
memset(Right, , sizeof(Right)); for(i=; i<; i++)
scanf("%d", &a[i]); scanf("%s", s); int len = strlen(s); for(i=; i<=len; i++)
sum[i] = sum[i-] + a[s[i-]-'a']; for(i=len; i>=; i--)
{
s[i*+] = s[i];
s[i*+] = '#';
}
s[] = '$'; Manacher(); int ans = -INF;
for(i=; i<len; i++)
{
int t = ;
if(Left[i])
t += sum[i];
if(Right[len-i])
t += sum[len]-sum[i]; ans = max(ans, t);
} printf("%d\n", ans); }
return ;
}

(回文串 )Best Reward -- hdu -- 3613的更多相关文章

  1. 回文串---Best Reward

    HDU   3613 Description After an uphill battle, General Li won a great victory. Now the head of state ...

  2. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

  3. HDU 3613 Best Reward(manacher求前、后缀回文串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...

  4. HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

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

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

  6. Best Reward HDU 3613(回文子串Manacher)

    题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值.   分析:首先的明白这个最大价值有 ...

  7. hdu 3613 扩展kmp+回文串

    题目大意:给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串(从左往右或者从右往左读,都一样),那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果 ...

  8. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  9. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

随机推荐

  1. Linux就业技术指导(六):天津IDC机房项目实践

    一,天津IDC机房项目图片介绍 服务器DELL R720 二,远程控制卡配置方法 远程控制卡,在服务器没有装操作系统或者操作系统出问题了.用户可以通过连接到远程控制卡来连接服务器,就如同切换到我们的虚 ...

  2. MySql log_bin

    [MySql log_bin] 1.查看 log_bin 是否启用. 默认情况下,mysql server 不启用 binlog(验证方法1: 执行"show variables" ...

  3. 解决selenium不支持firefox低版本的问题

    解决selenium不支持firefox低版本的问题 在火狐浏览器升级后,突然发现webdriver运行脚本的时候不能调出火狐浏览器了,并报错WebDriverException:Message:'C ...

  4. IE6 PNG不透明问题 (只解决img标签的图片)

    <script type='text/javascript' src="/script/ie6.pngfix.js"></script> 会让一些posit ...

  5. 快速上手Vue

    课程目录: ES6常用语法 Vue基础以及指令 Vue组件 Vue-Router Vue生命周期 npm webpack vue-cli Vuex以及axios

  6. 如何搭建hibernate框架

    我写这篇博客,主要是想让大家能够快速上手hibernate,本人建议学习框架,应该一个框架一个框架学习,别一上手就三大框架整合,学习之类的.这里只是单独搭建hibernate框架,让大家 能够更好的上 ...

  7. Finding Hotels

    Finding Hotels http://acm.hdu.edu.cn/showproblem.php?pid=5992 Time Limit: 2000/1000 MS (Java/Others) ...

  8. 【校招面试 之 C/C++】第6题 C++深拷贝与浅拷贝

    1.两个的区别(1)在未定义显示拷贝构造函数的情况下,系统会调用默认的拷贝函数——即浅拷贝,它能够完成成员的一一复制.当数据成员中没有指针时,浅拷贝是可行的: 但当数据成员中有指针时,如果采用简单的浅 ...

  9. pyhon之函数参数

    #函数的参数分为形参和实参,其中形参就是形式参数,是在创建函数的时候定义,实参就是实际参数,是在调用的函数的时候创建,这个并不是重点,具体#的参数内部,我们可以把参数分为以下4种# 1.普通参数# 2 ...

  10. Excel单元格内容批量加前缀

    比如83190001在A1单元格,要在A列单元内容前面批量加0,在B1输入公式="0"&A1然后向下复制再把B列复制的结果再复制一下:然后到新的一列粘贴,在“粘贴选项”中选 ...