题解 | #查找两个字符串a,b中的最长公共子串#

查找两个字符串a,b中的最长公共子串

http://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str1 = sc.nextLine();
String str2 = sc.nextLine();
if(str1.length()>str2.length()){
String temp=str1;
str1=str2;
str2=temp;

        }
    String max="";
    for (int i = 0; i < str1.length(); i++) {
        for (int j = i; j < str1.length(); j++) {
            String temp = str1.substring(i,j+1);
            if(str2.contains(temp)){
                if(temp.length()>max.length()){
                    max=temp;
                }
            }else {
                break;
            }
        }
    }
    System.out.println(max);
}

}}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务