Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1513
Test instructions: The minimum number of added characters to turn a string into a palindrome
Analysis: As long as the thought of the string in reverse order and the original string to find the longest common sub-sequence, the least added number is LEN-LCS, this problem is a bare LCS.
It is still up to you to scroll through the array to optimize space.
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 10010using namespacestd;Chars1[5110],s2[5110];intdp[2][5110];intMain () {intN; while(SCANF ("%d", &n) >0) {scanf ("%s", S1); for(intI=1; i<=n; i++) {S2[i-1]=s1[n-i]; } memset (DP,0,sizeof(DP)); intCur=0, nxt=1; for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) { if(s1[i-1]==s2[j-1]) Dp[nxt][j]=dp[cur][j-1]+1; ElseDp[nxt][j]=max (dp[nxt][j-1],dp[cur][j]); } swap (CUR,NXT); } printf ("%d\n", N-Dp[cur][n]); }}View Code
hdu1513 (longest common sub-sequence)