首页 > 其他 > 详细

hdu3613 Best Reward【Manacher】

时间:2019-01-12 15:54:16      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:algorithm   left   cpc   however   first   php   maximum   greate   long long   

Best Reward

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


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
 

 

Recommend
lcy

 

题意:

给定一个字符串,以及每个字母拥有的价值。现在要把这个字符串切成两半,如果子串是回文,那么就可以加上这个子串的字符价值之和,如果不是回文这个子串的价值就是0.

现在要求两个子串相加的最大价值。

思路:

用Manacher我们可以处理出,以某个节点$i$为中心时的回文串长度, 即$p[i]-1$。

如果暴力枚举分割点,如果子串的中心的$p[i] - 1$正好是子串的长度,那么说明这个子串是一个回文串。

这里由于我们要算每个字符的价值之和,可以预处理前缀和。

对于偶数长度的串和奇数长度的串分别求对应的价值。

 

 1 //#include<bits/stdc++>
 2 #include<stdio.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<stdlib.h> 
 7 
 8 #define LL long long
 9 #define ull unsigned long long
10 #define inf 0x3f3f3f3f 
11 
12 using namespace std;
13 
14 int t;
15 int val[30];
16 const int maxlen = 5e5 + 5;
17 char s[maxlen], s_new[maxlen * 2];
18 int s_val[maxlen], p[maxlen * 2];
19 
20 int init()
21 {
22     int len = strlen(s + 1);
23     s_new[0] = $;
24     s_new[1] = #;
25     int j = 2;
26     for(int i = 1; i <= len; i++){
27         s_new[j++] = s[i];
28         s_new[j++] = #;
29     }
30     s_new[j] = \0;
31     return j;
32 }
33 
34 void manacher()
35 {
36     int len = init();
37     int id, mx = 0;
38     
39     for(int i = 1; i < len; i++){
40         if(i < mx)p[i] = min(p[2 * id - i], mx - i);
41         else p[i] = 1;
42         while(s_new[i - p[i]] == s_new[i + p[i]])p[i]++;
43         if(mx < i + p[i]){
44             id = i;
45             mx = i + p[i];
46         }
47     }
48 }
49 
50 int main()
51 {
52     scanf("%d", &t);
53     while(t--){
54         memset(p, 0, sizeof(p));
55         for(int i = 0; i < 26; i++){
56             scanf("%d", &val[i]);
57         }
58         scanf("%s", s + 1);
59         
60         int len = strlen(s + 1);
61         for(int i = 1; i <= len; i++){
62             s_val[i] = s_val[i - 1] + val[s[i] - a];
63         }
64         manacher();
65 //        cout<<s_new<<endl;
66 //        for(int i = 0; i <= len * 2; i++){
67 //            cout<<p[i]<<" ";
68 //        }
69 //        cout<<endl;
70         
71         int ans = 0;
72         for(int i = 1; i < len; i++){
73             int tmp = 0;
74             if(p[i + 1] - 1 == i){
75                 if(i % 2){
76                     tmp += s_val[i / 2] * 2 + val[s[(i + 1) / 2] - a];
77                 } 
78                 else{
79                     tmp += s_val[i / 2] * 2;
80                 }
81             }
82             if(p[i + len + 1] - 1 == len - i){
83                 if((len - i) % 2){
84                     tmp += (s_val[i + (len - i) / 2] - s_val[i]) * 2 + val[s[i + (len + 1- i) / 2] - a];
85                 }
86                 else{
87                     tmp += (s_val[i + (len - i) / 2] - s_val[i]) * 2;
88                 }
89             } 
90             ans = max(ans, tmp);
91         }
92         printf("%d\n", ans); 
93     }
94     
95 }

 

hdu3613 Best Reward【Manacher】

标签:algorithm   left   cpc   however   first   php   maximum   greate   long long   

原文:https://www.cnblogs.com/wyboooo/p/10259959.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号