zoukankan      html  css  js  c++  java
  • hu3613 Best Reward

    地址: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): 2326    Accepted Submission(s): 944


    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

     思路:扩展kmp

     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 
     6 using namespace std;
     7 const int K=1e6+7;
     8 int nt[K],extand[K],v[30],sum[K],l[K],r[K];
     9 char sa[K],sb[K];
    10 void Getnext(char *T,int *next)
    11 {
    12     int len=strlen(T),a=0;
    13     next[0]=len;
    14     while(a<len-1 && T[a]==T[a+1]) a++;
    15     next[1]=a;
    16     a=1;
    17     for(int k=2; k<len; k++)
    18     {
    19         int p=a+next[a]-1,L=next[k-a];
    20         if( (k-1)+L >= p)
    21         {
    22             int j = (p-k+1)>0 ? (p-k+1) : 0;
    23             while(k+j<len && T[k+j]==T[j]) j++;
    24             next[k]=j;
    25             a=k;
    26         }
    27         else
    28             next[k]=L;
    29     }
    30 }
    31 void GetExtand(char *S,char *T,int *next)
    32 {
    33     Getnext(T,next);
    34     int slen=strlen(S),tlen=strlen(T),a=0;
    35     int MinLen = slen < tlen ? slen : tlen;
    36     while(a<MinLen && S[a]==T[a]) a++;
    37     extand[0]=a;
    38     a=0;
    39     for(int k=1; k<slen; k++)
    40     {
    41         int p=a+extand[a]-1, L=next[k-a];
    42         if( (k-1)+L >= p)
    43         {
    44             int j= (p-k+1) > 0 ? (p-k+1) : 0;
    45             while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
    46             extand[k]=j;
    47             a=k;
    48         }
    49         else
    50             extand[k]=L;
    51     }
    52 }
    53 int main(void)
    54 {
    55     int t;cin>>t;
    56     while(t--)
    57     {
    58         int ans=0;
    59         for(int i=0;i<26;i++)
    60             scanf("%d",v+i);
    61         scanf("%s",sa);
    62         int len=strlen(sa);
    63         for(int i=0;i<len;i++)
    64             sb[i]=sa[len-i-1];
    65         sum[0]=v[sa[0]-'a'];
    66         for(int i=1;i<len;i++)
    67             sum[i]=sum[i-1]+v[sa[i]-'a'];
    68         sb[len]='';
    69         GetExtand(sb,sa,nt);
    70         for(int i=0;i<len;i++)
    71         if(extand[i]==len-i) l[len-i-1]=1;
    72         else l[len-i-1]=0;
    73         GetExtand(sa,sb,nt);
    74         for(int i=0;i<len;i++)
    75         if(extand[i]==len-i) r[i]=1;
    76         else r[i]=0;
    77         for(int i=0;i<len-1;i++)
    78         {
    79             int tmp=0;
    80             if(l[i])   tmp+=sum[i];
    81             if(r[i+1])   tmp+=sum[len-1]-sum[i];
    82             ans=max(ans,tmp);
    83         }
    84         printf("%d
    ",ans);
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    AndroidStudio编译错误:Error: null value in entry: blameLogFolder=null
    Android中ViewPager实现滑动条及与Fragment结合的实例教程
    再说Android RecyclerView局部刷新那个坑
    【Android】图片切角,切指定的边。
    Android视频播放和横竖屏切换
    Android中3种全屏方法及3种去掉标题栏的方法
    Android 横竖屏切换
    android控件拖动,移动、解决父布局重绘时控件回到原点
    Android 自定义可拖拽View,界面渲染刷新后不会自动回到起始位置
    004 DOM01
  • 原文地址:https://www.cnblogs.com/weeping/p/6670281.html
Copyright © 2011-2022 走看看