最长回文子序列

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char str[1005];
int f[1005][1005];

int main(){
    scanf("%s",str);
    int len=strlen(str);
        for(int i=0;i<len;i++) f[i][i]=1;
        
        for(int l=2;l<=len;l++){
        for(int i=0;i+l-1<len;i++){
            int j=i+l-1;
            if(str[i]==str[j]){
                f[i][j]=f[i+1][j-1]+(i==j?1:2);
            }else{
                f[i][j]=max(f[i+1][j],f[i][j-1]);
            }
        }
    }
    printf("%d
",f[0][len-1]);
    return 0;
}
原文地址:https://www.cnblogs.com/Roni-i/p/8643837.html