zoukankan      html  css  js  c++  java
  • 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
    ", ans);
        }
    
        return 0;
    }
    
  • 相关阅读:
    冒泡排序
    linux常用命令
    Github上将公共仓库转为私有仓库or私有仓库转为共有仓库
    使用apt更新和升级系统软件
    Django用户认证模块中继承AbstractUser与AbstractBaseUser重写User表的区别
    详解django中的collectstatic命令以及STATIC_URL、STATIC_ROOT配置
    python入门指南
    python包装不上?国内网络问题,使用豆瓣源解决
    nginx入门
    Vue 实现页面刷新(provide 和 inject)
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11601890.html
Copyright © 2011-2022 走看看