hdu3613

您所在的位置:网站首页 邵氏弃儿事件后续 hdu3613

hdu3613

2024-06-20 22:53| 来源: 网络整理| 查看: 265

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

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.

 InputThe 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.

 OutputOutput a single Integer: the maximum value General Li can get from the necklace. Sample Input21 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1aba1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1acacac Sample Output16 Source2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU  Recommendlcy   |   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: [email protected] > Created Time: 2015年02月02日 星期一 11时59分27秒 ************************************************************************/#include #include #include #include #include #include #include #include #include #include #include using namespace std;const double pi = acos(-1);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair 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;}

0 0 【KMP】 hdu3613 Best Reward HDU3613 Best Reward hdu3613---Best Reward [Manacher][HDU3613][Best Reward] hdu3613 Best Reward(KMP) [扩展KMP][HDU3613][Best Reward] HDU3613 Best Reward - exkmp/Manacher HDU3613:Best Reward(拓展KMP) HDU3613 Best Reward 3连发之KMP HDU3613 Best Reward 3连发之manacher HDU3613 Best Reward —— 扩展KMP HDU3613 Best Reward(扩展KMP) HDU3613 Best Reward(manacher+枚举) HDU3613 Best Reward 3连发之扩展KMP HDU3613-----Best Reward-----用扩展KMP来判断某个串是不是回文串 Best Reward Best Reward Best Reward Android学习笔记 OpenCV训练分类器制作xml文档 hibernate二级缓存 Head First JQuery学习笔记(一) getRadomeInDay hdu3613---Best Reward 设计模式心得:适配器模式(adapter pattern)和外观模式(facade pattern) jQuery文字和图片列表滚动插件 有关于HMAX的资料1 测试覆盖率工具 ext.net 如何传参数 codeforces-div1-285-D Two-stage rotation animation is deprecated. This application should use the smoother single-stage an Screen命令使用指南


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3