"Dynamic Planning" palindrome string

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.