题解 | #公共子串计算#

公共子串计算

http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

无脑的子串穷举对比

import java.math.*;
import java.lang.*;

public class Main{
    public static void main(String[]args){
        Scanner input = new Scanner(System.in);
        //StringBuffer str1 = input.nextLine();
        //StringBuffer str2 = input.nextLine();
        String str1 = input.nextLine();
        String str2 = input.nextLine();
        int lesline = 0;
        int maxle = 0;
        int linelength = 0;
        if(str1.length() > str2.length()){
            lesline = str2.length();
        }else{
            lesline = str1.length();
        }
        for(int i = 0; i < lesline; i++){ //短,指定字符首所在位置
            //int flag1 = 1;
            String a;
            for(int ii = i+1; ii <= lesline; ii++){ //指定字符尾所在位置
                //取短的
                if(str1.length() > str2.length()){
                     a = str2.substring(i,ii);
                }else{
                     a = str1.substring(i,ii);
                }
                for(int j = 0; j < Math.max(str1.length(),str2.length()); j++){ //长,指定字符首
                    for(int jj = j+1; jj <= Math.max(str1.length(),str2.length()); jj++){ //指定字符尾所在位置
                        //取短的
                        String b;
                        if(str1.length() > str2.length()){
                             b = str1.substring(j,jj);
                             if(a.equals(b) && (a.length() > linelength)){
                                 linelength = a.length();
                             }
                        }else{
                             b = str2.substring(j,jj);
                            if(a.equals(b) && (a.length() > linelength)){
                                 linelength = a.length();
                             }
                        }
                    }
                }
                
            }
            
            
        }
        System.out.println(linelength);
    }
}
全部评论

相关推荐

A_SOUL_Off...:疑似加班加出幻觉了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务