poj1159 dp(滚动数组优化)

H - 简单dp 例题扩展

Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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
题目大意:给你一个字符串,让你往其中插入字符,使其变成回文字符串,问最少需要插入多少字符。
思路分析:所谓回文字符串,就是正读和倒读一样的,因此可以考虑构造两个字符串,另一个字符串是
原字符串的逆序串,然后求最长公共子序列长度,那么字符串长度减去最长公共字符串长度就是需要插入
的字符数,注意dp数组开short,否则会爆内存。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
const int maxn=5000+100;
short dp[maxn][maxn];
int main()
{
    int n;
    char s1[maxn],s2[maxn];
    scanf("%d",&n);
       cin>>s1;
       for(int i=n-1;i>=0;i--)
        s2[n-i-1]=s1[i];
       memset(dp,0,sizeof(dp));
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               if(s1[i]==s2[j])
               {
                   if(i>=1&&j>=1)
                    dp[i][j]=dp[i-1][j-1]+1;
                   else dp[i][j]=1;
               }
               if(s1[i]!=s2[j])
               {
                   if(i==0&&j==0) dp[0][0]=0;
                   else if(i>=1&&j==0) dp[i][j]=dp[i-1][j];
                   else if(i==0&&j>=1) dp[i][j]=dp[i][j-1];
                   else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
               }
           }
       }
       cout<<(n-dp[n-1][n-1])<<endl;
    return 0;
}
优化:如果开数组dp[maxn][maxn],非常耗内存,如果不用short开就会MLE.但是我们注意到,
dp[i][j]只与上一层的dp[i-1][j-1]或dp[i-1][j]有关,因此可以采用就地滚动的方法节省
内存,只需要开一个dp[2][maxn]的数组,同时,字符串在储存的时候也有技巧,推荐从1-n储存,
这样dp边界处理起来就容易的多。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
const int maxn=5000+100;
const int inf=0xfffffff;
short dp[2][maxn];//滚动数组
int main()
{
    int n;
    char s1[maxn],s2[maxn];
    scanf("%d",&n);
       scanf("%s",s1+1);
       for(int i=n;i>=1;i--)
        s2[n-i+1]=s1[i];
       memset(dp,0,sizeof(dp));
       int ma=-inf;
       for(int i=1;i<=n;i++)
       {
           for(int j=1;j<=n;j++)
           {
               if(s1[i]==s2[j])
               {
                    dp[i%2][j]=dp[(i-1)%2][j-1]+1;
               }
               if(s1[i]!=s2[j])
               {
                    dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);
               }
               if(dp[i%2][j]>ma) ma=dp[i%2][j];
           }
       }
       cout<<(n-ma)<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/xuejianye/p/5475188.html