• (回文串 )Best Reward -- hdu -- 3613


    http://acm.hdu.edu.cn/showproblem.php?pid=3613

    Best Reward

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1210    Accepted Submission(s): 495


    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
     
    给你一个字符串,每个字符有它们各自的价值,把这个串分成两部分(随意分割),如果这部分是回文串的话计算它的价值,否则价值为0, 计算它能得到的最大价值

    /**
    s[]         先存原字符串,后存扩展后的字符串
    p[]        p[i] 表示以i为中心的回文串有多长(只记录一边的长度)
    sum[]    sum[i]表示前i个字符的总价值和
    Left[]    Left[i] 表示前缀长度为 i 的串是否是回文串
    Right[]  Right[i] 表示后缀长度为 i 的串是否是回文串
    **/

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    
    using namespace std;
    #define INF 0x3f3f3f3f
    #define N 1000007
    
    char s[N];
    int Left[N], Right[N], sum[N], p[N];
    
    void Manacher()
    {
        int len = strlen(s);
    
        int index = 0, MaxLen = 0, i;
    
        for(i=2; i<len; i++)
        {
            if(MaxLen>i) p[i] = min(p[index*2-i], MaxLen-i);
            else p[i] = 1;
    
            while( s[i-p[i]]==s[i+p[i]]) p[i]++;
    
            if(p[i]+i>MaxLen)
            {
                MaxLen = p[i] + i;
                index = i;
            }
    
            if(p[i]==i)
                Left[p[i]-1] = true;
            if(p[i]+i==len)
                Right[p[i]-1] = true;
        }
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int a[30], i;
    
            memset(Left, 0, sizeof(Left));
            memset(Right, 0, sizeof(Right));
    
            for(i=0; i<26; i++)
                scanf("%d", &a[i]);
    
            scanf("%s", s);
    
            int len = strlen(s);
    
            for(i=1; i<=len; i++)
                 sum[i] = sum[i-1] + a[s[i-1]-'a'];
    
            for(i=len; i>=0; i--)
            {
                s[i*2+2] = s[i];
                s[i*2+1] = '#';
            }
            s[0] = '$';
    
            Manacher();
    
            int ans = -INF;
            for(i=1; i<len; i++)
            {
                int t = 0;
                if(Left[i])
                    t += sum[i];
                if(Right[len-i])
                    t += sum[len]-sum[i];
    
                ans = max(ans, t);
            }
    
            printf("%d
    ", ans);
    
        }
        return 0;
    }
    View Code
    勿忘初心
  • 相关阅读:
    ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)
    ural 1356. Something Easier(数论,哥德巴赫猜想)
    ural 1261. Tips(进制运算)
    ural 1355. Bald Spot Revisited(数的素因子划分)
    ural 1049. Brave Balloonists(标准分解式,数论)
    jpa之hibernate4.2.4之tomcat6.0测试
    jpa之hibernate4.2.4之双向关联之一对一、一对多、多对多
    JPA之hibernate4.2.4之简单测试
    oracle之存储过程
    dom4j之创建xml、读取xml、修改xml
  • 原文地址:https://www.cnblogs.com/YY56/p/4853437.html
Copyright © 2020-2023  润新知