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

Palindrome

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

Total Submission(s): 3265    Accepted Submission(s): 1130

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

策略: RT

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
const int M = 5050;
using namespace std;

int dp[2][M];
char a[M], b[M];
int n;

int main(){
    while(scanf("%d", &n) == 1){
        scanf("%s", a);
        int i, j;
        for(i = n-1; i >= 0; i --) b[n-1-i] = a[i];
        memset(dp, 0, sizeof(dp));
        for(i = 1; i <= n; i ++){
            for(j = 1; j <= n; j ++){
                if(a[i-1] == b[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1] + 1;
                else dp[i%2][j] = max(dp[(i-1)%2][j], dp[i%2][j-1]);
            }
        }
        printf("%d\n", n-dp[n%2][n]);
    }
    return 0;
}
时间: 2024-12-13 09:47:16

hdoj 1513 Palindrome 【LCS】+【滚动数组】的相关文章

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

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

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

题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求LCS,这样公共的就求出来了,那么再用总数减掉这个LCS, 那么剩下的肯定就是没有配对的了,就得必须加上了. 思路有了,这里又出现一个问题,这个求LCS,要用的空间复杂度太大了,MLE了...有了思路,还是过不了, 这个题应该用滚动数组来做,我想想在求LCS时,第一维我们只用到了i-1和i,所以呢,

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

hdu 1513 添最少字回文 (Lcs 滚动数组)

http://blog.csdn.net/ice_crazy/article/details/8244639 这里5000*5000超出内存,所以需要用滚动数组: 用一个now表示当前的结果,pre表示前一个的结果,不断滚动即可 #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cma

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

hdoj 1513 Palindrome

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

poj - 1159 - Palindrome(滚动数组dp)

题意:一个长为N的字符串( 3 <= N <= 5000),问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->>状态:dp[i][j]表示第i个字符到第j个字符组成的字符串变成回文串的最少插入次数. 状态转移方程: 若sz[i] == sz[j],则:dp[i][j] = dp[i + 1][j - 1]; 否则:dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1; 提交,5

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