"Dynamic Planning" palindrome string time limit: 1 Sec memory limit: MB
Title Description
The so-called Palindrome string, is a string, from left to right reading and right-to-left reading is exactly the same, such as "ABA". Of course, the question we give you is no longer as simple as judging whether a string is a palindrome string. Now ask you, give you a string, you can add characters anywhere, at least add a few more characters, you can make this string a palindrome string.
Input
The first line gives the integer N (0<n<100)
The next n rows, one string per line, and no more than 1000 per string length.
Output
Minimum number of characters to be added per line of output
Sample input
1ab3bd
Sample output
2
Analysis: To insert a minimum number of characters to make a palindrome string is to find the maximum palindrome length in the input string and then subtract the maximum length by the total size is the number of characters to be asked
DP[I][J] represents the maximum length from the I position of the input string to the J position
if (C[i-1]==c[j-1]) dp[i][j]=dp[i-1][j-1]+1
else Dp[i][j]=max (Dp[i-1][j],dp[i][j-1]
1#include <iostream>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 intN;8 Charc[1111],d[1111];9 intdp[1111][1111];Ten One intMain () A { - while(cin>>N) - { the while(n--) - { -Cin>>C; - intlen=strlen (c); +Memset (DP,0,sizeof(DP)); - for(intI=0, j=len-1; i<len;i++,j--) +d[j]=C[i]; A at for(intI=1; i<=len;i++)//Dp[i][j] need to start from 0 - { - for(intj=1; j<=len;j++) - { - if(c[i-1]==d[j-1])//C[i-1] d[j-1] from 0 to len-1 -dp[i][j]=dp[i-1][j-1]+1; in Else -Dp[i][j]=max (dp[i-1][j],dp[i][j-1]); to } + } -cout<<len-dp[len][len]<<Endl; the } * } $ return 0;Panax Notoginseng}
"Dynamic Planning" palindrome string