返回在两字符串中第一个相同的子串(大于1个字符),并将子串输出
比如:
输入
adbAFEHHFS
acwfagAFEf
输出为
AFE
输入
qfdgJKlin
qdfgJKwfo
输出为
gJK
import java.util.Scanner;
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s;
String temp = "";
String input = "";
int count = 0;
int ct = 0;
Scanner scanner = new Scanner(System.in);
String s1 = scanner.next();
String s2 = scanner.next();
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
if (s1.charAt(i) == s2.charAt(j)) {
temp += s1.charAt(i);
count++;
i++;
} else {
if (count > 0) {
i--;
}
count = 0;
temp = "";
}
if (count > ct) {
ct = count;
input = temp;
if (i >= s1.length()) {
break;
}
}
}
}
System.out.println(input);
}
}
------解决方案--------------------
暂时没有太好的办法