题目连接: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. SQL基本查询_子查询(实验四)

    SQL基本查询_子查询(实验四) 1.查询所有员工中薪水低于"孙军"的员工姓名和薪水: 2.查询与部门编号为"01"的岗位相同的员工姓名.岗位.薪水及部门号: ...

  2. softmax分类算法原理(用python实现)

    逻辑回归神经网络实现手写数字识别 如果更习惯看Jupyter的形式,请戳Gitthub_逻辑回归softmax神经网络实现手写数字识别.ipynb 1 - 导入模块 import numpy as n ...

  3. lesson - 5 Linux用户和组管理

    1. /etc/passwd由 : 分隔成7个字段(1) 用户名 规则:大小写字母.数字.减号(不能出现在首位).点以及下划线,其他字符不合法 (2) x 放密码,安全起见放到 /etc/shadow ...

  4. React Native出现"Native module cannot be null"问题

    经查跟PushNotification有关,需要手动完成Linking. 两步解决此问题: 配置Linking Libraries:https://facebook.github.io/react-n ...

  5. Linux(CentOS6.5)修改系统市区被中国标准时间(北京时间)

    本文地址http://comexchan.cnblogs.com/ ,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 备份时区配置文件 cp /etc/localtime /etc/l ...

  6. java递归实现文件夹文件的遍历输出

    学习java后对一个面试小题(今年年初在团结湖面试的一个题目) 的习题的编写. ''给你一个文件,判断这个文件是否是目录,是目录则输入当前目录文件的个数和路径,''' /** * @author li ...

  7. Linux 文本编辑器vi命令

    1.Vim Vim  是一个功能强大的全屏幕文本编辑器,是 Linux/UNIX 上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim 没有菜单,只有命令 2.Vim 工作模式 3.插入 ...

  8. asp.net 限制上传文件的大小与时间

    在web.Config文件中配置限制上传文件大小与时间的字符串是在<httpRuntime><httpRuntime/>节中完成. maxRequsetLength 属性:用于 ...

  9. jquery.cookie的path坑

    在使用jquery.cookie设置cookie的时候,通常都是直接设置,没有针对path,domain和expires等进行具体的设置,这会导致,同一个cookie的key对应多个value. 1. ...

  10. CentOS7下安装MySQL并配置远程连接

    一.CentOS7下安装MySQL数据库 CentOS7默认的安装包里面已经没有 MySQL-Server安装包了,远程镜像中也没有了. 默认的是MariaDB (MySQL的一个分支,开发这个分支的 ...