Palindrome String time limit: theMs | Memory Limit:65535KB Difficulty:4
-
-
Describe
-
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
-
-
-
-
Ideas:
-
-
The length of the LCS of the original string and the crossdress is calculated first, and the Ans=len-lcs.lenth
-
-
code:
-
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;char S[1005];char ss[ 1005];int Dp[1005][1005];int Main () { int t; scanf ("%d", &t); while (t--) { scanf ("%s", s+1); int Len=strlen (s+1);//Note to write s+1 int k=1; for (int i=len;i>=1;i--) { ss[k++]=s[i]; } Memset (Dp,0,sizeof (DP)); for (int i=1;i<=len;i++) {for (int j=1;j<=len;j++) { if (S[i]==ss[j]) { dp[i][ j]=dp[i-1][j-1]+1; } else { Dp[i][j]=max (dp[i-1][j],dp[i][j-1]); }}} int Ans=len-dp[len][len]; printf ("%d\n", ans); } return 0;}
-
-
Nyoj palindrome string (LCS)