hdu3613(扩展KMP)
生活随笔
收集整理的這篇文章主要介紹了
hdu3613(扩展KMP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用kmp的方法寫了好久,發現以我的想法用kmp不僅難以實現而且無法證明正確性。 于是還是使用擴展kmp。。。
Best Reward
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 378????Accepted Submission(s): 151
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 2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU?
Recommend lcy?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <math.h> #include <map> #include <queue> #include <sstream> #include <iostream> using namespace std; #define INF 0x3fffffff #define N 500500char g[N],g1[N]; int next[N]; int chg[30]; int len; int sum[N]; int mark[N],mark1[N]; int num[N];void build(char s[]) {memset(next,0,sizeof(next));int p=0,a=0;next[0]=len;for(int i=1;i<len;i++){if(i+next[i-a]-1<p){next[i]=next[i-a];}else{int k=p-i;while(p+1<len && s[p+1]==s[k+1]){p++; k++;}p=max(p,i);next[i]=k+1;a=i;}} }void extend_kmp(char s[],char t[]) {memset(sum,0,sizeof(sum));int p=-1,a=0;for(int i=0;i<len;i++){if(i+next[i-a]-1<p){sum[i]=next[i-a];}else{int k=p-i;while(p+1<len && s[p+1]==t[k+1]){p++; k++;}p=max(p,i);sum[i]=k+1;a=i;}} }int main() {//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);//freopen("//home//chen//Desktop//ACM//out.text","w",stdout);int T;scanf("%d",&T);while(T--){for(int i=0;i<26;i++)scanf("%d",chg+i);scanf("%s",g);len=strlen(g);for(int i=0;i<len;i++){g1[len-1-i]=g[i];}build(g);extend_kmp(g1,g);memset(mark,0,sizeof(mark));memset(num,0,sizeof(num));int tmp=0;for(int i=0;i<len;i++){tmp+=chg[g[i]-'a'];num[i]=tmp;}for(int i=0;i<len-1;i++){if(sum[len-1-i]==i+1){mark[i]=1;}}memset(mark1,0,sizeof(mark1));build(g1);extend_kmp(g,g1);for(int i=0;i<len-1;i++){if(sum[len-1-i]==i+1){mark1[i]=1;}}int mx=-INF;for(int i=0;i<len-1;i++){tmp=0;int k=len-i-2;if(mark[i]==1)tmp+=num[i];if(mark1[k]==1)tmp+=num[len-1]-num[i];mx=max(mx,tmp);}printf("%d\n",mx);}return 0; }?
總結
以上是生活随笔為你收集整理的hdu3613(扩展KMP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最长公共子字符串
- 下一篇: 向大家推荐几本数据库入门的书