HDU3613 Best Reward —— 扩展KMP

基于网关实现不同网段S7-1200 CPU的通信方法 在A网段的计算机上ping B网段的计算机:在A网段的计算机上使用ping命令,向B网段的计算机发送数据包。在B网段的计算机上ping A网段的计算机:同样地,在B网段的计算机上使用ping命令,向A网段的计算机发送数据包。数据包也会经过路由器进行转发,最终到达A网段的计算机。配置路由器:在路由器上配置两个网段的IP地址和子网掩码,并设置路由表,使得路由器可以将A网段和B网段之间的数据进行转发。确定两个网络之间的路由器:假设需要将A网段和B网段进行通信,需要在A、B两个网段之间设置一台路由器。 阅读详情

题目链接: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): 2644    Accepted Submission(s): 1072


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: v 1, v 2, ..., v 26 (-100 ≤ v i ≤ 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. 

 

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<bits/stdc++.h>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 5e5+10;

void pre_EXKMP(char x[], int m, int Next[])
{
    Next[0] = m;
    int j = 0;
    while(j+1<m && x[0+j]==x[1+j]) j++;
    Next[1] = j;
    int k = 1;
    for(int i = 2; i<m; i++)
    {
        int p = Next[k]+k-1;
        int L = Next[i-k];
        if(i+L<=p) Next[i] = L;

        else
        {
            j = max(0, p-i+1);
            while(i+j<m && x[i+j]==x[0+j]) j++;
            Next[i] = j;
            k = i;
        }
    }
}

void EXKMP(char x[], int m, char y[], int n, int Next[], int exd[])
{
    pre_EXKMP(x,m,Next);
    int j = 0;
    while(j<n && j<m && x[j]==y[j]) j++;
    exd[0] = j;
    int k = 0;
    for(int i = 1; i<n; i++)
    {
        int p = exd[k]+k-1;
        int L = Next[i-k];
        if(i+L<=p) exd[i] = L;
        else
        {
            j = max(0,p-i+1);
            while(i+j<n && j<m && y[i+j]==x[0+j]) j++;
            exd[i] = j;
            k = i;
        }
    }
}

char s1[MAXN], s2[MAXN];
int val[30], Next[MAXN], exd1[MAXN], exd2[MAXN], sum[MAXN];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        rep(i,0,25) scanf("%d",&val[i]);
        scanf("%s",s1);
        int len = strlen(s1);
        rep(i,1,len)
        {
            sum[i] = sum[i-1] + val[s1[i-1]-'a'];
            s2[len-i] = s1[i-1];
        }

        EXKMP(s1, len, s2, len, Next, exd1);
        EXKMP(s2, len, s1, len, Next, exd2);
        int ans = 0;
        for(int i = 1; i<len; i++)
        {
            int tmp = 0;
            if(exd1[len-i]==i) tmp += sum[i];
            if(exd2[i]==len-i) tmp += sum[len]-sum[i];
            ans = max(ans,tmp);
        }
        printf("%d\n", ans);
    }
}



代码二(下标从1开始):

#include<bits/stdc++.h>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 5e5+10;

void pre_EXKMP(char x[], int m, int Next[])
{
    Next[1] = m;
    int j = 0;
    while(2+j<=m && x[1+j]==x[2+j]) j++;
    Next[2] = j;
    int k = 2;
    for(int i = 3; i<=m; i++)
    {
        int p = Next[k]+k-1;
        int L = Next[i-k+1];
        if(i+L<=p) Next[i] = L;
        else
        {
            j = max(0, p-i+1);
            while(i+j<=m && x[i+j]==x[1+j]) j++;
            Next[i] = j;
            k = i;
        }
    }
}

void EXKMP(char x[], int m, char y[], int n, int Next[], int exd[])
{
    pre_EXKMP(x,m,Next);
    int j = 1;
    while(j<=n && j<=m && x[j]==y[j]) j++;
    exd[1] = j-1;
    int k = 1;
    for(int i = 2; i<=n; i++)
    {
        int p = exd[k]+k-1;
        int L = Next[i-k+1];
        if(i+L<=p) exd[i] = L;
        else
        {
            j = max(0, p-i+1);
            while(i+j<=n && j<=m && y[i+j]==x[1+j]) j++;
            exd[i] = j;
            k = i;
        }
    }
}

char s1[MAXN], s2[MAXN];
int val[30], Next[MAXN], exd1[MAXN], exd2[MAXN], sum[MAXN];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i = 0; i<26; i++)
            scanf("%d",&val[i]);

        scanf("%s",s1+1);
        int len = strlen(s1+1);
        for(int i = 1; i<=len; i++)
        {
            sum[i] = sum[i-1] + val[s1[i]-'a'];
            s2[len-i+1] = s1[i];
        }

        EXKMP(s1, len, s2, len, Next, exd1);
        EXKMP(s2, len, s1, len, Next, exd2);
        int ans = 0;
        for(int i = 1; i<=len-1; i++)
        {
            int tmp = 0;
            if(exd1[len-i+1]==i) tmp += sum[i];
            if(exd2[i+1]==len-i) tmp += sum[len]-sum[i];
            ans = max(ans,tmp);
        }
        printf("%d\n", ans);
    }
}


思科 MIB 库 思科 MIB 阅读详情

相关推荐

一文学会Cadence Sigrity PDN仿真——硬件新手小白都能看懂的保姆级教程

电源完整性的仿真,实际上是在仿真以及优化系统中的等效串联电阻。知道了仿真以及优化的目标之后,接下来就是要弄明白这个串联电阻从哪儿来以及怎么优化。整个系统中的串联电阻实际上由三部分组成:没有相位的阻抗,有相位的容抗和感抗,得益于相位,在某个特定的频点,容抗和感抗会大小相等,相位相反而相互抵消,这时候整个系统中就剩下了阻抗,所以整个系统的串联电阻达到了一个最低值,这种现象就是----谐振。当系统处于谐振点时候,整个系统会有一个最小的串联电阻,所以第一个优化电阻的手段就是让谐振来的更频繁一点。

学习帮的博客 2万+

HDU3613:Best Reward(拓展KMP)

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

ACM!荣耀之路! 2142

研一脑电小白日记 之 EEGLAB预处理实战指南

本文为脑电研究新手提供了一份详尽的EEGLAB预处理实战指南。从MATLAB与EEGLAB环境搭建、使用NeuracleEEGFileReader插件导入BDF格式数据开始,逐步讲解了电极定位、滤波、降采样、ICA去伪迹等核心步骤,旨在帮助初学者高效完成脑电数据预处理,为后续分析奠定坚实基础。

weixin_29230649的博客 559

HDU - 3613——Best Reward (分割回文串最大权值)

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 d...

creator平 661

深入了解神经网络:构建人工智能的基石

这篇博客将为您解释神经网络的构造,让您能够理解这个令人着迷的领域的基本工作原理。神经网络是人工智能领域的关键技术,它们的构造基于神经元的灵感,并结合了数学、统计和机器学习的原理。输入可以类比为神经元的树突,而输出可以类比为神经元的轴突,计算则可以类比为细胞核。较好的方法就是预先设定几个可选值,通过切换这几个值来看整个模型的预测效果,选择效果最好的值作为最终选择。:这是神经网络的核心部分,包含多个层次的神经元。、神经网络结构图中的拓扑与箭头代表着预测过程时数据的流向,跟训练时的数据流有一定的区别;

AI_dataloads的博客 2万+

扫地机器人:从仿真到决策,一个完整SLAM导航系统的自研之路

本文详细记录了从零开始自研扫地机器人SLAM导航系统的全过程,涵盖ROS-Gazebo仿真系统搭建、地图预处理优化、全覆盖规划算法迭代等关键技术。通过三次算法升级,最终实现92%的清洁效率,并分享DWA调参、状态机设计等实战经验,为开发者提供从仿真到落地的完整解决方案。

weixin_29053695的博客 352

hdu 3613 Best Reward (kmp扩展)

题意: 给出一个字符串,并且给出每种字符串对应的价值,现在要二分这个字符串,如果二分的两部分,如果是回文串就加上对应字符的价值所谓这两个部分的价值,如果不是回文两个部分的价值为0。 题解: 这题相当于可以转化为求回文前缀和后缀,求回文前缀:将原串反转接到原串后面。求回文后缀:将原串接到原串反转后的串后面。人后求next数组,next[len]就最长的那个回文前缀,不断k=next[k]就能找

ACM之梦 554

GD32 TLI+IPA实战:5分钟搞定RGB888屏幕显示,并开启2D图形加速

本文详细介绍了如何使用GD32微控制器的TLI和IPA功能快速驱动RGB888屏幕并开启2D图形加速。通过硬件配置、时序设置和显存管理,开发者可以高效实现图形界面渲染,并利用IPA加速器提升性能,适用于嵌入式GUI开发等场景。

weixin_29163797的博客 231

Best Reward (manacher)

Best Reward (manacher)

binbin_12345的博客 216

股票价格预测的秘密武器:用C#实现蒙特卡洛路径模拟,轻松掌握市场波动规律!

摘要: 蒙特卡洛模拟是股票预测的有效工具,通过随机抽样模拟价格路径,克服传统技术分析的局限性。其核心模型是几何布朗运动(GBM),描述股价随机变化过程。C#实现的关键步骤包括:1)获取输入参数;2)生成随机数路径;3)分析结果。该方法不预测具体价格,而是提供可能的价格范围,帮助投资者评估风险。代码结构清晰,分为输入、模拟、分析和可视化四个模块,通过参数封装提高可读性,适用于不确定市场的概率预测。

墨夶的博客 602

如何程序化的将 Wire 文件转换成 STEP、CATIA、DWG、NX、FBX 等格式

Wire 文件是专有格式,只有 Autodesk Alias 软件能够直接打开和编辑。 虽然可以在 Alias 软件中手动打开文件并导出为 STEP,但在实际工作中,经常会遇到需要转换数十甚至数百个文件的场景。如果每次都手动操作软件进行转换,不仅效率低下,还容易出错,批量场景下会非常麻烦。因此,程序化、自动化的转换方案变得至关重要。

weixin_44211633的博客 452

Best Reward (manacher--o(n))

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 di...

Mr_Kingk的博客 272

Origin教程004:Origin图形导出

需要编辑的话,双击图形,会自动用origin软件打开图形,这种方式是最开始导出图形的功能,方式一和二都是后面增加的。如果导出的图片边距较大的时候,会影响图片的美观,可以通过。也可以设置在PPT中直接编辑,找到。解决方法是,选择一条线,

WwLK123的博客 6334

HDU 3613 Best Reward(扩展KMP)

题意:每个字母有一个权值,要把一个字符串分为2部分(都非空),只有这部分是回文串才能算权值,问权值和最大是多少。 做法:这题比较简单,用manacher算出每个点为中心的最大回文串长度,必须延伸到两端才是有用的。不过学习了下扩展KMP。 学习资料:http://www.cnblogs.com/Rlemon/archive/2013/06/26/3157574.html 思想其实跟manach

glq007的acm的奋斗 871

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[扩展KMP][HDU3613][Best Reward]

题意:将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0。 问如何分割,使得两个串权值之和最大思路:首先了解扩展kmp 扩展KMP:给出模板串A和子串B,长度分别为lenA和lenB,要求在线性时间内,对于每个A[i](0<=i<=lenA-1),求出A[i..lenA-1]与B的最长公共前缀长度,记为ex[i](或者说

学习之路 476

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

扩展KMP --- HDU 3613 Best Reward

Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613   Mean:  给你一个字符串,每个字符都有一个权值(可能为负),你需要将这个字符串分成两个子串,使得这两个子串的价值之和最大。一个子串价值的计算方法:如果这个子串是回文串,那么价值就是这个子串所有字符权值之和;否则价值为0。  

北岛知寒 285

扩展kmp求最长回文子串_hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串

/**题目:hdu3613 Best Reward链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613题意:有一个字符串,把他切成两部分。如果这部分是回文串,那么他的值为所有字母的权值和。否则这部分值为0;这两部分的值和为该切法的权值。求最大的切法的权值。思路:如果能够判断[0,i],[i,n-1]是一个回文串(0<=i取最大的。扩展kmp的做法...

weixin_39762001的博客 158

HDU3613 Best Reward(扩展KMP)

题意:给一个由小写字母构成的字符串,每个字母否有对应的值,将串分成两个子串,如果子串是回文串的话子串的值就是每个字符的值之和;否则子串的值就是0.原串的值等于两个子串之和,求原串能取到的最大的值。 思路:求回文子串的和的最大值显然需要找出所有可能的回文子串,可以用扩展KMP。先将原串s1反转得到s2,以s1为目标串s2为模式串做一次扩展KMP得到extend1数组,此时若 extend1

hcx11333的博客 287

hdu 3613 Best Reward 扩展kmp

#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int MM=500100; int next1[MM],extand1[MM]; int next2[MM],extand2[MM]; int sum[MM]; char S[...

amily05200的博客 147

HDU 3613 Best Reward(扩展KMP:回文判断)

HDU 3613 Best Reward(扩展KMP:回文判断) http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:给你一个字符串,要你把这个字符串分成两段,并使得被分开的两段价值和最大.一个串如果是回文,那么它的价值就是所有字符的价值和,否则价值为0. 分析:        首先原始串为S,将S逆转得到串T.(S=abcaaa,那么T=a

code 2608

HDU 3613 Best Reward(扩展KMP模板)

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 neck

HJY 324

HDU 3613 Best Reward 正反两次扩展KMP

题目来源:HDU 3613 Best Reward 题意:每个字母对应一个权值 将给你的字符串分成两部分 如果一部分是回文 这部分的值就是每个字母的权值之和 求一种分法使得2部分的和最大 思路:考虑扩展KMP 输出a串 得到a的反串b 求出f[0]和f[1] 和 extend[0]和extend[1] 正反求2次 枚举位置i 分成2部分0到i-1 和i到n-1 因为分成的2部分必须组成原字符

嗯。 1045

manacher || 扩展kmp -- Best Reward HDU - 3613

Best Reward HDU - 3613 题意: T次询问,每次给你一个字符串,字符串仅由26个小写字母组成,每个小写字母都有一个属性值。要你计算字符串的最大贡献。把字符串分成前后两个子串,若子串回文串则该子串贡献为字母的属性和,否则贡献为0。 扩展kmp思路: 用扩展kmp求出每个回文前缀和回文后缀并标记。在遍历划分子串求最大值。 扩展kmp思路代码 #include<iostream> #include<cstdio> #include<cstring> #in

dajiangyou123456的博客 219

Best Reward HDU - 3613(扩展KMP)

要把原串反转一下进行比较。 扩展kmp求t与s的最长前缀的长度,用extend[i]记录。 背。 #include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const int...

weixin_43819578的博客 139

HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举

题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3104Accepted Submission(s): 1...

alince20008的博客 212

HDU3613 Best Reward 3连发之扩展KMP

题目链接:HDU3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1420    Accepted Submission(s): 576 Problem Description

teddywang 797
上一篇: HDU2222 Keywords Search —— AC自动机
下一篇: POJ1679 The Unique MST —— 次小生成树
DOLFAMINGO
博客等级 码龄10年 8粉丝 216原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值