zoukankan      html  css  js  c++  java
  • S

    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. 

    InputThe 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 ≤ 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 v 1, the value of 'b' is v 2, ..., and so on. The length of the string is no more than 500000. 

    OutputOutput 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
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstring>
    using namespace std;
    #define MAXN 500010
    typedef long long LL;
    /*
    扩展KMP 算法
    extend[i] 表示 从s[i]开始的s后缀 与t[]最长的相同前缀长度
    */
    char str1[MAXN],str2[MAXN];
    int sum[MAXN],v[27],Next[MAXN],extend1[MAXN],extend2[MAXN];
    void EKMP(char s[],char t[],int Next[],int extend[])
    {
        int i,j,p,L;//第一部分求Next[] Next[i]表示从i开始的后缀和t[]的最长相同前缀长度
        int ls = strlen(s);
        int lt = strlen(t);
        Next[0] = lt;
        j = 0;
        while(j+1<lt&&t[j]==t[j+1])
            j++;
        Next[1] = j;//t[1]开始的后缀和t[]的最长相同前缀长度
    
        int a=1;
        for(i=2;i<lt;i++)
        {
            p = a+Next[a]-1;//a表示之前匹配的最长长度的开始,p表示之前匹配到的最长长度的结尾
            L = Next[i-a];    
            if(i+L<p+1)
                Next[i] = L;
            else
            {
                j = max(0,p-i+1);
                while(i+j<lt&&t[i+j]==t[j])
                    j++;
                Next[i] = j;
                a = i;
            }
        }
    
        j=0;
        while(j<ls && j<lt && s[j]==t[j])j++;
        extend[0] = j;
        a = 0;
        for(i=1;i<ls;i++)
        {
            p = extend[a]+a-1;
            L = Next[i-a];
            if(i+L<p+1)
                extend[i] = L;
            else
            {
                j = max(0,p-i+1);
                while(j+i<ls&&j<lt&&s[i+j]==t[j])
                    j++;
                extend[i] = j;
                a = i;
            }
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            for(int i=0;i<26;i++)
                scanf("%d",&v[i]);
            scanf("%s",str1);
            int len = strlen(str1);
            sum[0] = 0;
            for(int i=0;i<len;i++)
            {
                sum[i+1] = sum[i] + v[str1[i]-'a'];
                str2[i] = str1[len-1-i];
            }
    
            EKMP(str2,str1,Next,extend1);
            EKMP(str1,str2,Next,extend2);
            int ans = -1000;
            for(int i=1;i<len;i++)
            {
                int tmp = 0;
                if(extend1[i]+i==len)//前半部分回文
                    tmp += sum[len-i];
                int pos = len-i;
                if(extend2[pos]+pos==len)//后半部分回文
                    tmp += sum[len]-sum[pos];
                ans = max(tmp,ans);
            }
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    挺好的程序员面试网址
    [转]浅谈协方差矩阵
    坚持
    matlab中文论坛
    看书
    Opengl绘制点
    说服力
    心情
    vector操作
    我的毕设
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6691104.html
Copyright © 2011-2022 走看看