本帖最后由 枫儿 于 2013-10-13 21:22 编辑
获取两个字符串中最大相同子串,这段代码咋取都是S1跟S1比较呢,也和毕姥爷的代码比较了几次,没看到哪的问题- class StringDemo4
- {
- public static void main(String[] args)
- {
- String s1 = "abcwerthelloyuiodef";
- String s2 = "cvhellobnm";
- sop(cd(s1,s2));
- }
- public static String cd(String s1,String s2)
- {
- String max="", min="";
- max = (s1.length()>s2.length())?s1: s2;
- min = (max==s1)?s2: s1;
- for (int x=0; x<min.length(); x++)
- {
- for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++)
- {
- String temp=min.substring(y,z);
- sop(temp);
- if(max.contains(temp));
- return temp;
- }
- }
- return "";
- }
- public static void sop(String sop)
- {
- System.out.println(sop);
- }
- }
复制代码 |