Palindrome POJ - 1159 (dp+滚动数组)

思路

因为n<=5000,n^2的复杂度可以接受。
题目等价于求n- 最长回文子串长度,也等价于求s和s的逆串的最长公共子序列,
dp[i][j],即可求出。
考虑到内存问题,状态转移只和前一个i有关,所以直接记录前面一个状态就行。

代码

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
const int maxn=5e3+8;
typedef long long ll;
int n;
char s[maxn];
int dp[2][maxn];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    scanf("%d",&n);
    scanf("%s",s+1);
    char s2[maxn];
    for(int i=1;i<=n;i++) s2[i]=s[n-i+1];
    int f=0;
    for(int i=1;i<=n;i++)
    {
        f=f^1;
        for(int j=1;j<=n;j++)
        {
            if(s[i]==s2[j])
            {
                dp[f][j]=dp[f^1][j-1]+1;
            }
            else
            {
                dp[f][j]=max(max(dp[f^1][j],dp[f][j-1]),dp[f][j]);
            }
        }
    }
    printf("%d\n",n-dp[f][n]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/82667026
今日推荐