hu3613 Best Reward
地址: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): 2326 Accepted Submission(s): 944
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.
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
6
思路:扩展kmp
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio> using namespace std;
const int K=1e6+;
int nt[K],extand[K],v[],sum[K],l[K],r[K];
char sa[K],sb[K];
void Getnext(char *T,int *next)
{
int len=strlen(T),a=;
next[]=len;
while(a<len- && T[a]==T[a+]) a++;
next[]=a;
a=;
for(int k=; k<len; k++)
{
int p=a+next[a]-,L=next[k-a];
if( (k-)+L >= p)
{
int j = (p-k+)> ? (p-k+) : ;
while(k+j<len && T[k+j]==T[j]) j++;
next[k]=j;
a=k;
}
else
next[k]=L;
}
}
void GetExtand(char *S,char *T,int *next)
{
Getnext(T,next);
int slen=strlen(S),tlen=strlen(T),a=;
int MinLen = slen < tlen ? slen : tlen;
while(a<MinLen && S[a]==T[a]) a++;
extand[]=a;
a=;
for(int k=; k<slen; k++)
{
int p=a+extand[a]-, L=next[k-a];
if( (k-)+L >= p)
{
int j= (p-k+) > ? (p-k+) : ;
while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
extand[k]=j;
a=k;
}
else
extand[k]=L;
}
}
int main(void)
{
int t;cin>>t;
while(t--)
{
int ans=;
for(int i=;i<;i++)
scanf("%d",v+i);
scanf("%s",sa);
int len=strlen(sa);
for(int i=;i<len;i++)
sb[i]=sa[len-i-];
sum[]=v[sa[]-'a'];
for(int i=;i<len;i++)
sum[i]=sum[i-]+v[sa[i]-'a'];
sb[len]='\0';
GetExtand(sb,sa,nt);
for(int i=;i<len;i++)
if(extand[i]==len-i) l[len-i-]=;
else l[len-i-]=;
GetExtand(sa,sb,nt);
for(int i=;i<len;i++)
if(extand[i]==len-i) r[i]=;
else r[i]=;
for(int i=;i<len-;i++)
{
int tmp=;
if(l[i]) tmp+=sum[i];
if(r[i+]) tmp+=sum[len-]-sum[i];
ans=max(ans,tmp);
}
printf("%d\n",ans);
}
return ;
}
hu3613 Best Reward的更多相关文章
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- 回文串---Best Reward
HDU 3613 Description After an uphill battle, General Li won a great victory. Now the head of state ...
- hdu 2647 Reward
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...
- Reward
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdoj 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Reward HDU
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32 ...
- Reward(拓扑结构+邻接表+队列)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
随机推荐
- 【转】Native Thread for Win32 B-Threads Synchronization(通俗易懂,非常好)
http://www.bogotobogo.com/cplusplus/multithreading_win32B.php Synchronization Between Threads In t ...
- 编程之美 set 19 连连看游戏设计
题目 1. 怎么用简单的计算机模型来描述这个问题 ? 2. 怎么判断两个图像是否能相消 3. 怎么求出相同图形之间的最短距离(转弯数最少)? 4. 怎么确定目前处于死锁状态? 如何设计算法来解除死锁? ...
- iOS开发之--TableViewCell重用机制避免重复显示问题
常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复 // 这样配置的话超过页面显示的内容会重复出现 - (UITableViewCell *)tableView:(U ...
- Codelf 搜索开源代码帮程序员命名
"计算机科学里两件最难的事:缓存失效和命名." Codelf通过搜索在线开源平台Github, Bitbucket, Google Code, Codeplex, Sourcefo ...
- std::lock_guard
/* std::lock_guard:更方便线程对于互斥量的上锁操作 std::lock_guard:在定义一个lock_guard时上锁,保证在析构或者异常时解锁 */ #include <i ...
- mybatis设置数据库连接的密码不需要加密
mybatis设置数据库连接的密码不需要加密:在数据库连接配置处加上:<property name="connectionProperties" value="co ...
- windows下安装google protocbuf
首先安装setuptools: windows:======== 1.下载 ez_setup.py,安装setuptoolshttps://bitbucket.org/pypa/setuptools/ ...
- 反编译apk + eclipse中调试smali
1.对apk使用apktool反编译出可调试的smali代码到out文件夹 apktool -d d 定点加粉丝_com.mingniu.wxddjfs_440.apk -o out 这里必须使用-d ...
- linux查看硬件信息的方法
目前会Linux的人不少,但是精通的不多,怎样才能做一个符合企业需求的Linux人才,首先要有良好的Linux基础知识.本文为你讲解Linux的知识,今天所讲的是 Linux硬件信息怎样查看,希望你能 ...
- devops model
1,自动部署() 2,在线监控 (可视化,智能化) 3,故障诊断 (故障识别,故障隔离) 4,快速定位 (识别环境与程序,数据等问题) 评测工具开发---- 统计条目增加大项目: BI中权限―拨测数据 ...