题意

在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的。

思路

从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的。

倍长原序列,在新序列中求所有区间的最长回文子序列长度(一般子序列就表示不是连续的串)。

答案就等于所有长度为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)的更多相关文章

  1. leetcode 730. 统计不同回文子序列(区间dp,字符串)

    题目链接 https://leetcode-cn.com/problems/count-different-palindromic-subsequences/ 题意 给定一个字符串,判断这个字符串中所 ...

  2. 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )

    1.问题描述 给定一个字符串(序列),求该序列的最长的回文子序列. 2.分析 需要理解的几个概念: ---回文 ---子序列 ---子串 http://www.cnblogs.com/LCCRNblo ...

  3. NOIP2016提高组初赛(2)四、读程序写结果3、求最长回文子序列

    #include <iostream> using namespace std; int lps(string seq, int i, int j) { int len1, len2; i ...

  4. 最长回文子序列(LPS)

    问题描述: 回文是正序与逆序相同的非空字符串,例如"civic"."racecar"都是回文串.任意单个字符的回文是其本身. 求最长回文子序列要求在给定的字符串 ...

  5. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  6. [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  7. Leetcode 516.最长回文子序列

    最长回文子序列 给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 示例 1:输入: "bbbab" 输出: 4 一个可能的最长回文子序列为 " ...

  8. 简单动态规划——最长公共子序列&&最长回文子序列&&最长上升||下降子序列

    最长公共子序列,顾名思义当然是求两个字符串的最长公共子序列啦,当然,这只是一道非常菜的动规,所以直接附上代码: #include<iostream> #include<cstdio& ...

  9. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

随机推荐

  1. Found multiple occurrences of org.json.JSONObject on the class path:

    Question: Found multiple occurrences of org.json.JSONObject on the class path: jar:file:/C:/Users/nm ...

  2. Pycharm 2018 1.2版本 Mac注册码激活码

    此链接中较为详细的解决了次问题:http://www.orsoon.com/Mac/159477.html

  3. js自动类型转换

    <script> testeq0 = null == undefined; testeq1 = "0" == 0; //比较前字符串转换为数字 testeq2 = 0 ...

  4. Linux系统——awk命令

    awk命令不仅仅是Linux系统的命令,也是一种编程语言,用来处理数据和生成报告(Exel),处理的数据可以是一个或多个文件(标准输入和管道获取标准输入).可在命令行上编辑操作,也可以写成awk程序运 ...

  5. HashMap底层原理

    原文出自:http://zhangshixi.iteye.com/blog/672697 1.    HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射 ...

  6. Salesforce中Html的转义,InputField和RemoteAction

    在Salesforce的开发中,有时候需要在对象中插入记录,其中有的字段需要插入Html,但是对于输入Html的域,大多数框架和网站都需要做Html的转义处理,防止XSS或者SQL注入攻击.有时候我们 ...

  7. CCProgressTimer Loading条

    CCProgressTimer *p_SpTime = CCProgressTimer::create(CCSprite::createWithSpriteFrameName("IM_Pro ...

  8. python的socket网络编程(二)

    (注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 国庆八天假,已过去3天了,加上明天又是中秋,还是决定在今晚把之前想写的东西写完.国庆节在宁波老家,吃好喝好睡好,就是没有好好 ...

  9. Linux各目录缩写含义

    Unix已经有35年历史了.许多人认为它开始于中世纪,这个中世纪是相对于计算机技术的产生和发展来说的.在过去的时间里,Unix和它的子分支Linux收集有许多的历史和一些完全古老的语言.在这篇技巧文章 ...

  10. CSS 图片廊

    CSS 图片廊 一.示例一 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...