现在的位置: 首页 > 综合 > 正文

hdu3613—Best Reward

2019年02月15日 ⁄ 综合 ⁄ 共 3505字 ⁄ 字号 评论关闭

Best Reward

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

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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3618 3620 3614 3615 3616 
 

Statistic | Submit | Discuss
| Note

这题我是用manacher做的,不过很多人好像是EKMP搞的

先求出p数组,然后遍历一遍,找到回文到字符串端点的回文串,然后求最大值即可

/*************************************************************************
    > File Name: hdu3613.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月02日 星期一 11时59分27秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 1100000;
char str[N];
char mat[N];
int p[N];
int val[30];
int sum[N];

void manacher (int cnt)
{
	memset (p, 0, sizeof(p));
	int MaxId = 0, id;
	for (int i = 1; i < cnt; ++i)
	{
		if (MaxId > i)
		{
			p[i] = min (p[2 * id - i], MaxId - i);
		}
		else
		{
			p[i] = 1;
		}
		while (str[i + p[i]] == str[i - p[i]])
		{
			++p[i];
		}
		if (p[i] + i > MaxId)
		{
			MaxId = p[i] + i;
			id = i;
		}
	}
}

int main ()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		for (int i = 0; i < 26; ++i)
		{
			scanf("%d", &val[i]);
		}
		scanf("%s", mat);
		int cnt = 2;
		int len = strlen (mat);
		for (int i = 0; i < len; ++i)
		{
			str[cnt++] = mat[i];
			str[cnt++] = '#';
		}
		str[0] = '?';
		str[1] = '#';
		str[cnt] = '\0';
		sum[0] = 0;
		manacher (cnt);
		for (int i = 1; i < cnt; ++i)
		{
			sum[i] = sum[i - 1];
			if (str[i] != '#')
			{
				sum[i] += val[str[i] - 'a'];
			}
		}
		int ans = 0;
		for (int i = 2; i < cnt - 1; ++i)
		{
			if (p[i] - 1 == len)
			{
				continue;
			}
			if (i - p[i] + 1 == 1)
			{
				int tmp = sum[i + p[i] - 1];
				int id = (cnt - 1 + i + p[i] - 1) / 2;
				if (p[id] == cnt - 1 - id + 1)
				{
					tmp += sum[cnt - 1] - sum[i + p[i] - 2];
				}
				ans = max(ans, tmp);
			}
			else if (i + p[i] - 1 == cnt - 1)
			{
				int tmp = sum[i + p[i] - 1] - sum[i - p[i]];
				int id = (1 + i - p[i] + 1) / 2;
				if (p[id] == id)
				{
					tmp += sum[i - p[i] + 1];
				}
				ans = max(ans, tmp);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

抱歉!评论已关闭.