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