Nyoj palindrome string (LCS)

Source: Internet
Author: User

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)

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.