最长公共子串问题
2016-06-28 20:53
357 查看
参考 http://blog.csdn.net/hackbuteer1/article/details/6686931
public class Test4 {
static int longest_com_substr(String str1, String str2) {
int postion = 0,j = 0,len1, len2, len, s1_start = 0, s2_start = 0, curmax = 0, max = 0;
len1 = str1.length();
len2 = str2.length();
len = len1 + len2;
StringBuilder sb = new StringBuilder();
char[] str1Array = new char[len1];
str1Array = str1.toCharArray();
char[] str2Array = new char[len2];
str2Array = str2.toCharArray();
for (int i = 1; i < len; i++) {
if (i <= len1)
s1_start = len1 - i;
else
s2_start = i - len1;
curmax = 0;
for ( j = 0; (s1_start + j < len1) && (s2_start + j < len2); j++) {
if (str1Array[s1_start + j] == str2Array[s2_start + j]) {
curmax++;
} else {
if (max < curmax) {
postion = s1_start+j-1;
max = curmax;
}
curmax = 0;
}
}
if (max < curmax) {
postion = s1_start+j-1;
max = curmax;
}
}
for(int k=0;k<max;k++){
sb.append(str1Array[postion-max+1+k]);
}
System.out.println(sb);
return max;
}
public static void main(String[] args) {
int max;
max = longest_com_substr("bbbbbba", "qqqqqq");
System.out.println(max);
}
}
public class Test4 {
static int longest_com_substr(String str1, String str2) {
int postion = 0,j = 0,len1, len2, len, s1_start = 0, s2_start = 0, curmax = 0, max = 0;
len1 = str1.length();
len2 = str2.length();
len = len1 + len2;
StringBuilder sb = new StringBuilder();
char[] str1Array = new char[len1];
str1Array = str1.toCharArray();
char[] str2Array = new char[len2];
str2Array = str2.toCharArray();
for (int i = 1; i < len; i++) {
if (i <= len1)
s1_start = len1 - i;
else
s2_start = i - len1;
curmax = 0;
for ( j = 0; (s1_start + j < len1) && (s2_start + j < len2); j++) {
if (str1Array[s1_start + j] == str2Array[s2_start + j]) {
curmax++;
} else {
if (max < curmax) {
postion = s1_start+j-1;
max = curmax;
}
curmax = 0;
}
}
if (max < curmax) {
postion = s1_start+j-1;
max = curmax;
}
}
for(int k=0;k<max;k++){
sb.append(str1Array[postion-max+1+k]);
}
System.out.println(sb);
return max;
}
public static void main(String[] args) {
int max;
max = longest_com_substr("bbbbbba", "qqqqqq");
System.out.println(max);
}
}
相关文章推荐
- HBuilder
- 肤色检测一例-使用rgb颜色模型
- Linux-ulimit命令
- 访问服务器地址中的某个文件链接,下载文件到客户端
- python强制类型转换(dpkt)
- unsigned long的一点应用
- session入库
- 为 Tomcat 安装 apr
- 吉布斯采样——原理及matlab实现
- Erasure Coding(纠删码)深入分析
- 算法之美--交换排序--冒泡排序
- Codeforces 545C Woodcutters
- SpiralOrderTraverse,螺旋遍历二叉树,利用两个栈
- BBC-unit7 session1
- 字符串分割,以及字符串匹配
- Visual Studio属性配置中使用宏
- out.println()和<%=%>那种输出方式更好?
- okHttp post&get请求
- Javassist library is missing in classpath! Please add missed dependency!
- android 实现截取 内容超过屏幕大小的长图