题目描述
题目标题:
计算两个字符串的最大公共字串的长度,字符不区分大小写
详细描述:
接口说明
原型:
int getCommonStrLength(char * pFirstStr, char * pSecondStr);
输入参数:
char * pFirstStr //第一个字符串
char * pSecondStr//第二个字符串
输入描述:
输入两个字符串
输出描述:
输出一个整数
示例1
输入
asdfas werasdfaswer
输出
6
我自己用了for循环做的,最简单的,没有用动态规划,不知道动态规划的逻辑,如果有大大神有提示就好了
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String a = sc.next();
String b = sc.next();
int strLength = getCommonStrLength(a, b);
System.out.println(strLength);
}
}
public static int getCommonStrLength(String a, String b) {
int commonlenth = 0;
if(a.length()==0||b.length()==0)return 0;
for (int i = 0; i < a.length(); i++) {
for (int j = i; j < a.length(); j++) {
for (int k = j; k < a.length(); k++) {
String sub1 = a.substring(j, k);
String sub2 = a.substring(k, a.length());
if(b.contains(sub1)&&commonlenth<sub1.length())commonlenth = sub1.length();
if(b.contains(sub2)&&commonlenth<sub2.length())commonlenth = sub2.length();
}
}
}
return commonlenth;
}
}