posts - 70,comments - 408,trackbacks - 0
public String getMaxMatch(String a,String b) {  
        StringBuffer tmp = new StringBuffer();  
        String maxString = "";  
        int max = 0;  
        int len = 0;  
        char[] aArray = a.toCharArray();  
        char[] bArray = b.toCharArray();  
        int posA = 0;  
        int posB = 0;  
        while(posA<aArray.length-max) {  
            posB = 0;  
            while(posB<(bArray.length-max)) {                                  
                 if(aArray[posA]==bArray[posB]) {  
                      len = 1;  
                      tmp = new StringBuffer();  
                      tmp.append(aArray[posA]);                                          
                      while((posA+len<aArray.length)&&(posB+len<bArray.length)&&(aArray[posA+len]==bArray[posB+len])) {  
                           tmp.append(aArray[posA+len]);  
                           len++;  
                      }  
                      if(len>max) {  
                            max = len;  
                            maxString = tmp.toString();  
                      }  
                 }  
                      posB++;  
            }  
                      posA++;  
         }          
            return maxString;                      
    }
posted on 2006-06-30 15:06 我心依旧 阅读(1919) 评论(0)  编辑  收藏

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


网站导航: