zoukankan      html  css  js  c++  java
  • hdu3613(扩展KMP)

    用kmp的方法写了好久,发现以我的想法用kmp不仅难以实现而且无法证明正确性。 于是还是使用扩展kmp。。。

    Best Reward

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


    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
     
    Source
     
    Recommend
    lcy
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <map>
    #include <queue>
    #include <sstream>
    #include <iostream>
    using namespace std;
    #define INF 0x3fffffff
    #define N 500500
    
    char g[N],g1[N];
    int next[N];
    int chg[30];
    int len;
    int sum[N];
    int mark[N],mark1[N];
    int num[N];
    
    void build(char s[])
    {
        memset(next,0,sizeof(next));
        int p=0,a=0;
        next[0]=len;
        for(int i=1;i<len;i++)
        {
            if(i+next[i-a]-1<p)
            {
                next[i]=next[i-a];
            }
            else
            {
                int k=p-i;
                while(p+1<len && s[p+1]==s[k+1])
                {
                    p++; k++;
                }
                p=max(p,i);
                next[i]=k+1;
                a=i;
            }
        }
    }
    
    void extend_kmp(char s[],char t[])
    {
        memset(sum,0,sizeof(sum));
        int p=-1,a=0;
        for(int i=0;i<len;i++)
        {
            if(i+next[i-a]-1<p)
            {
                sum[i]=next[i-a];
            }
            else
            {
                int k=p-i;
                while(p+1<len && s[p+1]==t[k+1])
                {
                    p++; k++;
                }
                p=max(p,i);
                sum[i]=k+1;
                a=i;
            }
        }
    }
    
    
    int main()
    {
        //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
        //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            for(int i=0;i<26;i++)
                scanf("%d",chg+i);
            scanf("%s",g);
            len=strlen(g);
            for(int i=0;i<len;i++)
            {
                g1[len-1-i]=g[i];
            }
            build(g);
            extend_kmp(g1,g);
            memset(mark,0,sizeof(mark));
            memset(num,0,sizeof(num));
            int tmp=0;
            for(int i=0;i<len;i++)
            {
                tmp+=chg[g[i]-'a'];
                num[i]=tmp;
            }
    
            for(int i=0;i<len-1;i++)
            {
                if(sum[len-1-i]==i+1)
                {
                    mark[i]=1;
                }
            }
            memset(mark1,0,sizeof(mark1));
            build(g1);
            extend_kmp(g,g1);
            for(int i=0;i<len-1;i++)
            {
                if(sum[len-1-i]==i+1)
                {
                    mark1[i]=1;
                }
            }
            int mx=-INF;
            for(int i=0;i<len-1;i++)
            {
                tmp=0;
                int k=len-i-2;
                if(mark[i]==1)
                    tmp+=num[i];
                if(mark1[k]==1)
                    tmp+=num[len-1]-num[i];
                mx=max(mx,tmp);
            }
            printf("%d
    ",mx);
        }
        return 0;
    }
  • 相关阅读:
    错误论:错误是人类实践结果的一种状态,是实践的一个中间环节
    逻辑思维
    正确与错误、真理与谬误
    正确的判断,来自于错误的判断
    PHP+MySQL实现对一段时间内每天数据统计优化操作实例
    centos linux ip地址无法连接数据库,ssh登录服务器时必须使用22端口
    如何更改linux文件目录拥有者及用户组
    navicat ssh通道受限问题处理
    Navicat for MySQL 使用SSH方式链接远程数据库(二)
    Navicat for MySQL 使用SSH方式链接远程数据库
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3224011.html
Copyright © 2011-2022 走看看