(回文串 )Best Reward -- hdu -- 3613
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
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.
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.
/**
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 ;
}
最新文章
- featherview模板引擎
- Linux学习 :字符设备框架
- java8中map的meger方法的使用
- Tomcat编码问题及访问软链接文件设置
- NOI 题库 2753
- CentOS6.5菜鸟之旅:安装ATI显卡驱动
- ORA-01207: file is more recent than control file -
- Android应用程序中应用图标和名字的设置
- 学习笔记1_Day09_Servlet
- apache 反向代理配置(ubuntu)
- SqlServer2008(R2) 数据库使用外网IP实例连接服务器
- Azure 网站的新增功能:可配置的环境变量
- 基于visual Studio2013解决C语言竞赛题之1012连接字符串
- hightchart导出图片
- Ztree改版 - 将图标字体化 - fontAwesome
- SpringCloud学习之SpringCloudBus
- [HTML/CSS]浮动的那点事儿
- ajax---获取XMLHttpReuquest 对象
- Java == 和 equals 区别
- 一些简单二分题,简单的hash,H(i),字符串题