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

Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4532    Accepted Submission(s): 1547

Problem Description
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.

 
Input
Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.
 
Output
Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.
 
Sample Input
5
Ab3bd
 
Sample Output
2
 
Source

题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符

思路:将该字符串与其反转求一次LCS,然后所求就是n减去最长公共子串的长度,但是要注意这里字符串最长有5000,dp数组二维都开5000的话就会超内存,这里就用到了滚动数组,因为在LCS的计算中,i的变化只相差1,所以可以通过对2取余来进行滚动

LCS: 求连个串s1,s2的最长公共自序列:dp[i][j] 表示扫描到第一个串的第i个位置第二个串的第j个位置的最长公共子序列。 当s1[i]==s2[j]时,dp[i][j] = d[i-1][j-1]+1;

当s1[i]!=s2[j]时,dp[i][j] = max(dp[i-1][j],dp[i][j-1]);

空间优化: 通过上面的转移方程可以看出来dp[i][j]的状态之和上一列和当前列有关系,所以可以通过二维滚动数组的形式来储存,通过i的奇偶来控制

下面是代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
char s1[N],s2[N];
int dp[][N];
int n; void LCS()
{
int i,j;
memset(dp,,sizeof(dp));
for(i = ; i<=n; i++)
{
for(j = ; j<=n; j++)
{
int x = i%;
int y = -x;
if(s1[i-]==s2[j-])
dp[x][j] = dp[y][j-]+;
else
dp[x][j] = max(dp[y][j],dp[x][j-]);
}
}
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
getchar();
scanf("%s",s1);
for(i = ; i < n; i++)
s2[i] = s1[n--i];
//s2[i] = '\0';
LCS();
printf("%d\n",n-dp[n%][n]);
}
return ;
}

LCS最长公共子序列~dp学习~4的更多相关文章

  1. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  2. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  3. 动态规划模板2|LCS最长公共子序列

    LCS最长公共子序列 模板代码: #include <iostream> #include <string.h> #include <string> using n ...

  4. LCS 最长公共子序列

    区别最长公共子串(连续) ''' LCS 最长公共子序列 ''' def LCS_len(x, y): m = len(x) n = len(y) dp = [[0] * (n + 1) for i ...

  5. LCS最长公共子序列(最优线性时间O(n))

    这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...

  6. LCS最长公共子序列HDU1159

    最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...

  7. POJ 2250(LCS最长公共子序列)

    compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  8. Atcoder F - LCS (DP-最长公共子序列,输出字符串)

    F - LCS Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are gi ...

  9. 最长公共子序列 DP

    class Solution: def LCS(self,A,B): if not A or not B: #边界处理 return 0 dp = [[0 for _ in range(len(B)+ ...

随机推荐

  1. C# VS2010结合SQL Server 2008数据库编程实现方法

    SQL Server 数据库在C#编程中经常用到,如何实现在具体项目中数据库和具体应用的结合是我们经常遇到的问题,我们这次主要针对如何使用SQL Server 数据库展开,下面是具体的操作以及简单的代 ...

  2. [置顶] xamarin android Fragment实现底部导航栏

    前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment  Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...

  3. ArcGIS 网络分析[1.3] 在个人地理数据库中创建网络数据集/并简单试验最佳路径

    上篇使用shp文件创建网络数据集,然而在ArcGIS 9中就支持地理数据库了,数据库的管理更为科学强大. 本篇就使用个人地理数据库进行建立网络数据集,线数据仍然可以是1.1中的线数据,但是我做了一些修 ...

  4. php date函数

    PHP星期几获取代码: 1 date("l"); 2 //data就可以获取英文的星期比如Sunday 3 date("w"); 4 //这个可以获取数字星期比 ...

  5. 关于git的一些理论知识

    一.什么是版本控制器 好多刚用git的coder一说起git,就随口会说出版本控制器嘛,我问那是干嘛的,大部分人就回答上传代码的.然后会用,但是有些理论你问他们他们就不知道了,比如不是代码的文件就不能 ...

  6. Java中list<Object[]>、list<Student>、list<Map<String,String>>排序

    1:list<Object[]>的排序   public static void main(String[] args) { // TODO Auto-generated method s ...

  7. C# DataGridView中DataGridViewComboBoxCell列,下拉框事件的处理【完美解决】

    http://blog.csdn.net/a312100321/article/details/25195311 问题:DataGridView绑定数据源之后,有一列需要用下拉框DataGridVie ...

  8. Oracle PL/SQL Developer集成TFS进行团队脚本文件版本管理

    对于传统的使用关系型数据库的大型软件产品,后台数据库的持续开发和维护可能会产生大量的脚本文件,针对这些脚本文件应该怎样比较方便的进行版本管理,以及如何快捷的在团队之间进行权限等协作管理呢?不同的数据库 ...

  9. Spring之设置Bean值

    Java实例的属性值可以有很多种数据类型.基本类型值.字符串类型.java实例甚至其他的Bean实例.java集合.数组等.所以Spring允许通过如下几个元素为Bean实例的属性指定值: value ...

  10. php 面向对象三大特点:封装、继承、多态

    在讲解这三大特性前,我们先讲访问修饰符. php中有3中访问修饰符:public protected private: public:表示公有的:可在本类.子类.对象实例中访问. protected: ...