P1435 [IOI2000] 回文字串 / [蓝桥杯 2016 省] 密码脱落


https://www.luogu.com.cn/problem/P1435


动态规划,LCS


黄色题
字符串输入下标从0开始!!!!!!!!!!!!!!!!!!! 


#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
int n;
int dp[5001][5001];
char str1[5001],str2[5001];
int main()
{
    //freopen("palindrome.in", "r", stdin);
    //freopen("palindrome.out", "w", stdout);
    scanf("%s", str1+1);
    n = strlen(str1+1);
    for(int i = 1; i <= n; i++)
        str2[i] = str1[n-i+1];                                //做一个逆序的字符串数组 
    for(int i = 1; i<=n; i++)
        for(int j = 1; j <= n; j++)
            if(str1[i] == str2[j])
                dp[i][j] = dp[i-1][j-1] + 1;        //最长公共自序列匹配 
            else
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);        //不匹配的往下匹配状态 
    printf("%d/n", n-dp[n][n]);                        //字符串长度减去匹配出的最长公共自序列的值 
    return 0;                                        //即需要添加的字符数 
}

 

原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/277267.html

(0)
上一篇 2022年7月27日 09:40
下一篇 2022年7月27日 10:06

相关推荐

发表回复

登录后才能评论