HDU3613 Best Reward

解决一个算法问题,关于如何将一串由不同种类宝石组成的项链切割成两部分,使得这两部分作为回文串时的价值总和最大。

Best Reward

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



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
 

Source
 

题意:给一串字符,每个字符都有一个价值,要分成两半,求出最大价值,分成两半的字符只有是回文串时才有价值,否则价值为0。

分析:分成两串,前一串一定含有第一个字符,所有这可以想到字符串的前缀,同样,后一串肯定包含最后一个字符,所以这是后缀。我们把字符串反转过来,设原来的字符串是s1,反转过的是s2,用s1匹配s2,就相当于那串字符的前缀匹配他自己的后缀,所以这样匹配出来的就是他的最大前缀字符串,同样用s2匹配s1,他自己的后缀匹配前缀,就匹配出来了最大后缀。



#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=500010;
int val[26];
int next[MAXN],pre[MAXN],pos[MAXN],sum[MAXN];
char s1[MAXN],s2[MAXN];
void getnext(char *a,int n)
{
    int i=0,k=-1;
    next[0]=-1;
    while(i<n)
    {
        if(k==-1||a[i]==a[k])
        {
            i++;
            k++;
            next[i]=k;
        }
        else
            k=next[k];
    }
}
int KMP(char *a,char *b,int n)  //a是要匹配的串
{
    int i=0,j=0;
    while(i<n&&j<n)
    {
        if(i==-1||a[i]==b[j])
            i++,j++;
        else
            i=next[i];
    }
    return i;
}
int main()
{
    int t,n,i;
    scanf("%d",&t);
    while(t--)
    {
        for(i=0;i<26;i++)
            scanf("%d",&val[i]);
        scanf("%s",s1);
        n=strlen(s1);
        sum[0]=0;
        for(i=0;i<n;i++)
        {
            s2[i]=s1[n-1-i];
            sum[i+1]=sum[i]+val[s1[i]-'a'];
        }
        getnext(s1,n);
        int k=KMP(s1,s2,n);
        while(k)
        {
            pre[k]=n+1;
            k=next[k];
        }
        getnext(s2,n);
        k=KMP(s2,s1,n);
        while(k)
        {
            pos[k]=n+1;     //n+1仅仅是个标记
            k=next[k];
        }
        int ans=0,temp=0;
        for(i=1;i<n;i++)
        {
            if(pre[i]==n+1)
                temp+=sum[i];
            if(pos[n-i]==n+1)
                temp+=sum[n]-sum[i];
            if(temp>ans)
                ans=temp;
            temp=0;
        }
        printf("%d\n",ans);
    }
    return 0;
}


内容概要:本文档是一份关于交换路由配置的学习笔记,系统地介绍了网络设备的远程管理、交换机与路由器的核心配置技术。内容涵盖Telnet、SSH、Console三种远程控制方式的配置方法;详细讲解了VLAN划分原理及Access、Trunk、Hybrid端口的工作机制,以及端口镜像、端口汇聚、端口隔离等交换技术;深入解析了STP、MSTP、RSTP生成树协议的作用与配置步骤;在路由部分,涵盖了IP地址配置、DHCP服务部署(接口池与全局池)、NAT转换(静态与动态)、静态路由、RIP与OSPF动态路由协议的配置,并介绍了策略路由和ACL访问控制列表的应用;最后简要说明了华为防火墙的安全区域划分与基本安全策略配置。; 适合人群:具备一定网络基础知识,从事网络工程、运维或相关技术岗位1-3年的技术人员,以及准备参加HCIA/CCNA等认证考试的学习者。; 使用场景及目标:①掌握企业网络中常见的交换与路由配置技能,提升实际操作能力;②理解VLAN、STP、OSPF、NAT、ACL等核心技术原理并能独立完成中小型网络搭建与调试;③通过命令示例熟悉华为设备CLI配置逻辑,为项目实施和故障排查提供参考。; 阅读建议:此笔记以实用配置为主,建议结合模拟器(如eNSP或Packet Tracer)动手实践每一条命令,对照拓扑理解数据流向,重点关注VLAN间通信、路由选择机制、安全策略控制等关键环节,并注意不同设备型号间的命令差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值