hdu 3613 manacher算法
来源:互联网 发布:js触发事件 自动 编辑:程序博客网 时间:2024/05/17 09:23
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
题解:
做这道题更加熟悉了mancher算法的应用。
思路:先用Manacher算法求出以每个字母为中心的回文串的长度,并计算该字符串的前缀价值和。然后枚举切割点,得到两份
子串。这样就可以知道每个子串的中心点,然后检查以该子串的中心点作为中心点的回文串的长度,如果长度等于该子串的长度,
那么就加上该子串的价值。然后和最优价值比较就行了。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 500005;char str[2*maxn];int p[2*maxn],sum[2*maxn],val[27];int pre[maxn],pos[maxn];int main(){ int T; scanf("%d",&T); while(T--) { memset(pre,0,sizeof(pre)); memset(pos,0,sizeof(pos)); for(int i=0;i<26;i++) { scanf("%d",&val[i]); } scanf("%s",str); int len = strlen(str),id=0,mx=0; int ans = -INF,tmp=0; for(int i=1;i<=len;i++) sum[i] = sum[i-1]+val[str[i-1]-'a']; for(int i=len;i>=0;--i) { str[i+i+2]=str[i]; str[i+i+1]='#'; } str[0]='*'; str[2*len+2]='\0'; for(int i=2;str[i];i++) { p[i]=mx>i?min(p[2*id-i],mx-i):1; while(str[i+p[i]]==str[i-p[i]]) { p[i]++; } if(i+p[i]>mx) { mx = i+p[i]; id = i; } if(i-p[i]==0)//前缀是回文串 { pre[p[i]-1]=1; } if(i+p[i]==2*len+2)//后缀是回文串 { pos[p[i]-1]=1; } } for(int i=1;i<len;i++) { if(pre[i]) tmp+=sum[i]; if(pos[len-i]) tmp+=sum[len]-sum[i]; if(tmp>ans) ans = tmp; tmp=0; } printf("%d\n",ans); } return 0;}
- HDU 3613(Manacher算法)
- HDU 3613 Manacher算法
- hdu 3613 manacher算法
- hdu 3613 Best Reward(manacher算法)
- hdu 3294 manacher算法
- hdu 3068 manacher算法
- Hdu 5371 Manacher算法
- HDU 5371 manacher算法
- hdu 最大回文 Manacher算法
- hdu-3068 Manacher算法模板
- HDU 3613 Best Reward(KMP+思维||manacher算法)
- HDU 3613 Manacher应用
- HDU 3613 Manacher应用
- HDU 3613 Manacher
- Hdu 3068 最长回文 (manacher算法)
- HDU 4513 回文串Manacher算法
- hdu 3068 最长回文 (manacher算法)
- HDU 3294 Girls' research 字符串-manacher算法
- makefile 使用总结(精)
- java EE开发之Servlet第五课:xml解析
- 一些好用的css和js
- ubuntu16.04上基于 VirtualEnv 安装tensorflow
- 【Android】实现开7分钟后关3分钟无限循环
- hdu 3613 manacher算法
- 微信公众平台开发 获取用户基本信息
- kmemdup
- [SDUT](2134)数据结构实验之栈四:括号匹配 ---栈
- 在Vue项目中使用easyScroll滚动条插件
- HTTPS握手过程与对称加密、非对称加密
- PullToRefreshBase添加头部视图
- shell脚本实例
- Linux下限制IP访问