题目连接: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. Docker入门书籍

    https://yuedu.baidu.com/ebook/d817967416fc700abb68fca1 精细讲解,入门使用极佳.

  2. linux下配置Tomcat开机启动

    我们在linux下安装好tomcat之后:经常是需要配置到开机启动的: 这样的话就不需要我们每次重启linux服务器之后自己在登陆运行startup.sh文件启动tomcat了 本次的演示环境是在ce ...

  3. echart异步刷新图表,详细配置注释

    echarts刷新技巧: echartData.chear(); //当异步改变数据时,配合echartData .setOption(option)才会有动画效果 echartData.resize ...

  4. jQuery 文档操作方法

    jQuery 文档操作方法 这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). 方法 描述 addClass() 向匹配的元素添加指定的类名. after() 在匹配的元素之 ...

  5. IE下判断IE版本的语句...[if lte IE 8]……[endif]

    <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见   <!--[if lte IE 7]> <![endif]--> ...

  6. 解决SVN造成的桌面图标问号

    之前不小心直接将版本库 的内容检出 到桌面,后才发现桌面上的文件 都变成了问号,本来也以为没有多大问题,删除.svn 即可,可是删除所有的.svn后,桌面上还是显示问号,刷新了很多次,还重启电脑 了, ...

  7. Java源码解读(一)——HashMap

    HashMap作为常用的一种数据结构,阅读源码去了解其底层的实现是十分有必要的.在这里也分享自己阅读源码遇到的困难以及自己的思考. HashMap的源码介绍已经有许许多多的博客,这里只记录了一些我看源 ...

  8. web打印总结

    一.打印样式 区别显示和打印的样式 使用media="print"控制打印时的样式,如下: 打印时不显示打印按钮,设置页面宽度 <style media="prin ...

  9. LAMP源码安装,搭建zabbix监控

    #LAMP#httpd-2.2.32#mysql-5.7.17-linux-glibc2.5-x86_64 二进制压缩版#php5.3.27 1.系统环境优化检查 sed -i 's/SELINUX= ...

  10. 【树链剖分】洛谷P3379 树链剖分求LCA

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...