String is probably the biggest problem description given two strings, find the two of them shared the longest string. For example one is "tabcfg" other "abckj" then the largest substring is "abc".
Dynamic programming algorithm is the decomposition of the most important issues, find recursion. Thinking that my train of thought, first of all get the two strings, how to find the longest substring of it?
1. Assume that they (string a, b) the first letter is not the same, then remove the first letter of the comparison, respectively, that use a.subString (1) and b compared with b.subString (1) and a comparison , the longest sub-string has not changed, right? the answer is yes. ok recursive appeared to end the conditions that have an empty string, the return value is a and b of the longest substring.
b. Suppose the first letter of the same that they then have been relatively down, knows the difference between the first n letters is not the same, then the former n-1 letters saved as a sub-string c, the a.subString (n) and b.subString (n) of the longest substring denoted by d, then back to c, and d a long one.
Some people may say from behind Wang Qianmian should be compared to find the same one by one a step further than before, in fact, is the same reason, the key to find the decomposition of the problem. Here are just initiate the following specific java is achieved.
import java.util.HashMap;
import java.util.Map;
/ **
* @ Author HEACK
*
* /
public class CompareStr (
/ **
* @ Param args
* /
public static void main (String [] args) (
/ / TODO Auto-generated method stub
String str1 =
"Asldkfjiowerbasdfjlkajlfj
aldjoiernzxcvasldkfjiowerbasdfjlkajlfjaldjoiernzxcvasldkfjiow
erbasdfjlkajlfjaldjoiernzxcv ";
String str2 =
"Asdkjfolasdnflxckvjlasdofiwiuerjalskdfnznxcmvlaskdfjalsdfjl
asdjfaskdfjasiuejrlasdf ";
/ / String str2 = "abc happyies dutcbirthday peter";
CompareStr cj = new CompareStr ();
System.out.println (cj.getLongestString (str1, str2));
)
private boolean isEmpty (String str) (
return str == null | | str.trim (). length () == 0;
)
private Map map = new HashMap ();
private String getLongestString (String str1, String str2) (
if (isEmpty (str1) | | isEmpty (str2)) (
return "";
)
StringBuffer key = new StringBuffer ();
key.append (str1). append ("&&"). append (str2);
if (map.containsKey (key.toString ())) (
return map.get (key.toString ());
)
StringBuffer longestStr = new StringBuffer ();
char [] str1List = str1.toCharArray ();
char [] str2List = str2.toCharArray ();
int i = 0;
for (i = 0; i
if (str1List [i] == str2List [i]) (
longestStr.append (str1List [i]);
) Else (
break;
)
)
String subStr1 = str1.substring (i);
String subStr2 = str2.substring (i);
if (i == 0) (
String retStr1 = getLongestString (subStr1.substring (1), subStr2);
String retStr2 = getLongestString (subStr1, subStr2.substring (1));
String returnStr = retStr1.length ()> = retStr2.length ()? RetStr1: retStr2;
map.put (key.toString (), returnStr);
return returnStr;
) Else (
String retStr = getLongestString (subStr1, subStr2);
String returnStr = retStr.length ()> = longestStr.toString (). Length ()? RetStr
: LongestStr.toString ();
map.put (key.toString (), returnStr);
return returnStr;
)
)
) HashMap to store the string has been calculated, using space for time. Optimized code can, of course, you can also tried his hand Oh.