HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)
题意
在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的。
思路
从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的。
倍长原序列,在新序列中求所有区间的最长回文子序列长度(一般子序列就表示不是连续的串)。
答案就等于所有长度为n的区间中最长回文的长度 和 所有长度为n-1的区间中最长回文的长度+1(在轴上的两点可不同) 中最大的那个。
【求最长回文子序列】:设dp[i][j]表示[i,j]区间内的最长回文子序列,则dp[i][j] = max(dp[i+1][j], dp[i][j-1], (if a[i]==a[j])dp[i+1][j-1]).
代码
[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;
int a[2005];
int dp[2005][2005];
int main(){
int n;
while(scanf("%d", &n), n){
for (int i = 1; i <= n; i ++){
scanf("%d", &a[i]);
a[i+n] = a[i];
}
int n1 = n + n;
MEM(dp, 0);
for (int i = 1; i <= n1; i ++) dp[i][i] = 1;
for (int len = 1; len < n1; len ++){
for (int i = 1; i + len <= n1; i ++){
int j = i + len;
dp[i][j] = max(dp[i+1][j], max(dp[i][j-1], a[i] == a[j]?dp[i+1][j-1]+2:0));
}
}
int res = 0;
for (int i = 1; i <= n; i ++) res = max(res, dp[i][i+n-1]);
for (int i = 1; i <= n; i ++) res = max(res, dp[i][i+n-2]+1);
printf("%d\n", res);
}
return 0;
}
[/cpp]
HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)的更多相关文章
- leetcode 730. 统计不同回文子序列(区间dp,字符串)
题目链接 https://leetcode-cn.com/problems/count-different-palindromic-subsequences/ 题意 给定一个字符串,判断这个字符串中所 ...
- 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
1.问题描述 给定一个字符串(序列),求该序列的最长的回文子序列. 2.分析 需要理解的几个概念: ---回文 ---子序列 ---子串 http://www.cnblogs.com/LCCRNblo ...
- NOIP2016提高组初赛(2)四、读程序写结果3、求最长回文子序列
#include <iostream> using namespace std; int lps(string seq, int i, int j) { int len1, len2; i ...
- 最长回文子序列(LPS)
问题描述: 回文是正序与逆序相同的非空字符串,例如"civic"."racecar"都是回文串.任意单个字符的回文是其本身. 求最长回文子序列要求在给定的字符串 ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- Leetcode 516.最长回文子序列
最长回文子序列 给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 示例 1:输入: "bbbab" 输出: 4 一个可能的最长回文子序列为 " ...
- 简单动态规划——最长公共子序列&&最长回文子序列&&最长上升||下降子序列
最长公共子序列,顾名思义当然是求两个字符串的最长公共子序列啦,当然,这只是一道非常菜的动规,所以直接附上代码: #include<iostream> #include<cstdio& ...
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
随机推荐
- JQuery变量数字相加的研究
在 jquery中,一个变量和一个数字相加,期望得到1+2=3但是: eg: <input type="text" class="input_Num" i ...
- webService 总结
Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...
- css3有哪些新特性
转载:http://blog.csdn.net/lxcao/article/details/52797914
- VS 和 Eclipse 的一些快捷键记录
VS: 自动排版: Ctrl+K+F 选项卡之间的切换: Ctrl+Tab 统一缩进: Shift+Tab Ctrl + M + O: 折叠所有方法 Ctrl + M + L: 展开所有方法 Ecli ...
- design thinking
- PHP 日期格式中 Y与y
大写Y效果: $nowtime = time(); $nowtime=date("Y-m-d",$nowtime); 结果: 2015-10-24 小写y效果: $nowtime ...
- Centos75 解决Nginx出现403 forbidden(13: Permission denied)
Centos75 新安装的vm,nginx出现403 forbidden 一般为SELinux设置为开启状态(enabled)的原因 切为root ,执行: sed -i 's/SELINUX=enf ...
- 20145331《Java程序设计》第1周学习总结
20145331<Java程序设计>第1周学习总结 教材学习内容总结 第一章 1.java的三大平台分别为java SE.java EE.java ME,其中java SE是基础. 2.j ...
- Linux文件系统学习(一)之相关概念⭐⭐⭐
“一切皆是文件”是 Unix/Linux 的基本哲学之一.不仅普通的文件,目录.字符设备.块设备.套接字等在 Unix/Linux 中都是以文件被对待:它们虽然类型不同,但是对其提供的却是同一套操作界 ...
- Hive的执行生命周期
1.入口$HIVE_HOME/bin/ext/cli.sh 调用org.apache.hadoop.hive.cli.CliDriver类进行初始化过程 处理-e,-f,-h等信息,如果是-h,打印提 ...