hdu 3613 Best Reward (manacher)
2015-10-03 17:43
239 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3613
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
题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是每个字母的价值之和,如果不是那么价值就是0;
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
abbadf
那么可以分成abba 和df abba的价值是1+2+2+1=6,df不是回文串所以价值为0;总价值就是6;
我们可以用数组L【i】R【i】来记录串的前缀长度为i的是否是回文串和后缀长度为i的是否是回文串;
Best Reward
Problem DescriptionAfter 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
题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是每个字母的价值之和,如果不是那么价值就是0;
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
abbadf
那么可以分成abba 和df abba的价值是1+2+2+1=6,df不是回文串所以价值为0;总价值就是6;
我们可以用数组L【i】R【i】来记录串的前缀长度为i的是否是回文串和后缀长度为i的是否是回文串;
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define N 1501000 #define INF 0x3f3f3f3f #define PI acos (-1.0) #define EPS 1e-8 #define met(a, b) memset (a, b, sizeof (a)) char str ; int a , p , sum , L , R ; ///sum[i]代表s串中前i个字符的价值总和; ///p[i]代表以i为中心的回文串的半径(包含i本身); ///L[i]表示前i个字符是否是回文串,R[i]表示长度为i的后缀是否为回文串; void manacher (); int main () { int t; scanf ("%d", &t); while (t--) { for (int i=0; i<26; i++) scanf ("%d", &a[i]); scanf ("%s", str); met (sum, 0); met (p, 0); met (L, 0); met (R, 0); int len = strlen (str); for (int i=1; i<=len; i++) sum[i] += sum[i-1] + a[ str[i-1]-'a' ]; for (int i=len; i>=0; i--) { str[i*2+2] = str[i]; str[i*2+1] = '#'; } str[0] = '$'; manacher (); int ans = 0; for (int i=1; i<len; i++) { int t = 0; if (L[i]) t += sum[i]; if (R[ len-i ]) t += sum[len] - sum[i]; ans = max (ans, t); } printf ("%d\n", ans); } return 0; } void manacher () { int len = strlen (str), maxlen = 0, Index = 0; for (int i=2; i<len; i++) { if (maxlen > i) p[i] = min (p[ Index*2-i ], maxlen-i); else p[i] = 1; while (str[ i+p[i] ] == str[ i-p[i] ]) p[i]++; if (maxlen < i + p[i]) { maxlen = i + p[i]; Index = i; } if (i == p[i]) L[ p[i]-1 ] = true; if (i + p[i] == len) R[ p[i]-1 ] = true; } }
相关文章推荐
- Spring DI 利用构造函数给属性赋值
- Arduino2-库文件及基本函数
- 配置Java环境变量
- 10.3 linux
- Angularjs登陆-Form测试
- 轻松搞定面试中的二叉树题目
- 大连交通大学IPTV使用方法
- 【笔试】49、数字在排序数组中出现的次数
- UVa 1601 例题7-9 万圣节后的早晨
- gerrit cherry pick to merge code
- 轻松搞定面试中的链表题目
- 我的博客生涯
- 《zw版·Halcon-delphi系列原创教程》halconxlib控件列表
- 无题(一):去除字符串中的空格
- 快速排序
- 弱校联萌十一大决战之强力热身 J. Right turn (模拟)
- 快速排序
- 【笔试】48、数组中的逆序对
- Heartbleed 实战:一个影响无数网站的缓冲区溢出漏洞
- QT5 学习之路15---布局管理器