公共字串计算
公共字串计算
http://www.nowcoder.com/questionTerminal/98dc82c094e043ccb7e0570e5342dd1b
import java.util.*;
public class Main
{
public int getCommonStrLength(String s, String t) {
int result = 0;
for (int j = 0; j < t.length(); j++) {
for (int i = 0, val = 0; i < s.length() && i + j < t.length(); i++) {
if (t.charAt(i + j) == s.charAt(i)) {
val++;
result = Math.max(result, val);
}
else {
val = 0;
}
}
}
for (int i = 0; i < s.length(); i++) {
for (int j = 0, val = 0; j < t.length() && i + j < s.length(); j++) {
if (s.charAt(i + j) == t.charAt(j)) {
val++;
result = Math.max(result, val);
}
else {
val = 0;
}
}
}
return result;
}
public Main() {
}
public static void main(String[] args)
{
Main solution = new Main();
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String s = in.nextLine();
String t = in.nextLine();
int result = solution.getCommonStrLength(s, t);
System.out.println(result);
}
}
}

科大讯飞公司氛围 455人发布