HDU-3613-Best Reward(Manacher, 前缀和)

链接:

https://vjudge.net/problem/HDU-3613

题意:

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.

思路:

跑一边马拉车, 对马拉车修改后的字符串跑前缀和.
枚举切割点即可.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 5e5+10;

int hw[MAXN*2], val[30], Sum[MAXN*2];
char s[MAXN], ss[MAXN*2];

void Manacher(char *str)
{
    int maxr = 0, mid;
    int len = strlen(str);
    for (int i = 1;i < len;i++)
    {
        if (i < maxr)
            hw[i] = min(hw[mid*2-i], maxr-i);
        else
            hw[i] = 1;
        while (str[i+hw[i]] == str[i-hw[i]])
            hw[i]++;
        if (hw[i]+i > maxr)
        {
            maxr = hw[i]+i;
            mid = i;
        }
    }
}

void Change(char *str, char *to)
{
    to[0] = to[1] = '#';
    int len = strlen(str);
    for (int i = 0;i < len;i++)
    {
        to[i*2+2] = str[i];
        to[i*2+3] = '#';
    }
    to[len*2+2] = 0;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        for (int i = 0;i < 26;i++)
            scanf("%d", &val[i]);
        scanf("%s", s);
        Change(s, ss);
        Manacher(ss);
        int len = strlen(ss);
        Sum[0] = Sum[1] = 0;
        for (int i = 2;i < len;i++)
        {
            if (ss[i] == '#')
                Sum[i] = Sum[i-1];
            else
                Sum[i] = Sum[i-1]+val[ss[i]-'a'];
        }
        int ans = -1e8;
        for (int i = 3;i <= len-2;i += 2)
        {
            int lmid = (i+1)/2, rmid = (len-1+i)/2;
            int tmp = 0;
            if (hw[lmid] == lmid)
                tmp += Sum[i];
            if (hw[rmid]+rmid == len)
                tmp += Sum[len-1]-Sum[i];
            ans = max(ans, tmp);
        }
        printf("%d\n", ans);
    }

    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/11601890.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值