美文网首页
HWOJ公共字串计算

HWOJ公共字串计算

作者: Yuu_CX | 来源:发表于2017-02-18 10:35 被阅读0次
    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;
        }
    }
    

    相关文章

      网友评论

          本文标题:HWOJ公共字串计算

          本文链接:https://www.haomeiwen.com/subject/rdprwttx.html