hdu 3613 KMP算法扩展
Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 500 Accepted Submission(s): 201
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std; const int MAX=+;
char s1[MAX],s2[MAX];
int f[MAX],sum[MAX],val[];
bool per[MAX],pos[MAX]; void getFail(char *a,int len)
{
int i,j;
f[]=f[]=;
for(i=;i<len;i++)
{
j=f[i];
while(j && a[i]!=a[j]) j=f[j];
f[i+]=(a[i]==a[j]?j+:);
}
} int find(char *a,char *b,int len)
{
int i,j=;
for(i=;i<len;i++)
{
while(j && a[i]!=b[j]) j=f[j];
if(a[i]==b[j]) j++;
}
return j==len?f[j]:j;//必需要分成两部分,排除整个为回文串的情况
} int main()
{
int n,k,i,num,ans,len;
scanf("%d",&n);
while(n--)
{
for(i=;i<;i++)scanf("%d",val+i);
scanf("%s",s1);
len=strlen(s1);
for(i=;i<=len/;i++) s2[i]=s1[len-i-],s2[len-i-]=s1[i];
sum[]=val[s1[]-'a'];
for(i=;i<=len;i++) sum[i]=sum[i-]+val[s1[i-]-'a'];
getFail(s1,len);
k=find(s2,s1,len);//求s1最大前缀回文串位置
memset(per,false,sizeof(per));
while(k) per[k]=true,k=f[k];//标记所有前缀回文串
getFail(s2,len);
k=find(s1,s2,len);//求s1最大后缀回文串位置
memset(pos,false,sizeof(pos));
while(k) pos[k]=true,k=f[k];//标记所有后缀回文串
ans=-INF;
for(i=;i<len;++i)
{
num=;
if(per[i]) num+=sum[i];
if(pos[len-i])num+=sum[len]-sum[i];
if(num>ans)ans=num; }
printf("%d\n",ans);
}
return ;
}
hdu 3613 KMP算法扩展的更多相关文章
- hdu 4300 kmp算法扩展
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- hdu 1686 KMP算法
题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- HDU 2594 kmp算法变形
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- KMP算法模板&&扩展
很不错的学习链接:https://blog.csdn.net/v_july_v/article/details/7041827 具体思路就看上面的链接就行了,这里只放几个常用的模板 问题描述: 给出字 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
随机推荐
- Silverlight日记:动态生成DataGrid、行列装换、动态加载控件
本文主要针对使用DataGrid动态绑定数据对象,并实现行列转换效果. 一,前台绑定 <sdk:DataGrid x:Name="dataGrid2" Style=" ...
- iOS快速开发框架--Bee Framework
Bee Framework是一款iOS快速开发框架,允许开发者使用Objective-C和XML/CSS来进行iPhone和iPad开发,由 Gavin Kwoe 和 QFish 开发并维护. 其早期 ...
- C#传递数组参数
在C#中,可以将数组作为参数传递给方法,同时方法可以更改数组元素的值. 一.将一维数组作为参数传递给方法 using System;using System.Collections.Generic;u ...
- AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?
问题 An error occurred while starting the application. ArgumentException: AddDbContext was called with ...
- 66. Plus One@python
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- day3-python 登录
import datetime # 1. f = open('users') result = f.read() f.close() user_list = result.split() # user ...
- linux文件属性描述(inode,block)
1.ls -lhi 文字解释: 第一列:inode索引节点编号(人的身份证,全国唯一) 系统读取文件时首先通过文件名找到inode,然后才能读取到文件内容. 第二列:文件类型及权限(共10个字符, ...
- Python基础:字符串(string)
字符串的常用操作 字符串与数组一样,支持索引操作.切片与遍历 索引.切片操作: name = 'jason' name[0] 'j' name[1:3] 'as' 遍历: for char in na ...
- redis列表,字典,管道,vue安装,创建项目
redis mysql,redis,mogondb 1.mysql,oracle:关系型数据库,有表概念 2.redis,mongodb/nosql:非关系型数据库 没有表概念 mongodb存储在硬 ...
- Python9-进程理论-day35
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Tim'''进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源 ...