HDOJ 3613 Best Reward --- 扩展KMP算法求前(后)缀回文串

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3613

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

 题意:

给定一个字符串比如abacac,每个字符都有一个价值,将字符串拆成2部分,每部分如果是回文串则总价值为各字符价值之和,否则总价值为0。 求总价值最大。

比如所有字符价值为1,

可以拆成 a,bacac->总价值为0,  ab,acac->价值0, aba,cac->价值为6......

最后算出总价值最大为6.

题解:

这个题实在让我吐血,在网上看扩展kmp看了半天才看懂,然后看别人代码看半天才知道怎么做,然后自己太菜写代码又写了一个多小时,最后因为有2条语句不小心写错,debug了很久,现在终于能AC了,lz差点崩溃了。。。

以上都是废话

题解就是先前缀和数组预处理一遍价值

然后很关键的一步就是求前(后)缀回文串,直接用源串对逆串做一遍扩展KMP,去判断extend数组中extend[i]是否等于len-i(i从0开始算,len是字符串长度),等于的话后缀串s[i...len-1]就是回文。

前缀回文反一遍过来就是,对逆串和源串做扩展KMP,道理和上面一样的。

最后就是for循环扫一遍字符串,将其拆成2部分,分别判断前后部分是否是回文串然后用之前算的前缀和数组求价值就OK了。

 

#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAXN 500005
using namespace std;
int value[27];
char input[MAXN],s[MAXN],t[MAXN];// 输入,源串,逆串 
int extend[MAXN],Next[MAXN];
int prefix[MAXN];// 前缀和数组
bool suffix_palindrome[MAXN],prefix_palindrome[MAXN];// 前后缀回文标记 
void CalNext() {
    int len1 = strlen(t);
    int len2 = len2;
    memset(Next,0,sizeof(Next));
    Next[0] = len1;
    int a = 1,p = 1;// a不能初始化为0,不然后面Next[i-a]就直接用了还没算出来的值 
    int j;
    for(int i = 1;i < len1;i++) {
        if(i < p && (Next[i-a]+i) < p) {
            Next[i] = Next[i-a];
        } else {
            j = p - i;
            j = max(0,j);
            while(t[i+j] == t[j] && (i+j) < len1) j++;
            Next[i] = j;
            a = i,p = i + j;   // 更新a和p 
        }
    }
    
}

// 扩展KMP算法求extend数组
void Ex_Kmp() {
    int len1 = strlen(s);
    int len2 = strlen(t);
    CalNext();
    memset(extend,0,sizeof(extend));
    int pos = 0;
    // 计算extend[0]
    while(s[pos] == t[pos] && pos < len1 && pos < len2) {
        pos++;
    }
    extend[0] = pos;
    int a = 0,p = pos;
    int j;
    for(int i = 1;i < len1;i++) {
        if(i < p && (Next[i-a]+i) < p) {
            extend[i] = Next[i-a];
        } else {
            j = p - i;
            j = max(0,j);
            while(s[i+j] == t[j] && (i+j) < len1 && j < len2) j++;
            extend[i] = j;
            a = i,p = i + j;   // 更新a和p 
        }
    }
    
}

int main()
{
    int num;
    scanf("%d",&num);
    while(num--) {
        for(int i = 0;i < 26;i++) {
            scanf("%d",&value[i]);
        }
        scanf("%s",input);
        // 求前缀和
        memset(prefix,0,sizeof(prefix));
        int len = strlen(input);
        prefix[0] = value[input[0]-'a'];
        for(int i = 1;i < len;i++) {
            prefix[i] = prefix[i-1] + value[input[i]-'a'];
        }
        // 求后缀回文串
        strcpy(s,input);
        reverse(input,input+len);
        strcpy(t,input);
        Ex_Kmp();
        for(int i = 0;i < len;i++) {
            suffix_palindrome[i] = ( (extend[i]==len-i) ? true : false);
        }
        // 求前缀回文串
        strcpy(s,input);
        reverse(input,input+len);
        strcpy(t,input);
        Ex_Kmp();
        for(int i = 0;i < len;i++) {
            prefix_palindrome[i] = ( (extend[len-1-i]==1+i) ? true : false);
        }
        int ret = 0;
        for(int i = 1;i < len;i++) {
            int tmp = 0;
            if(suffix_palindrome[i]) tmp += prefix[len-1] - prefix[i-1];
            if(prefix_palindrome[i-1]) tmp += prefix[i-1];
            ret = max(ret,tmp);
        }
        printf("%d\n",ret);
    }
   return 0;
} 

 

阅读更多
换一批

Best Reward

11-05

Problem DescriptionnAfter 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. nnOne 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.) nnIn 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. nnAll 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. nnNow the problem is: how to cut the given necklace so that the sum of the two necklaces's value is greatest. Output this value. nn nnInputnThe first line of input is a single integer T (1 ≤ T ≤ 10) - the number of test cases. The description of these test cases follows. nnFor 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. nnThe 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. nn nnOutputnOutput a single Integer: the maximum value General Li can get from the necklace.n nnSample Inputn2n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1naban1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1nacacacn nnSample Outputn1n6

没有更多推荐了,返回首页