zoukankan      html  css  js  c++  java
  • hdu-3613 Best Reward (manacher算法)

    Best Reward
     

    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
     
    先manacher算法扫一遍,前缀和也扫一遍,暴力枚举前面的回文串中点,如果这个回文串的第一位的id不是1,那么这个回文串的值为0,不然就用前缀和算出前串总值,这样同理也可以得到后串的值,加起来以后就是把串分为两部分得到的值,然后和原来的答案比较。
     
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    const int maxn = 500100;
    char s[maxn];
    char txt[maxn<<1];
    int p[maxn<<1];
    int sum[maxn<<1];
    int len; int val = 0; int n;
    int v[26];
    int oo = 0x3f3f3f3f;
    
    void make() {
        txt[0] = '$';
        int pos = 0;
        for(int i = 0; i < len; i++) {
            txt[++pos] = '#';
            txt[++pos] = s[i];
        }txt[++pos] = '#';
        n = len * 2 + 2;
        txt[n] = '';
    }
    
    int get(int id) {
        if(txt[id] >= 'a' && txt[id] <= 'z') {
            return v[txt[id] - 'a'];
        }
        return 0;
    }
    
    
    void solve() {
        int maxx = 0, id;
        for(int i = 1; i < n; i++) {
            if(i < maxx)
                p[i] = min(p[id * 2 - i], p[id] + id - i);
            else p[i] = 1;
            while(txt[i - p[i]] == txt[i + p[i]]) {
                if(i + p[i] > maxx) {
                    maxx = i + p[i];
                    id = i;
                }
                p[i]++;
            }
        }
        for(int i = 1; i < n; i++) sum[i] = sum[i - 1] + get(i);
        for(int i = 1; i < n; i++) p[i]--;
        int ans = -oo, tsum = 0;
        for(int i = 2; i < n - 1; i++) {
            tsum = 0;
            int l, r;
            int newl, newr;
    
            if(p[i]) {
                l = i - p[i];
                r = i + p[i];
            }
            else {
                l = i - 1, r = i + 1;
            }
    
            if(l != 1 && r != len * 2 + 1) {
                tsum = 0;
                l = 1;
                newl = r, newr = len * 2 + 1;
            }
            else if(l != 1 && r == len * 2 + 1) {
                newl = 1, newr = l;
                tsum = sum[r] - sum[l];
            }
            else if(l == 1 && r != len * 2 + 1) {
                newl = r, newr = len * 2 + 1;
                tsum = sum[r] - sum[l];
            }
            else {
                tsum = -oo;
            }
    
            int newmid = (newl + newr) / 2;
            //printf("check %d %d %d %d %d %d %d
    ", l, r, newl, newr, tsum, ans, newmid);
            if(newmid - p[newmid] != 1 && newmid + p[newmid] != len * 2 + 1) {
                ans = max(ans, tsum);
                continue;
            }
            tsum += (sum[newr] - sum[newl]);
            ans = max(ans, tsum);
        }
        printf("%d
    ", ans);
    }
    
    int main() {
        int t;
        scanf("%d", &t);
        while(t--) {
            for(int i = 0; i < 26; i++) scanf("%d", &v[i]);
            scanf("%s", s);
            memset(p, 0, sizeof(p));
            memset(sum, 0, sizeof(sum));
            len = strlen(s);
            make();
            solve();
        }    
    }
     
  • 相关阅读:
    maven springMVC SSM框架中 出现的406 (Not Acceptable)
    eclipse中maven项目部署到tomcat
    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
    springMvc注解之@ResponseBody和@RequestBody
    springmvc后台接前端的参数,数组,集合,复杂对象等
    Spring项目JUnit测试报错ClassNotFoundException解决
    后台给前端返回图片
    前端js实现 blob转base64位 和 base64位转blob
    tomcat中实现特定路径下的图片的url访问Tomcat配置图片保存路径,图片不保存在项目路径下
    data:image/png;base64 上传图像将图片转换成base64格式
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5651556.html
Copyright © 2011-2022 走看看