Nyoj 37 Text string

Source: Internet
Author: User

palindrome Stringtime limit: ms | Memory limit:65535 KBDifficulty: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


Analysis: The original string is reversed first, then the inverse of the string and the original string of the longest common sub-sequence .


Code:

<span style= "font-size:18px;" > #include <cstdio> #include <iostream> #include <string> #include <algorithm> #include <    cstring>using namespace std; int dp[1000][1000]; string str,tmp; int n; int main () {while (scanf ("%d", &n)!=eof)            {while (n--) {cin>>str;            TMP = STR;            Reverse (Tmp.begin (), Tmp.end ());                    for (int i = 0; I<str.size (), i++) {for (int j = 0; J<tmp.size (); j + +) {  if (str[i]! = Tmp[j]) {dp[i+1][j+1] = max (Dp[i][j+1],dp[i+1][j])                    ;                    } else {dp[i+1][j+1] = dp[i][j]+1;        }}} printf ("%d\n", Str.size ()-Dp[str.size ()][tmp.size ()]); }} return 0;} </span> 


Nyoj 37 Text 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.