java实现字符串匹配问题之求两个字符串的最大公共子串
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/38924981
近期在项目工作中有一个关于文本对照的需求,经过这段时间的学习,总结了这篇博客内容:求两个字符串的最大公共子串。
算法思想:基于图计算两字符串的公共子串。详细算法思想參照下图:
输入字符串S1:achmacmh 输入字符串S2:macham
1)第a步,是将字符串s1,s2分别按字节拆分,构成一个二维数组;
2)二维数组中的值如b所看到的,比方第一行第一列的值表示字符串s2和s1的第一个字节是否相等,若相等就是1,否则就是0,终于产生b所看到的的二维数组;
3)分别求二维数组中斜线上的公共因子(斜线为元素a右下角值,即a[i][j]的下一个元素是a[i+1][j+1];公共因子为1所在的位置构成的字符串);
4)对全部公共因子排序,返回最大的公共因子的值。
详细的实现代码例如以下所看到的:
package cn.lulei.compare; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class StringCompare {
private int a;
private int b; public String getMaxLengthCommonString(String s1, String s2) {
if (s1 == null || s2 == null) {
return null;
}
a = s1.length();//s1长度做行
b = s2.length();//s2长度做列
if(a== 0 || b == 0) {
return "";
}
//设置匹配矩阵
boolean [][] array = new boolean[a][b];
for (int i = 0; i < a; i++) {
char c1 = s1.charAt(i);
for (int j = 0; j < b; j++) {
char c2 = s2.charAt(j);
if (c1 == c2) {
array[i][j] = true;
} else {
array[i][j] = false;
}
}
}
//求全部公因子字符串,保存信息为相对第二个字符串的起始位置和长度
List<ChildString> childStrings = new ArrayList<ChildString>();
for (int i = 0; i < a; i++) {
getMaxSort(i, 0, array, childStrings);
}
for (int i = 1; i < b; i++) {
getMaxSort(0, i, array, childStrings);
}
//排序
sort(childStrings);
if (childStrings.size() < 1) {
return "";
}
//返回最大公因子字符串
int max = childStrings.get(0).maxLength;
StringBuffer sb = new StringBuffer();
for (ChildString s: childStrings) {
if (max != s.maxLength) {
break;
}
sb.append(s2.substring(s.maxStart, s.maxStart + s.maxLength));
sb.append("\n");
}
return sb.toString();
} //排序,倒叙
private void sort(List<ChildString> list) {
Collections.sort(list, new Comparator<ChildString>(){
public int compare(ChildString o1, ChildString o2) {
return o2.maxLength - o1.maxLength;
}
});
} //求一条斜线上的公因子字符串
private void getMaxSort(int i, int j, boolean [][] array, List<ChildString> sortBean) {
int length = 0;
int start = j;
for (; i < a && j < b; i++,j++) {
if (array[i][j]) {
length++;
} else {
sortBean.add(new ChildString(length, start));
length = 0;
start = j + 1;
}
if (i == a-1 || j == b-1) {
sortBean.add(new ChildString(length, start));
}
}
} //公因子类
class ChildString {
int maxLength;
int maxStart; ChildString(int maxLength, int maxStart){
this.maxLength = maxLength;
this.maxStart = maxStart;
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new StringCompare().getMaxLengthCommonString("achmacmh", "macham"));
}
}
程序终于运行结果是:
对于两个文件的比对个人觉得能够參照这样的算法思想(自己如今并为实现),在日后的博客中将会写到。
上述实现过程中,用数组保存了全部的公共子串信息,然后排序取最大的子串,这样的做法假设仅仅是求最大子串的话,算法就不是非常合理,因此做了例如以下改动,List仅仅保存当前计算中最大的子串,详细实现例如以下:
/**
*@Description: 字符串比較
*/
package com.lulei.test; import java.util.ArrayList;
import java.util.List; public class StringCompare {
private int a;
private int b;
private int maxLength = -1; public String getMaxLengthCommonString(String s1, String s2) {
if (s1 == null || s2 == null) {
return null;
}
a = s1.length();//s1长度做行
b = s2.length();//s2长度做列
if(a== 0 || b == 0) {
return "";
}
//设置匹配矩阵
boolean [][] array = new boolean[a][b];
for (int i = 0; i < a; i++) {
char c1 = s1.charAt(i);
for (int j = 0; j < b; j++) {
char c2 = s2.charAt(j);
if (c1 == c2) {
array[i][j] = true;
} else {
array[i][j] = false;
}
}
}
//求全部公因子字符串,保存信息为相对第二个字符串的起始位置和长度
List<ChildString> childStrings = new ArrayList<ChildString>();
for (int i = 0; i < a; i++) {
getMaxSort(i, 0, array, childStrings);
}
for (int i = 1; i < b; i++) {
getMaxSort(0, i, array, childStrings);
}
StringBuffer sb = new StringBuffer();
for (ChildString s: childStrings) {
sb.append(s2.substring(s.maxStart, s.maxStart + s.maxLength));
sb.append("\n");
}
return sb.toString();
} //求一条斜线上的公因子字符串
private void getMaxSort(int i, int j, boolean [][] array, List<ChildString> sortBean) {
int length = 0;
int start = j;
for (; i < a && j < b; i++,j++) {
if (array[i][j]) {
length++;
} else {
//直接add,保存全部子串,以下的推断,仅仅保存当前最大的子串
//sortBean.add(new ChildString(length, start));
if (length == maxLength) {
sortBean.add(new ChildString(length, start));
} else if (length > maxLength) {
sortBean.clear();
maxLength = length;
sortBean.add(new ChildString(length, start));
}
length = 0;
start = j + 1;
}
if (i == a-1 || j == b-1) {
//直接add,保存全部子串,以下的推断,仅仅保存当前最大的子串
//sortBean.add(new ChildString(length, start));
if (length == maxLength) {
sortBean.add(new ChildString(length, start));
} else if (length > maxLength) {
sortBean.clear();
maxLength = length;
sortBean.add(new ChildString(length, start));
}
}
}
} //公因子类
class ChildString {
int maxLength;
int maxStart; ChildString(int maxLength, int maxStart){
this.maxLength = maxLength;
this.maxStart = maxStart;
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new StringCompare().getMaxLengthCommonString("abcdef", "defabc"));
}
}
java实现字符串匹配问题之求两个字符串的最大公共子串的更多相关文章
- Python基础-求两个字符串最长公共前轴
最长公共前缀,输入两个字符串,如果存在公共前缀,求出最长的前缀,如果没有输出no.如“distance”和“discuss”的最长公共前缀是“dis”. s1 = input('请输入第1个字符串-- ...
- 求两个字符串的最长公共子串——Java实现
要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...
- 在 Java 中不使用多余变量交换两个字符串
在 Java 中不使用多余变量交换两个字符串 public class Test { public static void main(String[] args) { String a = " ...
- 【Java】获取两个字符串中最大相同子串
题目 获取两个字符串中最大相同子串 前提 两个字符串中只有一个最大相同子串 解决方案 public class StringDemo { public static void main(String[ ...
- POJ - 3415 Common Substrings(后缀数组求长度不小于 k 的公共子串的个数+单调栈优化)
Description A substring of a string T is defined as: T( i, k)= TiTi+1... Ti+k-1, 1≤ i≤ i+k-1≤| T|. G ...
- pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。
http://poj.grids.cn/practice/2744 描述现在有一些由英文字符组成的大小写敏感的字符串,你的任务是找到一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是 ...
- 核心API的使用(获取两个字符串的最大相同子串)
/** * 获取两个字符串的最大相同子串. 例:abegad acegab */public class TheSameString { public static void main(String[ ...
- EditDistance,求两个字符串最小编辑距离,动态规划
问题描述: 题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to ...
- SQLServer中求两个字符串的交集(字符串以符号分隔)
两个字符串,以特定符号分隔(例如‘,’号),求交集 第一种情况: declare @m varchar(100),@n varchar(100)select @m=',2,3,5,7,8,9,10,' ...
随机推荐
- Servlet基础知识(一)——Servlet的本质
什么是Servlet: Servlet是运行在web服务器端(web容器,如tomcat)的程序,它与Applet相对,Applet是运行在客户端的程序. Servlet的主要作用是处理客户端的请求, ...
- HTML中小问题
1.a标签不添加href这个属性时,不会出现鼠标变成手型~
- (Problem 92)Square digit chains
A number chain is created by continuously adding the square of the digits in a number to form a new ...
- Unix/Linux笔记全集
1:Unix/Linux操作系统概述 要求:理解应用软件和操作系统的区别,掌握系统的Kernel(核心)和shell(外壳)之间的关系以及各自的作用 Solaris:Solaris 是Sun Micr ...
- HDU 5274 Dylans loves tree(树链剖分)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...
- 51nod 1237 最大公约数之和 V3(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...
- BZOJ 2253: [2010 Beijing wc]纸箱堆叠
题目 2253: [2010 Beijing wc]纸箱堆叠 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 239 Solved: 94 Descr ...
- Uber司机手机终端问答篇
手机客户端 Q:自带安卓手机可以使用吗? A:安卓终端已经推出,请在微信页面点左下菜单选取“下载司机端APP”查看! Q:对自带苹果手机的要求? A:4S型号及以上且未越狱:使用3G或4G网络 Q:客 ...
- MSP430与ATK-NEO-6M GPS模块
近短时间在网上买了一个GPS模块,正好正在学习MSP430单片机,于是决心将GPS模块与MSP430结合起来,同时将代码贴出来,发现网上搜到好多资料都要注册才能下载,有些还要钱.自己动脑,才能自娱自乐 ...
- linux 虚拟文件系统----------Virtual File System VFSkky
在了解虚拟文件系统之前,必须先知道什么是 Kernal Space 与 User Space. Kernal Space 与User Space 的差别,在于内存使用上安全机制的差异. kerna ...