POJ1159题解报告

一道简单DP,但我好像没用滚动数组,菜得真实

思路:i 从字符串尾n开始,j 从i开始,截取字符串 if(str[i] == str[j])因为字符相等,显然 dp[i][j]  = dp[i+1][j-1]

当字符不相等时dp[i][j] = 1 + min(dp[i+1][j],dp[i][j-1])

//newStart cy 2019 0906
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;//基本DP    最长对应子序列
const int maxn = 5000 + 15;
short dp[maxn][maxn];// i 到 j的字符串最少应该补的字母
int main()
{
    int n;//字符串长度
    cin>>n;
    string str;
    cin>>str;
    for(int i=n-1;i>=0;--i)//从逆序向正序递推
    {
        for(int j=i;j!=n;++j)//比较 (i,j)字符串 (important)
        {
            if(str[i]==str[j])
                dp[i][j] = dp[i+1][j-1];
            else
                dp[i][j] = 1 + min(dp[i+1][j],dp[i][j-1]);
        }
    }
    cout<<dp[0][n-1]<<endl;
}
不怕万人阻挡,只怕自己投降。
原文地址:https://www.cnblogs.com/newstartCY/p/11475080.html