HWOJ公共字串计算

2017-02-18  本文已影响0人  Yuu_CX
import java.util.*;
public class Main{
public static void main(String[] args) { 
        Scanner sc = new Scanner(System.in); 
        while(sc.hasNext()){
            int num = getCommonStrLength(sc.next(),sc.next());
            System.out.println(num);
        }
        sc.close(); 
    } 
    public static int getCommonStrLength(String s1, String s2){
        s1 = s1.toLowerCase();
        s2 = s2.toLowerCase();
        int len1 = s1.length();
        int len2 = s2.length();
        String minStr = null;
        String maxStr = null;
        String temp = null;
        minStr = len1 <= len2 ? s1 : s2;
        maxStr = len1 > len2 ? s1 : s2;
        for(int i=minStr.length();i>0;i--){
            for(int j=0;j<=minStr.length()-i;j++){
                temp = minStr.substring(j,j+i);
                for(int k=0;k<=maxStr.length()-i;k++){
                    if(maxStr.substring(k,k+i).equals(temp)){
                        return i;
                    }
                }
            }
        }
        return 0;
    }
}
上一篇下一篇

猜你喜欢

热点阅读