题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数。

析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串,

那么最后正反都得是一样的,所以我们就正反求LCS,这样公共的就求出来了,那么再用总数减掉这个LCS,

那么剩下的肯定就是没有配对的了,就得必须加上了。

思路有了,这里又出现一个问题,这个求LCS,要用的空间复杂度太大了,MLE了。。。有了思路,还是过不了,

这个题应该用滚动数组来做,我想想在求LCS时,第一维我们只用到了i-1和i,所以呢,我们第二维只要开2行就够了,

不断的来回存储,第一行和第二行反复,这样就OK了,至此这个题也就解决了。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std;
const int maxn = 5000 + 10;
int d[2][maxn];
char s[maxn], t[maxn]; int main(){
int n;
while(~scanf("%d", &n)){
scanf("%s", s+1);
for(int i = 1; i <= n; ++i)
t[i] = s[n-i+1];
t[n+1] = '\0';
memset(d, 0, sizeof(d)); for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j){
int x = i % 2;
int y = 1 - x;
if(s[i] == t[j]) d[x][j] = d[y][j-1] + 1;
else d[x][j] = max(d[y][j], d[x][j-1]);
} printf("%d\n", n - d[n%2][n]);
}
return 0;
}

HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)的更多相关文章

  1. POJ 1159 回文LCS滚动数组优化

    详细解题报告可以看这个PPT 这题如果是直接开int 5000 * 5000  的空间肯定会MLE,优化方法是采用滚动数组. 原LCS转移方程 : dp[i][j] = dp[i - 1][j] + ...

  2. poj 1159 Palindrome 【LCS】

    任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...

  3. hdu 1513 Palindrome【LCS滚动数组】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

  5. hdoj 1513 Palindrome【LCS+滚动数组】

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...

  7. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  8. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  9. POJ - 1159 Palindrome(dp-回文变形)

    d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长 ...

随机推荐

  1. Containerpilot 配置文件reload

    containerpilot -reload -config ./containerpilot.json 进程号不变,重新加载配置文件

  2. Android中WebView使用全解

    开始 在Android系统中内嵌的WebKit,这是一个浏览器内核,它帮助着我们可以浏览网页.在实际开发中,如果你想让你的App能够访问网页,那就需要用到WebView这个控件. 如何使用? 其实使用 ...

  3. Required String parameter ' ' is not present

    Required String parameter ' ' is not present 报错原因: url中的参数错误. 解决方法: 1.修正url中的参数的值. 2.在Controller层中的@ ...

  4. hdoj1087 (DP--LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:这题很简单了,纯LIS的解法,没有一点变形,由于数据小,使用O(n^2)LIS解法就足够了 ...

  5. np.hsplit()

    numpy.hsplit numpy.hsplit(ary, indices_or_sections)[source] Split an array into multiple sub-arrays ...

  6. Inside Triangle

    Inside Triangle https://hihocoder.com/contest/hiho225/problem/1 时间限制:10000ms 单点时限:1000ms 内存限制:256MB ...

  7. 转 Appium for Mac 环境准备篇

    转发地址:http://www.cnblogs.com/oscarxie/p/3894559.html 1. 爬墙因为后续安装过程中可能会碰到墙的问题,所以首先得解决爬墙的问题.我的方便,公司提供代理 ...

  8. 【校招面试 之 C/C++】第5题 C++各种构造函数的写法

    构造函数 ,是一种特殊的方法 .主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 .特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数 ...

  9. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  10. 11-matlba-bellman-ford;地杰斯特拉

    求最短路: 1.bellman-ford: %求s到各点的最短距离 function Dist = Bellman_Ford(s) load cityJuli; for i = 1:154 Dist( ...