zoukankan      html  css  js  c++  java
  • hdu3613 Best Reward manacher+贪心+前缀和

    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. 

    题意:给出一个由26种字母组成的字符串,每种字母有权值,当一个串是回文串的时候,它的权值是其中所有字母的权值之和,否则是0。

    现在要将这个字符串切成两段,问两段的权值和最大是多少。

    用前缀和处理串的前缀权值和。

    manacher进行回文串的匹配处理。

    贪心切分位置并用manacher处理的数组判断是否是回文子串,并用前缀和数组求值,取最大值。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 const int maxn=5e5+5;
     7 char s[maxn],t[maxn<<1];
     8 int p[maxn<<1];
     9 int a[maxn],v[26];
    10 
    11 void manacher(){
    12     int len=strlen(s),l=0;
    13     t[l++]='$';
    14     t[l++]='#';
    15     for(int i=0;i<len;++i){
    16         t[l++]=s[i];
    17         t[l++]='#';
    18     }
    19     t[l]=0;
    20     int maxx=0,num=0;
    21     for(int i=0;i<l;++i){
    22         p[i]=maxx>i?min(p[2*num-i],maxx-i):1;
    23         while(t[i+p[i]]==t[i-p[i]])p[i]++;
    24         if(i+p[i]>maxx){
    25             maxx=i+p[i];
    26             num=i;
    27         }
    28     }
    29 }
    30 
    31 int main(){
    32     int T;
    33     scanf("%d",&T);
    34     while(T--){
    35         for(int i=0;i<26;++i)scanf("%d",&v[i]);
    36         scanf("%s",s);
    37         int l=strlen(s);
    38         a[0]=v[s[0]-'a'];
    39         for(int i=1;i<l;++i){
    40             a[i]=a[i-1]+v[s[i]-'a'];
    41         }
    42         manacher();
    43         int ans=0;
    44         for(int i=0;i<l-1;++i){
    45             int tmp=0;
    46             int num=p[i+2]-1;
    47             if(num==i+1)tmp+=a[i];
    48             num=p[i+l+2]-1;
    49             if(num==l-i-1)tmp+=a[l-1]-a[i];
    50             if(tmp>ans)ans=tmp;
    51         }
    52         printf("%d
    ",ans);
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    《编译原理》-用例题理解-自顶向下语法分析及 FIRST,FOLLOW,SELECT集,LL(1)文法
    8 张脑图入门 JavaScript
    Navicat Premium 12连接Oracle时提示oracle library is not loaded的问题解决
    Spring boot 多模块项目 + Swagger 让你的API可视化
    Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版
    JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)
    SSM 项目从搭建爬坑到 CentOS 服务器部署
    LeetCode
    有趣的位运算
    记一次向maven中央仓库提交依赖包
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592559.html
Copyright © 2011-2022 走看看