hu3613 Best Reward

地址:http://acm.hdu.edu.cn/showproblem.php?pid=3613

题目:

Best Reward

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

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

2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU

Recommend

lcy

思路:扩展kmp

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5
 6 using namespace std;
 7 const int K=1e6+7;
 8 int nt[K],extand[K],v[30],sum[K],l[K],r[K];
 9 char sa[K],sb[K];
10 void Getnext(char *T,int *next)
11 {
12     int len=strlen(T),a=0;
13     next[0]=len;
14     while(a<len-1 && T[a]==T[a+1]) a++;
15     next[1]=a;
16     a=1;
17     for(int k=2; k<len; k++)
18     {
19         int p=a+next[a]-1,L=next[k-a];
20         if( (k-1)+L >= p)
21         {
22             int j = (p-k+1)>0 ? (p-k+1) : 0;
23             while(k+j<len && T[k+j]==T[j]) j++;
24             next[k]=j;
25             a=k;
26         }
27         else
28             next[k]=L;
29     }
30 }
31 void GetExtand(char *S,char *T,int *next)
32 {
33     Getnext(T,next);
34     int slen=strlen(S),tlen=strlen(T),a=0;
35     int MinLen = slen < tlen ? slen : tlen;
36     while(a<MinLen && S[a]==T[a]) a++;
37     extand[0]=a;
38     a=0;
39     for(int k=1; k<slen; k++)
40     {
41         int p=a+extand[a]-1, L=next[k-a];
42         if( (k-1)+L >= p)
43         {
44             int j= (p-k+1) > 0 ? (p-k+1) : 0;
45             while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
46             extand[k]=j;
47             a=k;
48         }
49         else
50             extand[k]=L;
51     }
52 }
53 int main(void)
54 {
55     int t;cin>>t;
56     while(t--)
57     {
58         int ans=0;
59         for(int i=0;i<26;i++)
60             scanf("%d",v+i);
61         scanf("%s",sa);
62         int len=strlen(sa);
63         for(int i=0;i<len;i++)
64             sb[i]=sa[len-i-1];
65         sum[0]=v[sa[0]-‘a‘];
66         for(int i=1;i<len;i++)
67             sum[i]=sum[i-1]+v[sa[i]-‘a‘];
68         sb[len]=‘\0‘;
69         GetExtand(sb,sa,nt);
70         for(int i=0;i<len;i++)
71         if(extand[i]==len-i) l[len-i-1]=1;
72         else l[len-i-1]=0;
73         GetExtand(sa,sb,nt);
74         for(int i=0;i<len;i++)
75         if(extand[i]==len-i) r[i]=1;
76         else r[i]=0;
77         for(int i=0;i<len-1;i++)
78         {
79             int tmp=0;
80             if(l[i])   tmp+=sum[i];
81             if(r[i+1])   tmp+=sum[len-1]-sum[i];
82             ans=max(ans,tmp);
83         }
84         printf("%d\n",ans);
85     }
86     return 0;
87 }
时间: 2024-10-06 21:50:26

hu3613 Best Reward的相关文章

HDU3613 Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2028    Accepted Submission(s): 819 Problem Description After an uphill battle, General Li won a great victory. Now the head of state decide to re

hdoj 2647 Reward

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5940    Accepted Submission(s): 1827 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

HDU2647 Reward(拓扑排序)反向建图

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4638    Accepted Submission(s): 1416 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

hdu-3613 Best Reward (manacher算法)

Best Reward 题目链接 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 gems

hdu 3613 Best Reward

Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)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

扩展KMP --- HDU 3613 Best Reward

Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权值(可能为负),让你将这个字符串分成两个字串,使得这两个子串的价值之和最大.一个子串价值的计算方法:如果这个子串是回文串,那么价值就是这个子串所有字符权值之和:否则价值为0. analyse: 经典的扩展KMP算法运用. 假设输入串为s,那么我们首先:strcpy(s1,s)     ;      

HDU 2647 Reward(拓扑排序)

Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,and some

hdu 2647 Reward (拓扑排序分层)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3815    Accepted Submission(s): 1162 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

HDU 2647 Reward

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9918    Accepted Submission(s): 3165 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa