zoukankan      html  css  js  c++  java
  • hdu 3613 exkmp

    题目链接  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): 940    Accepted Submission(s): 390


    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
     
    原串的前缀与(原串前后颠倒得到的反串)的前缀长度相同,那么原串对应的这段字符则为回文串。模拟算出ex[i],可以得出ex[i]+i=len,且如果前半段(到i)不是回文,则直接判断(i+1)以后的是否为回文即可。
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    
    using namespace std;
    const int inf=0x3fffffff;
    const int maxn=500000+10;
    char str1[maxn],str2[maxn];
    int nt[maxn],ex1[maxn],ex2[maxn],dp[maxn];
    int kind[26];
    int slen,tlen;
    void get_next(char *s)
    {
        nt[0]=slen;
        int l,r;
        for(l=0;l<slen-1&&s[l]==s[l+1];l++);
        nt[1]=l;
        l=1;
        for(int i=2;i<slen;i++)
        {
            r=l+nt[l]-1;
            if(nt[i-l]+i-1>=r)
            {
                int p=r-i+1>0?r-i+1:0;
                while(p+i<slen&&s[p]==s[p+i])
                    p++;
                nt[i]=p;
                l=i;
            }
            else
                nt[i]=nt[i-l];
        }
    }
    void get_extand(char *s,char *t,int *ex)
    {
        memset(nt,0,sizeof(nt));
        get_next(s);
        int l=0,r;
        while(l<slen&&s[l]==t[l])
            l++;
        ex[0]=l;
        l=0;
        for(int i=1;i<tlen;i++)
        {
            r=ex[l]+l-1;
            if(nt[i-l]+i-1>=r)
            {
                int p=r-i+1>0?r-i+1:0;
                while(p+i<tlen&&s[p]==t[i+p])
                    p++;
                ex[i]=p;
                l=i;
            }
            else
                ex[i]=nt[i-l];
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            for(int i=0;i<26;i++)
                scanf("%d",&kind[i]);
            scanf("%s",str1);
            memset(dp,0,sizeof(dp));
            dp[0]=0;
            tlen=slen=strlen(str1);
            for(int i=1;i<=slen;i++)
                dp[i]=dp[i-1]+kind[str1[i-1]-'a'];
             for(int i=slen-1,j=0;i>=0;i--,j++)
                str2[j]=str1[i];
            str2[slen]='';
            get_extand(str1,str2,ex1);
            get_extand(str2,str1,ex2);
            int ans=0;
            for(int i=0;i<slen;i++)
            {
                int temp=0;
                if(i&&ex1[i]+i==slen)
                {
                    int p=ex1[i];
                    temp+=dp[p];
                    if(ex2[p]+p==slen)
                    {
                        temp+=dp[slen]-dp[p];
                    }
                }
                else
                {
                    int p=i+1;
                    if(ex2[p]+p==slen)
                        temp+=dp[slen]-dp[p];
                }
                ans=ans>temp?ans:temp;
                //printf("i->%d ans->%d
    ",i,ans);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    不为失败找借口,只为成功找方法
  • 相关阅读:
    POJ 2449 Remmarguts' Date(第k短路のA*算法)
    UESTC 1717 Journey(DFS+LCA)(Sichuan State Programming Contest 2012)
    HRBUST 1211 火车上的人数【数论解方程/模拟之枚举+递推】
    洛谷 P1372 又是毕业季I[数论/神坑规律题]
    洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
    CCCC L2-003. 月饼[贪心/类似hdu贪心老鼠]
    二项式定理与杨辉三角
    Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]
    洛谷 P1784 数独[DFS/回溯]
    Wannafly交流赛1 B 硬币[数学思维/贪心]
  • 原文地址:https://www.cnblogs.com/youmi/p/4493124.html
Copyright © 2011-2022 走看看