HDU3613 Best Reward 3连发之KMP

题目链接:HDU3613

Best Reward

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


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: 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. 

 

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   |   We have carefully selected several similar problems for you:   3618  3620  3614  3615  3616 



题意:每个字符都有一个权值,现给一个字符串要分成两半,如果某一半是回文串就把所有的字符权值加起来,否则当0来处理,问最大值会是多少。

题目分析:之前在网上看到某大牛用三种常用的方法A掉这一题,于是我也试着分别用这些算法做一做。首先是KMP,其next数组求的是最长公共前后缀,所以把正常串储存在s1中,再倒过来储存在s2中,分别把他俩作为模式串和主串进行匹配,得到的一定是从头开始或是从尾开始的回文串长度(手动模拟下就出来了)。所以每次取next[k]为比k小的下一个前缀(后缀)回文串记录下来,然后加取最大值就可以了。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 999999999
using namespace std;
const int maxn=500010;
char s1[maxn],s2[maxn];
int nexts[maxn],pos[maxn],per[maxn],val[27],sum[maxn];
int len;
void getnext(char *s)
{
    int i=0,j=-1;
    nexts[0]=-1;
    while(i<len)
    {
        if(j==-1||s[i]==s[j])
        {
            nexts[++i]=++j;
        }
        else j=nexts[j];
    }
}

int kmp(char *t,char *s)
{
    int i=0,j=0;
    while(i<len&&j<len)
    {
        if(j==-1||s[i]==t[j])
        {
            i++;j++;
        }
        else j=nexts[j];
    }
    return j;
}

int main()
{
    int n,k;
    cin>>n;
    while(n--)
    {
        for(int i=0;i<27;++i) scanf("%d",&val[i]);
        scanf("%s",s1);
        len=strlen(s1);
        for(int i=0;i<len;++i)
        {
            sum[i+1]=sum[i]+val[s1[i]-'a'];
            s2[i]=s1[len-i-1];
        }
        getnext(s1);
        k=kmp(s1,s2);
        while(k) per[k]=n+1,k=nexts[k];
        getnext(s2);
        k=kmp(s2,s1);
        while(k) pos[k]=n+1,k=nexts[k];
        int ans=-inf,num=0;
        for(int i=1;i<len;++i)
        {
            if(per[i]==n+1)num+=sum[i];
            if(pos[len-i]==n+1) num+=sum[len]-sum[i];
            if(num>ans) ans=num;
            num=0;
        }
        if(ans<=0) ans=0;
        cout<<ans<<endl;
    }
}


你可能感兴趣的:(KMP)