HDU 3613 Best Reward(拓展KMP算法求解)

题目链接:

https://cn.vjudge.net/problem/HDU-3613

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.

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: v
1, v
2, ..., v
26 (-100 ≤ v
i ≤ 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 v
1, the value of ‘b‘ is v
2, ..., 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 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

解题思路:

问题的关键还是如何判断从某个位置分割开后,前缀和后缀是否是回文串

这次采用拓展kmp算法求解,关于拓展kmp的介绍请参考另一篇博客:https://www.cnblogs.com/wenzhixin/p/9355480.html

  先将s1反转赋值给s2,用s1去匹配s2,得到s2中每个后缀与s1的最长公共前缀数组extpre

            用s2去匹配s1,得到s1中每个后缀与s2的最长公共前缀数组extpos

  再枚举每一个分割点,判断,求值,更新答案取最优。

关键是如何使用extend数组,举个例子来说

abcde

当分割长度为2的时候,需要分别判断ab和bcde是否是回文串

先看ab,要看ab是否是回文串需要用到extpre数组,为什么用到它而不是另一个分析如下:

用s2去匹配s1制作的到extpre数组

s2 edcba

s1 abcde

要判断ab是否是回文串,就是看ba与s1的最长公共前缀是多少,如果恰好是ba的长度,就说明ba在原串中存在。也即extpre[ls - i] == i(其中ls是串的总长度)。同时又有两者逆序,必然是回文串。

再看bcde是否是回文串

用s1去匹配s2制作的到extpos数组

s1 abcde

s2 edcba

要判断bcde是否是回文串,就要看bcde与s2的最长公共前缀是多少,如果恰好是dcde的长度,就说明bcde在反串中存在。也即extpre[i] == ls - i(其中ls是串的总长度)。同时又有两者逆序,必然是回文串。

这样就完成了,是否是回文串的判断。

代码实现:

 1 #include<cstdio>
 2 #include<cstring>
 3
 4 const int maxn = 500010;
 5 int val[26], presum[maxn], next[maxn], extpre[maxn], extpos[maxn];
 6 char s1[maxn], s2[maxn];
 7
 8 void exkmp(char s[], char t[], int len, int ex[]);
 9 void get_next(char t[], int len);
10
11 int main()
12 {
13     int T;
14     scanf("%d", &T);
15
16     while(T--){
17         for(int i = 0; i < 26; i++){
18             scanf("%d", &val[i]);
19         }
20         scanf("%s", s1);
21         int ls = strlen(s1);
22         presum[0] = 0;
23         for(int i = 0;i < ls; i++){
24             s2[ls - 1 - i] = s1[i];
25             presum[i + 1] = presum[i] + val[s1[i] - ‘a‘];
26         }
27         s2[ls] = ‘\0‘;
28
29         exkmp(s2, s1, ls, extpre);//拿s1去匹配s2,得到s2中每个后缀与s1的最长公共前缀数组extpre
30         exkmp(s1, s2, ls, extpos);//拿s2去匹配s1,得到s1中每个后缀与s2的最长公共前缀数组extpos
31
32         int ans = -99999999;
33         for(int i = 1; i <= ls - 1; i++){//在长度为 i 处分割
34             int sum = 0;
35             if(extpre[ls - i] == i)
36                 sum += presum[i];
37             if(extpos[i] == ls - i)
38                 sum += presum[ls] - presum[i];
39             if(sum > ans)
40                 ans = sum;
41         }
42         printf("%d\n", ans);
43     }
44     return 0;
45 }
46
47 void get_next(char t[], int len){
48     next[0] = len;
49     int k = 1;
50     while(k < len && t[k] == t[k - 1])
51         k++;
52     next[1] = k;
53
54     int po = 1;
55     for(k = 2; k< len; k++){
56         if(next[k - po] + k < next[po] + po)
57             next[k] = next[k - po];
58         else{
59             int j = next[po] + po - k;
60             if(j < 0)
61                 j = 0;
62             while(k + j < len && t[k] == t[k + j])
63                 j++;
64
65             next[k] = j;
66             po = k;
67         }
68     }
69
70 }
71
72 void exkmp(char s[], char t[], int len, int ex[])
73 {
74     memset(next, 0, sizeof(next));
75     get_next(t, len);
76
77     int k=0;
78     while(k < len && s[k] == t[k])
79         k++;
80     ex[0] = k;
81
82     int po = 0;
83     for(k = 1; k< len; k++){
84         if(next[k - po] + k < ex[po] + po)
85             ex[k] = next[k - po];
86         else{
87             int j = ex[po] + po - k;
88             if(j < 0)
89                 j = 0;
90
91             while(k + j < len && j < len && s[k + j] == t[j])
92                 j++;
93             ex[k] = j;
94             po = k;
95         }
96     }
97 }

原文地址:https://www.cnblogs.com/wenzhixin/p/9355880.html

时间: 2024-08-10 23:17:12

HDU 3613 Best Reward(拓展KMP算法求解)的相关文章

hdu 3613 Best Reward (manachar算法)

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

HDU 3613 Best Reward 正反两次扩展KMP

题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考虑扩展KMP 输出a串 得到a的反串b 求出f[0]和f[1] 和 extend[0]和extend[1] 正反求2次 枚举位置i 分成2部分0到i-1 和i到n-1 由于分成的2部分必须组成原字符串 就是不能多也不能少 那么推断i+extend[i]是否等于n 等于说明i到n-1这个部分是回文串

拓展KMP算法 入门+模板

拓展KMP算法入门 博客推荐 扩展KMP算法, 图很形象,代码写的也很清晰,下面的模板就是出自该博客文章. 拓展KMP是求母串S长度为n和子串T长度为m,求S的每一个后缀子串与T的前缀子串匹配的最长长度. 代码实现 //求解模式串T的next数组,这个函数和下面的函数几乎相同 void getnext(string &T, int m, int[] next) { int a = 0, p = 0; next[0] = m; //T字符串自身和自身匹配 for(int i=1; i<m; i

拓展kmp算法总结

算法总结第二弹,上次总结了下kmp,这次就来拓展kmp吧. 拓展kmp是对KMP算法的扩展,它解决如下问题: 定义母串S,和字串T,设S的长度为n,T的长度为m,求T与S的每一个后缀的最长公共前缀,也就是说,设extend数组,extend[i]表示T与S[i,n-1]的最长公共前缀,要求出所有extend[i](0<=i<n). 注意到,如果有一个位置extend[i]=m,则表示T在S中出现,而且是在位置i出现,这就是标准的KMP问题,所以说拓展kmp是对KMP算法的扩展,所以一般将它称为

拓展KMP算法详解

拓展KMP解决的问题是给两个串S和T,长度分别是n和m,求S的每一个后缀子串与T的最长公共前缀分别是多少,记作extend数组,也就是说extend[i]表示S[i,n-1](i从0开始)和T的最长公共前缀长度. 需要注意的是如果extend[i]=m,即S[i,n-1]和T的最长公共前缀长度是m(正好是T的长度),那么就表示T在S中找到匹配而且起始位置是i,这就解释了为什么这个算法叫做拓展KMP了. 其实大致和KMP有异曲同工之妙,都是匹配,都是借用一个next数组. 下面举一个例子,S="a

HDU - 3613 Best Reward(manacher或拓展kmp)

传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Manacher:先记录下第i个字符的价值,然后求前缀和.然后遍历分的位置,分别判断前半段和后半段是否为回文串,是回文串的加上这段的价值(前缀和相减),更新最大价值. 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int p[1000100],v

HDU 3613 Best Reward(求前后缀回文 拓展KMP or Manacher)

题目大意: 给个字符串X,要把X分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串(从左往右或者从右往左读,都一样),那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问最多能获得多少价值? 思路: 把字符串X逆序后得到字符串Y 让X去匹配Y ,匹配的长度满足extend[i] + i == len,  len=|X|.    的那么X与y的匹配部分是回文串,这不难理解,画图即可 总复杂度是O(n),由于这是求前缀和后缀的回文,用

扩展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 2594 java实现字符串KMP算法

Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? Marge: OK. Homer: So I tak