HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)

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

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

那么最后正反都得是一样的,所以我们就正反求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;
}
时间: 2024-10-03 23:10:18

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

POJ 1159 回文LCS滚动数组优化

详细解题报告可以看这个PPT 这题如果是直接开int 5000 * 5000  的空间肯定会MLE,优化方法是采用滚动数组. 原LCS转移方程 : dp[i][j] = dp[i - 1][j] + dp[i][j -1] 因为 dp[i][j] 只依赖于 dp[i - 1][j] 和 dp[i][j - 1] 所以可以采用滚动数组如下: dp[i % 2][j] = dp[(i - 1) % 2][j] + dp[i % 2][j - 1] 可以实现节省空间的方法 答案存储在 dp[n % 2

POJ 1159 Palindrome(DP LCS)

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 t

POJ 1159 Palindrome(lcs加滚动数组)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52350   Accepted: 18041 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

POJ 1159 Palindrome DP

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 51913   Accepted: 17877 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

POJ - 1159 - Palindrome (LCS + 优化)

题目传送:Palindrome 思路:一看题目思路很清晰,就是求出字符串s和倒转s后的字符串t的最长公共子序列,但是一看空间开销有点大,如果开int就会爆,5000*5000有100MB了,这里可以开short int,差不多正好可以过去,还有一种做法就是弄一个滚动数组,因为求LCS,根据状态转移方程可以知道,只需要前一行和当前行就行了,所以开个2*5005就OK了,具体看代码 AC代码①: #include <cstdio> #include <cstring> #include

hdoj 1513 Palindrome【LCS+滚动数组】

Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3918    Accepted Submission(s): 1340 Problem Description A palindrome is a symmetrical string, that is, a string read identically from

LCS(滚动数组) POJ 1159 Palindrome

题目传送门 1 /* 2 LCS裸题:长度减去最大相同长度就是要插入的个数 3 dp数组二维都开5000的话就会超内存,这里就用到了滚动数组, 4 因为在LCS的计算中,i的变化只相差1,所以可以通过对2取余来进行滚动:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <algorithm> 10 #include <string> 1

poj 1159 Palindrome lcs+滚动数组

点击打开链接题目链接 Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52910   Accepted: 18248 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

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

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 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