The longest character sequence
The problem description is set by X (i), Y (i), z (i) to represent a single character, then x={x (1) x (2) ... x (m)},y={y (1) Y (2) ... y (n)},z={z (1) z (2) ...}, we call it a sequence of characters, where M,n and k are character sequence x, respectively. The length of the y,z, the number in parentheses () is called the subscript of the character sequence.
If there is a strict increment and the length is greater than 0 of the subscript sequence {i1, I2... Ik}, so that for all j=1,2,...... K, there is x (iJ) =z (j), then we call z a sequence of characters of X. Also, if z is both a sequence of characters of the x character and y, we call Z the common sequence of characters for X and Y.
In our question today, we want to calculate the common sequence of characters for the maximum length of two given character sequences X and Y, where we only need to output the length value corresponding to the common subsequence of the maximum length.
For example, the character sequence is X=ABCD,Y=ACDE, then their maximum length is 3, and the corresponding public character sequence is ACD. Input format input a line, separated by a space of two string output format output the length of the maximum length of the common character sequence corresponding to the two character sequences sample input AABB Aabb sample output 2 data size and contract input string length of up to 100, case-sensitive.
1 ImportJava.math.BigInteger;2 Importjava.util.Arrays;3 ImportJava.util.Scanner;4 5 6 Public classMain {7 StaticString A;8 StaticString B;9 Static intMax;Ten StaticStringBuffer S; One Staticstringbuffer S1; A Public Static voidMain (string[] args) { -Scanner input =NewScanner (system.in); -A =Input.next (); theb =Input.next (); -s =NewStringBuffer (); -S1 =NewStringBuffer (); -F (0); + System.out.println (max); - + } A Public Static voidFinti) { at inth; - for(H=i;h<a.length (); h++){ - S.append (A.charat (h)); - if(F1 (S.tostring ())) { - if(max<s.length ()) { -Max =s.length (); in } - } toF (h+1); +S.deletecharat (S.length ()-1); - } the } * Public Static BooleanF1 (String temp) { $ intj = 0;Panax Notoginseng for(intI=0;i<b.length (); i++){ - if(B.charat (i) = =Temp.charat (j)) { theJ + +; + if(j==temp.length ()) { A Break; the } + } - } $ if(J==temp.length ())return true; $ Else return false; - } -}
Algorithm to improve the longest character sequence