HDU3613 Best Reward —— 扩展KMP

来源:互联网 发布:知乎女神王诺诺是谁 编辑:程序博客网 时间:2024/06/06 03:36

题目链接: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): 2644    Accepted Submission(s): 1072


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
21 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1aba1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1acacac
 

Sample Output
16



题解:




代码一(下标从0开始):

#include<bits/stdc++.h>#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const double EPS = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 5e5+10;void pre_EXKMP(char x[], int m, int Next[]){    Next[0] = m;    int j = 0;    while(j+1<m && x[0+j]==x[1+j]) j++;    Next[1] = j;    int k = 1;    for(int i = 2; i<m; i++)    {        int p = Next[k]+k-1;        int L = Next[i-k];        if(i+L<=p) Next[i] = L;        else        {            j = max(0, p-i+1);            while(i+j<m && x[i+j]==x[0+j]) j++;            Next[i] = j;            k = i;        }    }}void EXKMP(char x[], int m, char y[], int n, int Next[], int exd[]){    pre_EXKMP(x,m,Next);    int j = 0;    while(j<n && j<m && x[j]==y[j]) j++;    exd[0] = j;    int k = 0;    for(int i = 1; i<n; i++)    {        int p = exd[k]+k-1;        int L = Next[i-k];        if(i+L<=p) exd[i] = L;        else        {            j = max(0,p-i+1);            while(i+j<n && j<m && y[i+j]==x[0+j]) j++;            exd[i] = j;            k = i;        }    }}char s1[MAXN], s2[MAXN];int val[30], Next[MAXN], exd1[MAXN], exd2[MAXN], sum[MAXN];int main(){    int T;    scanf("%d",&T);    while(T--)    {        rep(i,0,25) scanf("%d",&val[i]);        scanf("%s",s1);        int len = strlen(s1);        rep(i,1,len)        {            sum[i] = sum[i-1] + val[s1[i-1]-'a'];            s2[len-i] = s1[i-1];        }        EXKMP(s1, len, s2, len, Next, exd1);        EXKMP(s2, len, s1, len, Next, exd2);        int ans = 0;        for(int i = 1; i<len; i++)        {            int tmp = 0;            if(exd1[len-i]==i) tmp += sum[i];            if(exd2[i]==len-i) tmp += sum[len]-sum[i];            ans = max(ans,tmp);        }        printf("%d\n", ans);    }}



代码二(下标从1开始):

#include<bits/stdc++.h>#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const double EPS = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 5e5+10;void pre_EXKMP(char x[], int m, int Next[]){    Next[1] = m;    int j = 0;    while(2+j<=m && x[1+j]==x[2+j]) j++;    Next[2] = j;    int k = 2;    for(int i = 3; i<=m; i++)    {        int p = Next[k]+k-1;        int L = Next[i-k+1];        if(i+L<=p) Next[i] = L;        else        {            j = max(0, p-i+1);            while(i+j<=m && x[i+j]==x[1+j]) j++;            Next[i] = j;            k = i;        }    }}void EXKMP(char x[], int m, char y[], int n, int Next[], int exd[]){    pre_EXKMP(x,m,Next);    int j = 1;    while(j<=n && j<=m && x[j]==y[j]) j++;    exd[1] = j-1;    int k = 1;    for(int i = 2; i<=n; i++)    {        int p = exd[k]+k-1;        int L = Next[i-k+1];        if(i+L<=p) exd[i] = L;        else        {            j = max(0, p-i+1);            while(i+j<=n && j<=m && y[i+j]==x[1+j]) j++;            exd[i] = j;            k = i;        }    }}char s1[MAXN], s2[MAXN];int val[30], Next[MAXN], exd1[MAXN], exd2[MAXN], sum[MAXN];int main(){    int T;    scanf("%d",&T);    while(T--)    {        for(int i = 0; i<26; i++)            scanf("%d",&val[i]);        scanf("%s",s1+1);        int len = strlen(s1+1);        for(int i = 1; i<=len; i++)        {            sum[i] = sum[i-1] + val[s1[i]-'a'];            s2[len-i+1] = s1[i];        }        EXKMP(s1, len, s2, len, Next, exd1);        EXKMP(s2, len, s1, len, Next, exd2);        int ans = 0;        for(int i = 1; i<=len-1; i++)        {            int tmp = 0;            if(exd1[len-i+1]==i) tmp += sum[i];            if(exd2[i+1]==len-i) tmp += sum[len]-sum[i];            ans = max(ans,tmp);        }        printf("%d\n", ans);    }}


原创粉丝点击