posts - 73,  comments - 55,  trackbacks - 0
/*
 求两个字符串的最大公共子串
 String s1 = "abcdefghigj";
 String s2 = "xyzabcdeigj";
 则输出abcde
*/
 
public   class  Test
{
  
public  String search(String s1,String s2)
  
{
  String max 
=   "" ;
  
for ( int  i  =   0 ; i  <  s1.length(); i ++ )
  
{
    
for ( int  j  =  i + 1 ; j  <=  s1.length(); j ++ )
    
{
      String sub 
=  s1.substring(i,j);
      
if ((s2.indexOf(sub) !=   - 1 ) &&  sub.length()  >  max.length())
      
{
        max 
=  sub;
      }

    }

  }
  
  
return  max;
  }

  
  
public   static   void  main(String[] args)
  
{
    String s1 
=   " abedafghigj " ;
    String s2 
=   " xyzabfddfigj " ;
    String output 
=   new  Test().search(s1,s2);
    System.out.println(output);
  }

}
posted on 2007-03-05 15:50 保尔任 阅读(896) 评论(0)  编辑  收藏 所属分类: Arithmetic & Data Structure

只有注册用户登录后才能发表评论。


网站导航:
 

<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(4)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜