找出重复代码
题目
题目描述
小明负责维护项目下的代码,需要查找出重复代码,用以支撑后续的代码优化,请你帮助小明找出重复的代码。重复代码查找方法:
以字符串形式给出两行代码(字符审长度1< length <100,由英文宇母、数宇和空格组成),找出两行代码中的 最长公共子串Q
输入描述
输入的参数text1,text2分别表示两行代码
输出描述
输出任一最长公共子串
补充说明
如果不存在公共子串,返回空字符串
示例
示例1
input1:
hello123world
hello123abc4
outpu1:
hello123
示例2
input2:
rghello123wohellord66665rld
vfhello123abc4hellord66665
input2:
hellord6666
思路
代码
java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
String a=cin.next();
String b=cin.next();
int n=a.length();
int m=b.length();
for(int i=100;i>=0;i--){
for(int j=0;;j++){
if(j+i>n)
break;
String tmp=a.substring(j,j+i);
if(b.contains(tmp)) {
System.out.println(tmp);
i=-1;
break;
}
}
if(i==0)
System.out.println("");
}
}
}