First understand: The difference between the longest common substring and the longest common subsequence. Longest common substring (longest Common substirng): Longest consecutive common subsequence (longest Common Subsequence,lcs): no continuous
is really ashamed, online to do a question half a day no progress:
Given a string s, you can remove some characters from it so that the remaining string is a palindrome string. How to delete to make a palindrome the longest?
The number of characters that the output needs to be deleted.
The first is that they can understand the idea of dynamic programming in general, otherwise the complexity of the algorithm is necessarily too large. However, it is difficult to find its state and state transfer equation for palindrome strings, in other words: A sequence is not a palindrome string and the sequence of the subsequence does not have a significant relationship, a sequence is a palindrome string plus a letter may not be. So its state is very difficult to find.
Recently, there is not much time, can not be in a problem of behind closed doors (later to see actually also, this problem is the computer Department of class examples, simple own instead focusing more difficult to think of).
The idea is: to reverse the string, to find the new string and the maximum subsequence of the original string. ABCDA--->adcba maximum subsequence is ACA, and subtracting is the minimum number of characters to delete. So the question becomes the oldest sequence that asks for two strings.
So how do you find the oldest sequence of two strings? Since the dynamic programming is used, the most important thing is to determine the state and state transition equations.
Status: When the subscript of the STR1 is n (regardless of the latter), the eldest of the sequence length l at the time of the subscript is m,str2.
Transfer equation: 1, if STR1 (m) ==str2 (n), then L (m,n) =l (m-1,n-1) +1. 2, if STR1 (m)!=str2 (n), then L (m,n) =max (L (m-1,n), L (m,n-1))
To explain, if M and n are equal, then the oldest sequence of this time is undoubtedly the first L (m-1,n-1) plus 1, because the characters in this place of the two strings can be added to the oldest sequence. If not equal, either discard the new character m from str1, or discard the n character of Str2 (the oldest sequence is, of course, a uniquely determined character at each location), after discarding, from
L (M-1,n), L (m,n-1) pick a good (can be longer) for the oldest sequence of the current state.
Code:
1 ImportJava.util.Scanner;2 3 Public classMain {4 Public Static voidMain (string[] args) {5Solution s =Newsolution ();6Scanner sc =NewScanner (system.in);7 while(Sc.hasnextline ()) {8 System.out.println (S.getresult (Sc.nextline ()));9 }Ten sc.close (); One } A } - - classSolution { the Public intGetResult (String s) { -StringBuilder S1 =NewStringBuilder (s); -StringBuilder s2 =NewStringBuilder (s). reverse (); - returnS.length ()-LCS (S1, S2); + } - Public intLCS (StringBuilder s1, StringBuilder S2) { + intm =s1.length (); A intn =s2.length (); at int[] Mutrix =New int[M + 1] [n + 1]; - - for(inti = 1; I <= m; i++) { - for(intj = 1; J <= N; J + +) { - if(S1.charat (i-1) = = S2.charat (j-1)) -MUTRIX[I][J] = mutrix[i-1][j-1] + 1; in Else -MUTRIX[I][J] = Math.max (Mutrix[i-1][j], mutrix[i][j-1]); to } + } - returnMutrix[m][n]; the } *}
Delete some characters to make it a palindrome problem--the longest common subsequence (LCS) problem