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;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值