Luogu P1435_Longest Common Subsequence_Ideas are very important

Given a string, find at least how many data to insert to make the string a palindrome

**********************************************************************************

A palindrome string, that is, from left to right, from right to left, the string is still that string

Then invert the palindrome string and find the longest common subsequence with the source string, obviously the result is the length of the string

And now ask how many data are inserted at least to make the string a palindrome ,

First invert the string, and then find the longest common subsequence with the source string. The result is ret, then the minimum character to be inserted is the difference between the length of the string and ret

**********************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1010;

char a[maxn], b[maxn];
int dp[maxn][maxn] = {0};
int len;

intmain()
{
    int i, j;

    scanf("%s", a);
    len = strlen (a);
    for(i = 0; i < len; ++i) b[i] = a[len-i-1];
    for(i = 1; i <= len; ++i)
        for(j = 1; j <= len; ++j)
    {
        dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        if(a[i-1] == b[j-1])
            dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1);
    }
    int ans = len - dp [len] [len];
    printf("%d\n", ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726371&siteId=291194637