Two Rabbits HDU - 4745 (区间dp)

ROS & ROS2 机器人深度相机激光雷达多传感器标定工具箱入门教程(一) multisensor_calibration 是一个积极维护的通用标定工具箱,用于辅助基于目标的多传感器标定,支持 ROS 1 和 ROS 2。它提供了多种方法和应用来标定复杂的多传感器系统,例如。 阅读详情

Two Rabbits

HDU - 4745

Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were arranged as a clockwise ring. That is to say, the first stone was adjacent to the second stone and the n-th stone, and the second stone is adjacent to the first stone and the third stone, and so on. The weight of the i-th stone is ai.

The rabbits jumped from one stone to another. Tom always jumped clockwise, and Jerry always jumped anticlockwise.

At the beginning, the rabbits both choose a stone and stand on it. Then at each turn, Tom should choose a stone which have not been stepped by itself and then jumped to it, and Jerry should do the same thing as Tom, but the jumping direction is anti-clockwise.

For some unknown reason, at any time , the weight of the two stones on which the two rabbits stood should be equal. Besides, any rabbit couldn't jump over a stone which have been stepped by itself. In other words, if the Tom had stood on the second stone, it cannot jump from the first stone to the third stone or from the n-the stone to the 4-th stone.

Please note that during the whole process, it was OK for the two rabbits to stand on a same stone at the same time.

Now they want to find out the maximum turns they can play if they follow the optimal strategy.

Input The input contains at most 20 test cases.
For each test cases, the first line contains a integer n denoting the number of stones.
The next line contains n integers separated by space, and the i-th integer ai denotes the weight of the i-th stone.(1 <= n <= 1000, 1 <= ai <= 1000)
The input ends with n = 0. Output For each test case, print a integer denoting the maximum turns. Sample Input
1
1
4
1 1 2 1
6
2 1 1 2 1 3
0
Sample Output
1
4
5

        
  
Hint
For the second case, the path of the Tom is 1, 2, 3, 4, and the path of Jerry is 1, 4, 3, 2.
For the third case, the path of Tom is 1,2,3,4,5 and the path of Jerry is 4,3,2,1,5.

        
 

思路:这道题大意就是求最长非连续回文子串,首先求最长的回文子序列的状态转移方程比较好写

状态转移方程为 dp[i,j]=max(dp[i,j],dp[i+1,j],dp[i,j-1])

如果s[i]=s[j]   那么dp[i,j]=max(dp[i,j],dp[i+1,j-1]+2)

方法一:最后答案为两个区间的回文长度和

这种方法下我们只求区间1-n的每个子区间的最长的回文子序列,那么为什么就是两个区间长度之和呢

举个例子 1 3 3 1 9 8 9这个序列中我们可以分成两部分

1 3 3 1       9 8 9

1.........i      i+1....n

a-> <-b

这样a向右走从1->i 再从i+1->n这样它就走了1331989,而b向左走从i->1再从n->i+1 这样他走过的还是1331989

所以长度是两段回文序列长度的和

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1010;
int dp[maxn][maxn];
int a[maxn];
int main(){
    int n,ans;
    while(scanf("%d",&n),n){
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
            dp[i][i] = 1;
        }
        for(int l = 2; l <= n; l++){
            for(int i = 1; i+l-1 <= n; i++){
                int j = i + l - 1;
                dp[i][j] = max(dp[i][j],max(dp[i+1][j],dp[i][j-1]));
                if(a[i] == a[j]) dp[i][j] = max(dp[i][j],dp[i+1][j-1]+2);
            }
        }
        ans = 0;
        for(int i = 1; i <= n; i++){
            ans = max(ans,dp[1][i]+dp[i+1][n]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

方法二:就是使用倍增,因为他是一个环,所以我们把数组扩展乘两倍,然后就可以找每个区间回文序列的最长长度

这样我们枚举区间长度为n的最长回文序列取最大值,这种情况下两个兔子走的方式是从回文的两端相对行走,这样跳跃的次数是回文长度,但是这种情况下枚举的都是不同起点的情况,还有相同起点的情况就枚举起点,那么剩下的部分就是找区间长度为n-1的就可以了

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1010;
int dp[maxn<<1][maxn<<1];
int a[maxn<<1];
int main(){
    int n,ans;
    while(scanf("%d",&n),n){
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
            a[i+n] = a[i];
        }
        for(int i = 1; i <= 2*n; i++){
            dp[i][i] = 1;
        }
        for(int l = 1; l <= 2*n; l++){
            for(int i = 1; i+l <= 2*n; i++){
                int j = i + l;
                dp[i][j] = max(dp[i][j],max(dp[i+1][j],dp[i][j-1]));
                if(a[i] == a[j]) dp[i][j] = max(dp[i][j],dp[i+1][j-1]+2);
            }
        }
        ans = 0;
        for(int i = 1; i <= n; i++){
            ans = max(ans,dp[i][i+n-1]);
        }
        for(int i = 1; i <= n; i++){
            ans = max(ans,dp[i][i+n-2]+1);
        }
        printf("%d\n",ans);
    }
    return 0;
}




【异常】OpenAI提示内容有误 Invalid prompt: your prompt was flagged as potentially violating our usage policy. 用户需优先调整提示内容,确保符合OpenAI的使用政策。若问题持续,建议结合请求ID和具体代码示例联系官方支持。同时,可参考腾讯云、Microsoft Q&A等平台提供的错误处理实践(如重试机制、日志记录)优化代码健壮性。 阅读详情

相关推荐

10分钟快速搭建OpenClaw(基于Windows+飞书)

OpenClaw 是一款功能强大的 AI 智能体工具,能够帮助用户管理邮件、日历等日常事务。本文详细介绍了在 Windows11 系统上安装和配置 OpenClaw 的完整步骤,包括 Node.js 环境搭建、Git 安装、OpenClaw 部署以及飞书机器人创建与对接。通过简单的命令行操作和可视化界面配置,用户可以快速完成 AI 助手部署,实现智能办公自动化。文中还提供了飞书机器人权限设置等关键环节的图文指引,帮助用户轻松完成系统集成。

Hello, New World! 2940

HDU 4745——Two Rabbits【区间DP & 最长回文子序列】

题目传送门 Problem Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and...

NIRVANA REBIRTH 453

深度解析快递柜管理系统的C++实现:设计与实现

本文详细介绍了一个基于C++的快递柜管理系统的设计与实现,包括系统需求分析、架构设计、数据库设计、关键功能模块实现和测试与验证。通过详细的代码示例和理论分析,读者可以全面了解如何开发一个完整的快递柜管理系统。希望本文能够为读者在实际项目开发中提供有价值的参考。

m0_57781768的博客 719

HDU-4745 Two Rabbits(区间DP)

题意:在n个石头围成的环上,两只兔子一只顺时针跳,一只逆时针跳。它们首先各选一个重量相同的石头,再跳的时候可以跨过石头,但不能跳上或跨过已经跳过的石头。两只兔子同时跳到的石头的重量必须相同,且它们可以同时跳到一个石头上。求若满足上述规则,最多可以进行几轮。(n&lt;=1000)在环上的问题,很容易想到破环成链开两倍数组,虽然这题也可以这样做,但常数偏大。由于是环形的问题,我们可以让第一只兔子跳的...

Paulliant的博客 434

Twin Builder CFD 示例:构建、验证和评估静态 ROM

通过我们构建、验证和评估静态 ROM 的综合指南,释放 Twin Builder CFD 的潜力。

jkh920184196的博客 1118

ArcGIS Pro中协同区位熵方法在商业选址分析中的应用

本文详细介绍了如何利用ArcGIS Pro中的协同区位熵方法进行科学的商业选址分析。该方法通过量化目标客户与竞争对手在空间上的关联程度,精准识别市场空白区与竞争红海,帮助决策者从“凭感觉”转向“用数据说话”,有效提升选址成功率和投资回报。

weixin_29214559的博客 365

HDU 4745 Two Rabbits(模型转换最长回文子序列)

传送门啊 两只兔子在一个环形石子上,一个顺时针跳,一个逆时针跳 都不能跳超过一圈. 问,若保证两只兔子每一步都相同,最多跳几步? 思路 首先断环为链 发现如果满足跳的每一步都相同 如果一个兔子跳的是回文串,那么另一只兔子只需要逆着跳回去就好了 如果不是回文串,只能是最后一个数字不回文,这样另一个兔子可以从这个点开始跳… 这样就可以求最长回文子序列了 显然有 f[i][j]=max(f[i+1][j],f[i][j−1])f[i][j]=max(f[i+1][j],f[i][j-1])f[i][j]=max(

jziwjxjd的博客 527

FPGA 核脉冲梯形成型 Verilog 实现:从 MATLAB 到 50MHz 采样率部署

本文详细解析了FPGA实现核脉冲梯形成型的完整流程,从MATLAB仿真到50MHz采样率的Verilog硬件部署。重点探讨了梯形成型算法对负指数信号的优化处理,包括定点数量化、流水线架构设计及资源优化技巧,为核辐射探测与高能物理实验提供高效解决方案。

weixin_30920513的博客 395

hdu 4745 Two Rabbits(最长回文子序列DP)

Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were arr...

邵光亮的博客 796

c语言串口编程详解

串口: 一种异步通信接口,通常用于低速数据传输。RS-232: 最常见的串口标准,定义了电气特性、连接器和信号线。UART: 通用异步收发传输器,是实现串口通信的硬件模块。通过以上内容,你应能够理解并实现C语言在Windows和Linux平台上的串口编程。希望这本手册能帮助你在实际项目中顺利进行串口通信开发。如果有任何疑问或需要进一步的帮助,请随时提问。

jkzyx123的博客 2228

新手必看:C#调用YOLO模型完成图片/视频目标检测(上位机版)

新手学习C#调用YOLO模型,核心是先把基础流程跑通,再逐步优化。图片检测是基础,视频检测就是在图片的基础上加循环和线程,而上位机的实际项目,就是在这个基础上适配工业硬件和业务需求。本文的代码都是新手能直接复制运行的,如果你运行时遇到问题,先检查前期准备是否到位,再看避坑指南。记住,新手别追求一步到位,先实现功能,再慢慢优化性能和界面。等你能熟练调用YOLO模型后,再去研究模型训练、量化优化等进阶内容,会轻松很多。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 992

hdu - 4745 - Two Rabbits(最长回文子序列)

题目链接:传送门 题目大意;两只兔子,在n块围成一个环形的石头上跳跃,每块石头有一个权值ai,一只从左往右跳,一只从右往左跳,每跳一次,两只兔子所在的石头的权值都要相等,在一圈内(各自不能超过各自的起点,也不能再次回到起点)它们最多能经过多少个石头(1 &lt;= n &lt;= 1000, 1 &lt;= ai &lt;= 1000)。 题目思路: 两个方向跳跃,且每一步的值都相同,即求最...

qq_36782366的博客 265

Codex 接入国产大模型完整指南:CC-Switch 与 Codex++ 的工程化选型

Codex 默认走 OpenAI Responses API,而 DeepSeek、GLM、Kimi 等国产模型只提供 OpenAI-compatible 的 Chat Completions——两套协议在消息结构、流式响应、reasoning 字段、tool call 上都不兼容,所以"改个 Base URL + 填国产 Key"这种朴素操作根本跑不通。本文从架构师视角出发,把 Codex 接入国产大模型这件事讲透:先剖析协议链路到底卡在哪,再对比 CC-Switch和 Codex++两套主流方案的差异

少说,多做 3915

hdu 4745 Two Rabbits 区间dp

Two Rabbits Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2845Accepted Submission(s): 1484 Problem Description Long long ago, there li...

weixin_30652879的博客 141

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

HDU 4745 Two Rabbits 区间DP

题目描述:Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were

喵头鹰的博客 839

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

区间dp-hdu-4745-Two Rabbits

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4745 题目意思: 给n个环状的数,A、B两人沿相反的方向走,每单位时间走一步,要求相同时间两人到达相同的数,且同一位置同一个人不能走两次,走过的位置不能越过。 解题思路: 根据回文非连续序列的性质,从前往后,和从后往前序列是一样的,所以只用求出区间内最长的回文序列即可,又由于是环状,所

cc_again的专栏 3418

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

HDU4745 Two Rabbits【区间DP】

题目链接:HDU4745 Two Rabbits 题意:有一段环形字符串,两个人相对而行,要使得他们走过的字母序列相同的最长长度是多少;实际上就是求字符串中最长的回文子序列的长度,最终答案是取长度为n的区间里最长的长度 和 长度为n-1的区间里最长的长度+1 的最大值;+1是因为n-1的区间剩下的那个可以被同时当作起点,注意是环形字符串; 分析:根据差分,dp[l][r]=max(dp[l][...

MaJorieL 264

HDU - 4745 Two Rabbits(区间DP)

题目链接: [Two Rabbits] 大致题意: 有一个环,现在有两只兔子各从一个点开始起跳,一个沿顺时针,另一个沿逆时针,只能在一圈之内跳,并且每次所在的点的大小必须相同,问最多能经过几个点 解题思路: 环状的话可以先倍增改成链这道题目的话就是求最长回文子串 状态初始条件:dp[i][i]=1 (i=0:n-1) 状态转移方程:dp[i][j]=dp[i+1][j-1] + 2 if(str[i]==str[j]) ​ dp[i][j]=max(d

qq_49494204的博客 456

HDU 4745 Two Rabbits (区间dp)

题意:两只兔子,在n块围成一个环形的石头上跳跃,每块石头有一个权值ai,一只从左往右跳,一只从右往左跳,每跳一次,两只兔子所在的石头的权值都要相等,在一圈内(各自不能超过各自的起点,也不能再次回到起点)它们最多能经过多少个石头。 题解:区间dp 其实就是求最长回文子序列长度,对于一个环,可以先求iii到jjj之间的最长回文子序列长度,然后选取一个分割点kkk,这样就不需要再复制一份接到原串后面了,...

gongyuandaye的博客 226

HDU4745 Two Rabbits(区间dp)

Two RabbitsLong long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were arrang

ZYL的博客 382

HDU 4745 Two Rabbits 区间dp

I - Two Rabbits Time Limit:5000MS    Memory Limit:65535KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 4745 Appoint description: Description Long long ago, there liv

MY Blog 1099

HDU 4745 Two Rabbits (区间DP)

Two Rabbits Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1441    Accepted Submission(s): 731 Problem Description Long long ago,

jncsw的博客 362

HDU4745 Two Rabbits(区间DP)

题目链接 Problem Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and t...

qq_42479630的博客 276

HDU4745-Two Rabbits(区间DP)

Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were arranged as a cl...

weixin_43357583的博客 233

HDU 4745 Two Rabbits(区间dp)

这道题相对来说,我觉得就有点考查读题了 读题时一定要细心,大致就是要你求一个回文子序列,递推方程很简单 if(a[i]==a[j]) g[i][j]=max(g[i][j],g[i+1][j-1]+2); else g[i][j]=max(g[i+1][j],g[i][j-1]); 但不过他是环形的,但不过他又说了两只兔子可以同时站在一个石头上。 所以遍历答案是就有同时遍历,长度为n...

KXL5180的博客 193

HDU 4745 Two Rabbits (区间DP)

题意:n个石头排成一个环,每个环有自己的重量,两只兔子从两个石头上出发,每一轮从一个石头跳到另一个石头,一个顺时针,一个逆时针,并且同一时间两只兔子所在的石头重量相等。每只兔子不能跳到并且跳过自己曾在的石头。问最多进行多少轮。 思路: 把n个石头排成一排,再取n个石头和前n个石头一样,成2n个石头。比如说1121变成11211121 然后把这2*n个石头排成一个环,求这个环内的最长回文子串的

Neutralzz的博客 295
上一篇: Palindrome subsequence (区间dp)
下一篇: Messages (思维)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值