HDOJ 3613 Best Reward --- 扩展KMP算法求前(后)缀回文串

本文介绍了一种使用扩展KMP算法解决字符串分割问题的方法,旨在找到两个回文子串,使得其价值总和最大。通过预处理前缀和数组,结合扩展KMP算法判断前后缀是否为回文,最终实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3613

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

 题意:

给定一个字符串比如abacac,每个字符都有一个价值,将字符串拆成2部分,每部分如果是回文串则总价值为各字符价值之和,否则总价值为0。 求总价值最大。

比如所有字符价值为1,

可以拆成 a,bacac->总价值为0,  ab,acac->价值0, aba,cac->价值为6......

最后算出总价值最大为6.

题解:

这个题实在让我吐血,在网上看扩展kmp看了半天才看懂,然后看别人代码看半天才知道怎么做,然后自己太菜写代码又写了一个多小时,最后因为有2条语句不小心写错,debug了很久,现在终于能AC了,lz差点崩溃了。。。

以上都是废话

题解就是先前缀和数组预处理一遍价值

然后很关键的一步就是求前(后)缀回文串,直接用源串对逆串做一遍扩展KMP,去判断extend数组中extend[i]是否等于len-i(i从0开始算,len是字符串长度),等于的话后缀串s[i...len-1]就是回文。

前缀回文反一遍过来就是,对逆串和源串做扩展KMP,道理和上面一样的。

最后就是for循环扫一遍字符串,将其拆成2部分,分别判断前后部分是否是回文串然后用之前算的前缀和数组求价值就OK了。

 

#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAXN 500005
using namespace std;
int value[27];
char input[MAXN],s[MAXN],t[MAXN];// 输入,源串,逆串 
int extend[MAXN],Next[MAXN];
int prefix[MAXN];// 前缀和数组
bool suffix_palindrome[MAXN],prefix_palindrome[MAXN];// 前后缀回文标记 
void CalNext() {
    int len1 = strlen(t);
    int len2 = len2;
    memset(Next,0,sizeof(Next));
    Next[0] = len1;
    int a = 1,p = 1;// a不能初始化为0,不然后面Next[i-a]就直接用了还没算出来的值 
    int j;
    for(int i = 1;i < len1;i++) {
        if(i < p && (Next[i-a]+i) < p) {
            Next[i] = Next[i-a];
        } else {
            j = p - i;
            j = max(0,j);
            while(t[i+j] == t[j] && (i+j) < len1) j++;
            Next[i] = j;
            a = i,p = i + j;   // 更新a和p 
        }
    }
    
}

// 扩展KMP算法求extend数组
void Ex_Kmp() {
    int len1 = strlen(s);
    int len2 = strlen(t);
    CalNext();
    memset(extend,0,sizeof(extend));
    int pos = 0;
    // 计算extend[0]
    while(s[pos] == t[pos] && pos < len1 && pos < len2) {
        pos++;
    }
    extend[0] = pos;
    int a = 0,p = pos;
    int j;
    for(int i = 1;i < len1;i++) {
        if(i < p && (Next[i-a]+i) < p) {
            extend[i] = Next[i-a];
        } else {
            j = p - i;
            j = max(0,j);
            while(s[i+j] == t[j] && (i+j) < len1 && j < len2) j++;
            extend[i] = j;
            a = i,p = i + j;   // 更新a和p 
        }
    }
    
}

int main()
{
    int num;
    scanf("%d",&num);
    while(num--) {
        for(int i = 0;i < 26;i++) {
            scanf("%d",&value[i]);
        }
        scanf("%s",input);
        // 求前缀和
        memset(prefix,0,sizeof(prefix));
        int len = strlen(input);
        prefix[0] = value[input[0]-'a'];
        for(int i = 1;i < len;i++) {
            prefix[i] = prefix[i-1] + value[input[i]-'a'];
        }
        // 求后缀回文串
        strcpy(s,input);
        reverse(input,input+len);
        strcpy(t,input);
        Ex_Kmp();
        for(int i = 0;i < len;i++) {
            suffix_palindrome[i] = ( (extend[i]==len-i) ? true : false);
        }
        // 求前缀回文串
        strcpy(s,input);
        reverse(input,input+len);
        strcpy(t,input);
        Ex_Kmp();
        for(int i = 0;i < len;i++) {
            prefix_palindrome[i] = ( (extend[len-1-i]==1+i) ? true : false);
        }
        int ret = 0;
        for(int i = 1;i < len;i++) {
            int tmp = 0;
            if(suffix_palindrome[i]) tmp += prefix[len-1] - prefix[i-1];
            if(prefix_palindrome[i-1]) tmp += prefix[i-1];
            ret = max(ret,tmp);
        }
        printf("%d\n",ret);
    }
   return 0;
} 

 

内容概要:本文介绍了基于Python实现的SSA-GRU(麻雀搜索算法优化门控循环单元)时间序列预测项目。项目旨在通过结合SSA的全局搜索能力和GRU的时序信息处理能力,提升时间序列预测的精度和效率。文中详细描述了项目的背景、目标、挑战及解决方案,涵盖了从数据预处理到模型训练、优化及评估的全流程。SSA用于优化GRU的超参数,如隐藏层单元数、学习率等,以解决传统方法难以捕捉复杂非线性关系的问题。项目还提供了具体的代码示例,包括GRU模型的定义、训练和验证过程,以及SSA的种群初始化、迭代更新策略和适应度评估函数。; 适合人群:具备一定编程基础,特别是对时间序列预测和深度学习有一定了解的研究人员和技术开发者。; 使用场景及目标:①提高时间序列预测的精度和效率,适用于金融市场分析、气象预报、工业设备故障诊断等领域;②解决传统方法难以捕捉复杂非线性关系的问题;③通过自动化参数优化,减少人工干预,提升模型开发效率;④增强模型在不同数据集和未知环境中的泛化能力。; 阅读建议:由于项目涉及深度学习和智能优化算法的结合,建议读者在阅读过程中结合代码示例进行实践,理解SSA和GRU的工作原理及其在时间序列预测中的具体应用。同时,关注数据预处理、模型训练和优化的每个步骤,以确保对整个流程有全面的理解。
内容概要:本文详细介绍了如何使用PyQt5创建一个功能全面的桌面备忘录应用程序,涵盖从环境准备、数据库设计、界面设计到主程序结构及高级功能实现的全过程。首先,介绍了所需安装的Python库,包括PyQt5、sqlite3等。接着,详细描述了SQLite数据库的设计,创建任务表和类别表,并插入默认类别。然后,使用Qt Designer设计UI界面,包括主窗口、任务列表、工具栏、过滤器和日历控件等。主程序结构部分,展示了如何初始化UI、加载数据库数据、显示任务列表以及连接信号与槽。任务管理功能方面,实现了添加、编辑、删除、标记完成等操作。高级功能包括类别管理、数据导入导出、优先级视觉标识、到期日提醒、状态管理和智能筛选等。最后,提供了应用启动与主函数的代码,并展望了扩展方向,如多用户支持、云同步、提醒通知等。 适合人群:零基础初学者,对Python和桌面应用程序开发感兴趣的开发者。 使用场景及目标:①学习PyQt5的基本使用方法,包括界面设计、信号与槽机制;②掌握SQLite数据库的基本操作,如创建表、插入数据、查询等;③实现一个完整的桌面应用程序,具备增删改查和数据持久化功能;④了解如何为应用程序添加高级特性,如类别管理、数据导入导出、到期日提醒等。 阅读建议:此资源不仅适用于零基础的学习者,也适合有一定编程经验的开发者深入理解PyQt5的应用开发。建议读者跟随教程逐步实践,结合实际操作来理解和掌握每个步骤,同时可以尝试实现扩展功能,进一步提升自己的开发技能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值