字符串的最大子串么
代码比较简单。。直接上吧。大学时期的程序
import java.util.LinkedList; import java.util.List; public class MaxString { public static void main(String[] args) { List<Character> str1 = new LinkedList<Character>(); List<Character> str2 = new LinkedList<Character>(); str1.add(new Character('a')); str1.add(new Character('b')); str1.add(new Character('a')); str1.add(new Character('c')); str1.add(new Character('h')); str2.add(new Character('i')); str2.add(new Character('a')); str2.add(new Character('b')); str2.add(new Character('a')); str2.add(new Character('c')); str2.add(new Character('h')); str2.add(new Character('q')); String sub = maxSub(str1,str2); System.out.println(sub); } private static String maxSub(List<Character> str1, List<Character> str2) { if(str1.size()<=0||str2.size()<=0){ return null; } int k=0; while(str1.get(k).equals(str2.get(k))){ k++; if (k>=str1.size()||k>=str2.size()){ break; } } if (k == 0) { //the first one is not equal.... str2.remove(k); //just go to the next iteration return maxSub(str1,str2); } else { StringBuilder sb = new StringBuilder(); for(int i=0;i<k;i++) { sb.append(str1.get(i)); } for(int i=0;i<k;i++) { str1.remove(0);str2.remove(0); } String subString = maxSub(str1, str2); if (subString != null&&(subString.length()>sb.length())) { return subString; } else { return sb.toString(); } } } }