描述
查找两个字符串a,b中的最长公共子串。若有多个,输出在较短串中最先出现的那个。
注:子串的定义:将一个字符串删去前缀和后缀(也可以不删)形成的字符串。请和“子序列”的概念分开!
数据范围:字符串长度1≤length≤300
进阶:时间复杂度: O(n3) ,空间复杂度: O(n)
输入描述:
输入两个字符串
输出描述:
返回重复出现的字符
示例1
输入:
abcdefghijklmnop abcsafjklmnopqrstuvw输出:
jklmnop
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
String b = in.nextLine();
getRes(a, b);
}
}
public static void getRes(String s1, String s2) {
if (s1.length() < s2.length()) {
String tmp = s2;
s2 = s1;
s1 = tmp;
}
char[] chs1 = s1.toCharArray();
char[] chs2 = s2.toCharArray();
int size1 = chs1.length;
int size2 = chs2.length;
Obj res = Obj.build(0, 0, true);
for (int i=0; i<size1; i++) {
String tmp = "";
int count = 0;
boolean lx = false;
Map<Integer, Obj> map = new HashMap<>();
for (int j=0; j<size2; j++) {
Set<Map.Entry<Integer, Obj>> set = map.entrySet();
for (Map.Entry<Integer, Obj> item : set) {
int k = item.getKey();
Obj v = item.getValue();
if (map.get(k).isJx && i+v.count<size1 && chs1[i+v.count] == chs2[j]) {
map.get(k).add(1);
}
else {
map.get(k).setIsJx(false);
}
}
if (chs1[i] == chs2[j]) {
map.put(j, Obj.build(1, j, true));
}
}
Set<Map.Entry<Integer, Obj>> set = map.entrySet();
for (Map.Entry<Integer, Obj> item : set) {
int k = item.getKey();
Obj v = item.getValue();
if (v.isMore(res)) {
res = v;
}
}
}
System.out.println(s2.substring(res.start, res.start+res.count));
}
public static class Obj {
int count;
int start;
boolean isJx;
public static Obj build(int count, int start, boolean isJx) {
Obj obj = new Obj();
obj.count = count;
obj.start = start;
obj.isJx = isJx;
return obj;
}
public boolean isMore(Obj o) {
if (count > o.count) {
return true;
}
if (count == o.count) {
return start < o.start;
}
return false;
}
public void add(int i) {
this.count += i;
}
public void setIsJx(boolean bool) {
this.isJx = bool;
}
}
}