http://acm.hdu.edu.cn/showproblem.php?pid=4745

题意:

有一个环,现在有两只兔子各从一个点开始起跳,一个沿顺时针,另一个沿逆时针,只能在一圈之内跳,并且每次所在的点的大小必须相同,问最多能经过 几个点。

思路:
环状的话可以先倍增改成链。

这道题目的话就是求最长回文子串,它的求法是这样的:

设字符串为str,长度为n,p[i][j]表示第i到第j个字符间的子序列的个数(i<=j),则:

状态初始条件: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(dp[i+1][j],dp[i][j-1])  if (str[i]!=str[j])

最后找最大值的时候,除了找区间长度为n的,还要找区间长度为n-1的,此时的情况是两只兔子从同一个起点出发。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n;
int a[maxn];
int d[maxn][maxn]; int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d",&n) && n)
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i+n]=a[i];
d[i][i]=d[i+n][i+n]=;
} for(int r=;r<=n;r++)
{
for(int i=;i+r-<=*n;i++)
{
int j=i+r-;
if(a[i]!=a[j]) d[i][j]=max(d[i+][j],d[i][j-]);
else if(a[i]==a[j]) d[i][j]=d[i+][j-]+;
}
} int ans=;
for(int i=;i<=n;i++)
{
ans=max(ans,d[i][i+n-]);
ans=max(ans,d[i][i+n-]+); //两只兔子从同一个起点出发
}
printf("%d\n",ans);
}
return ;
}

HDU 4745 Two Rabbits(最长回文子序列)的更多相关文章

  1. HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)

    题意 在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的. 思路 从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的. 倍长原序列, ...

  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 ...

  10. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

随机推荐

  1. 【BZOJ5099】[POI2018]Pionek 几何+双指针

    [BZOJ5099][POI2018]Pionek Description 在无限大的二维平面的原点(0,0)放置着一个棋子.你有n条可用的移动指令,每条指令可以用一个二维整数向量表示.每条指令最多只 ...

  2. c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)

    Have you been in need of converting mp3 audios to wav audios?  If so, the skill in this article prov ...

  3. java类的初始化的默认值

    转自:http://blog.csdn.net/abc5382334/article/details/18254517 对于类的成员变量   不管程序有没有显示的初始化,Java  虚拟机都会先自动给 ...

  4. Hibernate数据类型映射

    Hibernate映射类型分为两种:内置的映射类型和客户化映射类型.内置映射类型负责把一些常见的Java类型映射到相应的SQL类型:此外,Hibernate还允许用户实现UserType或Compos ...

  5. PHP中文字数限制:中文字符串截取(mb_substr)

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...

  6. Hive桶列BucketedTables

    The CLUSTERED BY and SORTED BY creation commands do not affect how data is inserted into a table – o ...

  7. 三维凸包求内部一点到表面的最近距离(HDU4266)

    http://acm.hdu.edu.cn/showproblem.php?pid=4266 The Worm in the Apple Time Limit: 50000/20000 MS (Jav ...

  8. pta习题集5-16 地下迷宫探索

    地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智. ...

  9. MyBatis 内置日志工厂基于运行时自省机制选择合适的日志工具

    mybatis – MyBatis 3 | 日志 http://www.mybatis.org/mybatis-3/zh/logging.html MyBatis 内置日志工厂基于运行时自省机制选择合 ...

  10. JIRA licence crack and etc

    https://my.oschina.net/u/199525/blog/313788 http://blog.csdn.net/joinandjoin/article/details/9052785 ...