zoukankan      html  css  js  c++  java
  • hdu 3613 KMP算法扩展

    Best Reward

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


    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)
    分析:求出并记录回文前缀跟回文后缀,线性扫一下求最大值。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #define INF 99999999
    using namespace std;
    
    const int MAX=500000+10;
    char s1[MAX],s2[MAX];
    int f[MAX],sum[MAX],val[27];
    bool per[MAX],pos[MAX];
    
    void getFail(char *a,int len)
    {
        int i,j;
        f[0]=f[1]=0;
        for(i=1;i<len;i++)
        {
            j=f[i];
            while(j && a[i]!=a[j]) j=f[j];
            f[i+1]=(a[i]==a[j]?j+1:0);
        }
    }
    
    int find(char *a,char *b,int len)
    {
        int i,j=0;
        for(i=0;i<len;i++)
        {
            while(j && a[i]!=b[j]) j=f[j];
            if(a[i]==b[j]) j++;
        }
        return j==len?f[j]:j;//必需要分成两部分,排除整个为回文串的情况
    }
    
    int main()
    {
        int n,k,i,num,ans,len;
        scanf("%d",&n);
        while(n--)
        {
            for(i=0;i<26;i++)scanf("%d",val+i);
            scanf("%s",s1);
            len=strlen(s1);
            for(i=0;i<=len/2;i++) s2[i]=s1[len-i-1],s2[len-i-1]=s1[i];
            sum[1]=val[s1[0]-'a'];
            for(i=2;i<=len;i++) sum[i]=sum[i-1]+val[s1[i-1]-'a'];
            getFail(s1,len);
            k=find(s2,s1,len);//求s1最大前缀回文串位置
            memset(per,false,sizeof(per));
            while(k) per[k]=true,k=f[k];//标记所有前缀回文串 
            getFail(s2,len);
            k=find(s1,s2,len);//求s1最大后缀回文串位置 
            memset(pos,false,sizeof(pos));
            while(k) pos[k]=true,k=f[k];//标记所有后缀回文串 
            ans=-INF;
            for(i=1;i<len;++i)
            {
                num=0;
                if(per[i]) num+=sum[i];
                if(pos[len-i])num+=sum[len]-sum[i];
                if(num>ans)ans=num;
                
            }
            printf("%d
    ",ans);
        }
        return 0;
    } 
  • 相关阅读:
    codeforces 587B
    codeforces 552E
    算法竞赛模板 字典树
    算法竞赛模板 二叉树
    算法竞赛模板 图形面积计算
    TZOJ 1545 Hurdles of 110m(动态规划)
    算法竞赛模板 判断线段相交
    算法竞赛模板 折线分割平面
    TZOJ 3005 Triangle(判断点是否在三角形内+卡精度)
    算法竞赛模板 KMP
  • 原文地址:https://www.cnblogs.com/xiong-/p/3628929.html
Copyright © 2011-2022 走看看