LCS application-return words

Source: Internet
Author: User

I just introduced the weak LCS (longest common substring), and now I want to introduce an application of its powerful functions.

First, let's take a look at the question:

 

 

[Title Description] (vijos1327) A return word is a symmetric string-that is, a return word reads from left to right and reads from right to left to get the same result. Any given string can be converted into a return word by inserting several characters. Your task is to write a program to find the minimum number of characters to be inserted to convert a given string into a return word. For example, the string "ab3bd" can be converted into a return Word ("dab3bad" or "adb3133") after two characters are inserted "). However, inserting less than two characters cannot make it a return word. [Input format] the first line contains an integer N, indicating the length of the given string. The second line is a string of N length, A string consists of uppercase and lowercase letters and numbers. Output Format: an integer that represents the minimum number of characters to be inserted. [Sample input] 5ab3bd [sample output] 2

 

 

It seems that Ms has nothing to do with our LCS. It is very simple. LCS has two strings, and there is only one *. * |. However, after reading the questions again, we will find that they still have some similarities-they are all looking for the same part. In this way, as long as the original string S is reversed to S1, we have two strings ^. ^. Then we can find the maximum number of letters that can be paired with the two strings (if the string length is an odd number, the number in the middle is also calculated ). Then, the total length of the string minus the number of matching letters is the required number of inserted characters.

If you are confused, the following illustration may tell you the answer.

Example:

 

S (original string) a B 3 B D

S1 (reverse string) d B 3 B

Lcs B 3 B

 

Therefore, three characters are matched and can be input without adding characters. Only A and D are required ".

Some people asked me a weak question: Why is a character in the middle of an odd long string included in the "paired" category? When a new character is inserted, its position may move. Note that the numbers of unpaired characters on both sides must be equal (you can try it in several cases ). Therefore, the characters in the middle will not change.

 

Reference code:

 

 1 program palindrome;
2 VaR
3 A, B: array [1 .. 1000] of char;
4 f: array [0 .. 1000, 0 .. 1000] of longint;
5 N, I, j: integer;
6 function max (X, Y: longint): longint;
7 begin
8 if x> Y then exit (X)
9 else exit (y );
10 end;
11 begin
12 readln (N );
13 For I: = 1 to n do
14 begin
15 read (A [I]);
16 B [n-I + 1]: = A [I]; // create S2
17 end;
18 For I: = 1 to n do // LCs
19 For J: = 1 to n do
20 if a [I] = B [J] Then
21 f [I, j]: = f [I-1, J-1] + 1
22 else f [I, j]: = max (F [I-1, J], F [I, J-1]);
23 writeln (n-f [N, N]); // output the number of matched characters
24 end.
 

 

 

 

(Saltless original, reprinted please indicate the source)

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.