HDU 3613Best Reward(扩展KMP解法)

Best Reward

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


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
 

                    题目大意:题目意思还是比较容易懂的。小写字母a~z组成的长n的串, 每种字母有一定的价值(可以为负), 要你分成切成两个串, 总价值为两个串价值和, 若是回文, 则串的价值为每个字母价值和, 否则为0。 问最大价值多少。

            解题思路:可以将原串逆序到另一串,然后两个串相互EKMP匹配,比如str1与str2。先看str2的后半部分能否和str1前面的完全匹配,如果可以tmp+。再判断这个的时候还要判断str1的后面能否和str2前面是否匹配,这就需要进行两次的EKMP。具体详见代码。思路来源于kuangbin.

            题目地址:Best Reward

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;

char str1[500005],str2[500005];
int next[500005],extend1[500005],extend2[500005];
int v[26],sum[500005];
//扩展KMP是用来求主串每个点可以向后延伸与模式串匹配的最长的长度
void EKMP(char t[],char s[],int next[],int extend[])
{   //t为模式串,s为主串
    int i,j,p,a,b;
    int lent=strlen(t);
    int lens=strlen(s);
    next[0]=lent; j=0;
    while(j+1<lent&&t[j]==t[j+1])
        j++;
    next[1]=j;  //先是模式串自匹配

    a=1;
    for(i=2;i<lent;i++)
    {
        p=next[a]+a-1;
        b=next[i-a];
        if(i+b<p+1)
            next[i]=b;
        else
        {
            j=max(0,p-i+1);
            while(i+j<lent&&t[i+j]==t[j])
                j++;
            next[i]=j;
            a=i;
        }
    }

    j=0;
    while(j<lens&&j<lent&&s[j]==t[j])
        j++;
    extend[0]=j;
    a=0;
    for(i=1;i<lens;i++)
    {
        p=extend[a]+a-1;
        b=next[i-a];
        if(i+b<p+1)
            extend[i]=b;
        else
        {
            j=max(0,p-i+1);
            while(i+j<lens&&j<lent&&s[i+j]==t[j])
                j++;
            extend[i]=j;
            a=i;
        }
    }
}

int main()
{
    int tes,i;
    scanf("%d",&tes);
    while(tes--)
    {
        for(i=0;i<26;i++)
            scanf("%d",&v[i]);
        scanf("%s",str1);
        int len=strlen(str1);
        sum[0]=0;
        for(i=0;i<len;i++)
        {
            str2[i]=str1[len-1-i];
            sum[i+1]=sum[i]+v[str2[i]-'a'];
        }
        str2[len]='\0';
        EKMP(str1,str2,next,extend1);  //str1是模式串
        EKMP(str2,str1,next,extend2);  //str2是模式串
        int res=-1000000;
        for(i=1;i<len;i++)  //0~i-1与i~len-1
        {
            int tmp=0;
            //对串2来说,前len-i回文
            if(i+extend2[i]>=len)
                tmp+=sum[len-i];
            int pos=len-i;
            //对串2来说,后i个回文
            if(pos+extend1[pos]>=len)
                tmp+=sum[len]-sum[pos];
            if(tmp>res)
                res=tmp;
        }

        printf("%d\n",res);
    }
    return 0;
}



             

你可能感兴趣的:(EKMP)