动态规划 字符串最大公共子序列以及最大公共子串问题LCS
来源:互联网 发布:单品商城 源码 编辑:程序博客网 时间:2024/05/21 14:26
1、先科普下最长公共子序列 & 最长公共子串的区别:
找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的。而最长公共子序列则并不要求连续。
2、最长公共子串
其实这是一个序贯决策问题,可以用动态规划来求解。我们采用一个二维矩阵来记录中间的结果。这个二维矩阵怎么构造呢?直接举个例子吧:"bab"和"caba"(当然我们现在一眼就可以看出来最长公共子串是"ba"或"ab")
b a b
c 0 0 0
a 0 1 0
b 1 0 1
a 0 1 0
我们看矩阵的斜对角线最长的那个就能找出最长公共子串。
不过在二维矩阵上找最长的由1组成的斜对角线也是件麻烦费时的事,下面改进:当要在矩阵是填1时让它等于其左上角元素加1。
b a b
c 0 0 0
a 0 1 0
b 1 0 2
a 0 2 0
这样矩阵中的最大元素就是 最长公共子串的长度。
在构造这个二维矩阵的过程中由于得出矩阵的某一行后其上一行就没用了,所以实际上在程序中可以用一维数组来代替这个矩阵。
2.1 代码如下:
public
class
LCString2 {
public
static
void
getLCString(
char
[] str1,
char
[] str2) {
int
i, j;
int
len1, len2;
len1 = str1.length;
len2 = str2.length;
int
maxLen = len1 > len2 ? len1 : len2;
int
[] max =
new
int
[maxLen];
int
[] maxIndex =
new
int
[maxLen];
int
[] c =
new
int
[maxLen];
// 记录对角线上的相等值的个数
for
(i =
0
; i < len2; i++) {
for
(j = len1 -
1
; j >=
0
; j--) {
if
(str2[i] == str1[j]) {
if
((i ==
0
) || (j ==
0
))
c[j] =
1
;
else
c[j] = c[j -
1
] +
1
;
}
else
{
c[j] =
0
;
}
if
(c[j] > max[
0
]) {
// 如果是大于那暂时只有一个是最长的,而且要把后面的清0;
max[
0
] = c[j];
// 记录对角线元素的最大值,之后在遍历时用作提取子串的长度
maxIndex[
0
] = j;
// 记录对角线元素最大值的位置
for
(
int
k =
1
; k < maxLen; k++) {
max[k] =
0
;
maxIndex[k] =
0
;
}
}
else
if
(c[j] == max[
0
]) {
// 有多个是相同长度的子串
for
(
int
k =
1
; k < maxLen; k++) {
if
(max[k] ==
0
) {
max[k] = c[j];
maxIndex[k] = j;
break
;
// 在后面加一个就要退出循环了
}
}
}
}
}
for
(j =
0
; j < maxLen; j++) {
if
(max[j] >
0
) {
System.out.println(
"第"
+ (j +
1
) +
"个公共子串:"
);
for
(i = maxIndex[j] - max[j] +
1
; i <= maxIndex[j]; i++)
System.out.print(str1[i]);
System.out.println(
" "
);
}
}
}
public
static
void
main(String[] args) {
String str1 =
new
String(
"123456abcd567"
);
String str2 =
new
String(
"234dddabc45678"
);
// String str1 = new String("aab12345678cde");
// String str2 = new String("ab1234yb1234567");
getLCString(str1.toCharArray(), str2.toCharArray());
}
}
LCS的java算法---考虑可能有多个相同的最长公共子串
http://blog.csdn.net/rabbitbug/article/details/1740557
最大子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离
http://www.cnblogs.com/zhangchaoyang/articles/2012070.html
2.2 其实 awk 写起来也很容易:
echo
"123456abcd567
234dddabc45678
"|awk -vFS="
" 'NR==1{str=$0}NR==2{N=NF;for(n=0;n++<N;){s="
";for(t=n;t<=N;t++){s=s"
"$t;
if
(index(str,s)){a[n]=t-n;b[n]=s;
if
(m<=a[n])m=a[n]}
else
{t=N}}}}END{
for
(n=0;n++<N;)
if
(a[n]==m)print b[n]}'
ref:http://bbs.chinaunix.net/thread-4055834-2-1.html
2.3 perl的。。。真心没看懂。。。
#!/usr/bin/perl
use
strict;
use
warnings;
my
$str1
=
"123456abcd567"
;
my
$str2
=
"234dddabc45678"
;
my
$str
=
$str1
.
"\n"
.
$str2
;
my
(
@substr
,
@result
);
$str
=~ /(.+)(?=.*\n.*\1)(
*PRUNE
)(?{
push
@substr
,$1})(
*F
)/;
@substr
=
sort
{
length
(
$b
) <=>
length
(
$a
) }
@substr
;
@result
=
grep
{
length
==
length
$substr
[0] }
@substr
;
print
"@result\n"
;
3、最长公共子序列
import
java.util.Random;
public
class
LCS {
public
static
void
main(String[] args) {
// 随机生成字符串
// String x = GetRandomStrings(substringLength1);
// String y = GetRandomStrings(substringLength2);
String x =
"a1b2c3"
;
String y =
"1a1wbz2c123a1b2c123"
;
// 设置字符串长度
int
substringLength1 = x.length();
int
substringLength2 = y.length();
// 具体大小可自行设置
// 构造二维数组记录子问题x[i]和y[i]的LCS的长度
int
[][] opt =
new
int
[substringLength1 +
1
][substringLength2 +
1
];
// 从后向前,动态规划计算所有子问题。也可从前到后。
for
(
int
i = substringLength1 -
1
; i >=
0
; i--) {
for
(
int
j = substringLength2 -
1
; j >=
0
; j--) {
if
(x.charAt(i) == y.charAt(j))
opt[i][j] = opt[i +
1
][j +
1
] +
1
;
// 状态转移方程
else
opt[i][j] = Math.max(opt[i +
1
][j], opt[i][j +
1
]);
// 状态转移方程
}
}
System.out.println(
"substring1:"
+ x);
System.out.println(
"substring2:"
+ y);
System.out.print(
"LCS:"
);
int
i =
0
, j =
0
;
while
(i < substringLength1 && j < substringLength2) {
if
(x.charAt(i) == y.charAt(j)) {
System.out.print(x.charAt(i));
i++;
j++;
}
else
if
(opt[i +
1
][j] >= opt[i][j +
1
])
i++;
else
j++;
}
}
// 取得定长随机字符串
public
static
String GetRandomStrings(
int
length) {
StringBuffer buffer =
new
StringBuffer(
"abcdefghijklmnopqrstuvwxyz"
);
StringBuffer sb =
new
StringBuffer();
Random r =
new
Random();
int
range = buffer.length();
for
(
int
i =
0
; i < length; i++) {
sb.append(buffer.charAt(r.nextInt(range)));
}
return
sb.toString();
}
}
- 动态规划 字符串最大公共子序列以及最大公共子串问题LCS
- java动态规划 实现输出最大公共子序列的长度以及输出最大子字符串
- LCS最大公共子序列问题
- LCS最大公共子序列
- 最大公共子序列LCS
- LCS 最大公共子序列
- 最大公共子序列--动态规划
- 动态规划(最大公共子序列)
- 【动态规划】LCS算法:求两字符串最大公共子序列/删除字符使成为回文串
- 动态规划_最大公共子序列长度问题
- java 动态规划最大公共子序列问题
- 动态规划---实现输出最大公共子序列的长度以及输出最大子字符串(java语言描述)
- 动态规划解最大公共子串
- 最大公共子串--动态规划
- hdu 1159 LCS最大公共子序列
- 最大公共子序列LCS入门
- hdu 1159,最大公共子序列 LCS
- 最大公共子序列(LCS算法)
- linux 系统调用与标准库调用的区别详细解析
- linux线程同步05
- Android滑动菜单特效实现,仿人人客户端侧滑效果,史上最简单的侧滑实现
- jsp页面中,<%! %>的感叹号是干什么用的?
- PowerDesigner之PDM(物理概念模型)
- 动态规划 字符串最大公共子序列以及最大公共子串问题LCS
- UIWindow输入密码
- JQuery中animate的一些坑
- ApplePay
- Java Collections Framework(JAVA集合框架)中的常用集合及其特点、适用场景、实现原理简介
- javascript:算法笔记
- 给 App 提速:Android 性能优化总结
- Directshow获取系统默认设备
- jsp <%! %> 与 <% %> 区别