package maxCommon;
/**
* 找到最长公共子串
* @author root
*/
public class MaxCommonUnSeries { public static void main(String[] args) {
// TODO Auto-generated method stub String s1 = "AGTA";
String s2 = "AGGCT";
//String s = find(s1.toCharArray(), 0, s2.toCharArray(), 0);
String s = find(s1.toCharArray(), s2.toCharArray());
System.out.println(s);
}
/*
* 普通递归方法 O(2^n) 此处可以有两种实现方式,返回值 和 参数 首先通过返回值实现
*/
public static String find(char []s1, int s1_b, char []s2, int s2_b){
String temp1 = "";
String temp2 = "";
String temp3 = "";
String result = "";
if(s1_b>=s1.length || s2_b>= s2.length){
return "";
}
if(s1[s1_b]==s2[s2_b]){
temp1+= s1[s1_b];
//System.out.println("s1[s1_b]:"+s1[s1_b]+" "+"s1[s2_b]:"+s1[s2_b]);
String s = find(s1, s1_b+1, s2, s2_b+1);
temp1+= s;
//return temp;
}else{
temp2 = find(s1, s1_b+1, s2, s2_b);
temp3 = find(s1, s1_b, s2, s2_b+1);
}
result = temp1.length()>temp2.length()?temp1:temp2;
result = result.length()>temp3.length()?result:temp3;
//System.out.println("temp1:"+temp1+" "+"temp2:"+temp2+" "+"temp3:"+temp3+" ");
return result;
}
<span style="white-space:pre">	</span>//找到最长公共子串  通过参数实现
<span style="white-space:pre"> </span>public static String find1(String com_str, char []s1, int s1_b, char []s2, int s2_b){
<span style="white-space:pre"> </span>String temp = "";
<span style="white-space:pre"> </span>if(s1_b>=s1.length || s2_b>=s2.length){
<span style="white-space:pre"> </span>return "";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(s1[s1_b]==s2[s2_b]){
<span style="white-space:pre"> </span>temp = com_str+s1[s1_b];
<span style="white-space:pre"> </span>if(temp.length()>longest.length()){
<span style="white-space:pre"> </span>longest = temp;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>find1(temp ,s1, s1_b+1, s2, s2_b+1);
<span style="white-space:pre"> </span>}else{
<span style="white-space:pre"> </span>find1(com_str, s1, s1_b+1, s2, s2_b);
<span style="white-space:pre"> </span>find1(com_str, s1, s1_b, s2, s2_b+1);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return "";
<span style="white-space:pre"> </span>}
/*
* 动态规划 O(n^2)
*/
public static String find(char []ss1, char []ss2){
char []s1 = ("0"+new String(ss1)).toCharArray();
char []s2 = ("0"+new String(ss2)).toCharArray();
int[][] path = new int[ss1.length+1][ss2.length+1];
int[][] b = new int[ss1.length+1][ss2.length+1];
for(int i=1; i<path.length; i++){
for(int j=1; j<path[i].length; j++){
if(s1[i]==s2[j]){
path[i][j]=path[i-1][j-1]+1;
b[i][j]=0;
}else if(path[i-1][j]>path[i][j-1]){
path[i][j]=path[i-1][j];
b[i][j]=1;
}else{
path[i][j]=path[i][j-1];
b[i][j]=-1;
}
}
}
findPath(s1, b, b.length-1, b[0].length-1);
return "";
}
/*
* 动态规划-回溯法找路径
*/
public static void findPath(char []s1, int[][] b, int i, int j){
if(i==0 || j==0){
return;
}
if(b[i][j]==0){
findPath(s1, b, i-1, j-1);
System.out.print(s1[i]);
}else if(b[i][j]==1){
findPath(s1, b, i-1, j);
}else{
findPath(s1, b, i, j-1);
}
}
}

最长公共子子串 java的更多相关文章

  1. LCS 最长公共子子串

    与 最长公共子序列类似 只是最长公共子串必须连续 所以只能走斜线!!!! ''' LCS 最长公共子序列 ''' def LCS_len(x, y): m = len(x) n = len(y) dp ...

  2. 使用后缀数组寻找最长公共子字符串JavaScript版

    后缀数组很久很久以前就出现了,具体的概念读者自行搜索,小菜仅略知一二,不便讨论. 本文通过寻找两个字符串的最长公共子字符串,演示了后缀数组的经典应用. 首先需要说明,小菜实现的这个后缀数组算法,并非标 ...

  3. uva 10066 The Twin Towers (最长公共子)

    uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...

  4. 最长公共子序列/子串 LCS(模板)

    首先区分子序列和子串,序列不要求连续性(连续和不连续都可以),但子串一定是连续的 1.最长公共子序列 1.最长公共子序列问题有最优子结构,这个问题可以分解称为更小的问题 2.同时,子问题的解释可以被重 ...

  5. KMP该算法解释(最长公共子)

    一个:介绍KMP算法之前,首先解释一下BF算法 (1)BF算法(传统的匹配算法,是最简单的算法) BF算法是一种常见的模式匹配算法,BF该算法的思想是目标字符串S模式串的第一个字符P的第一个字符,以匹 ...

  6. POJ 2774 后缀数组:查找最长公共子

    思考:其实很easy.就在两个串在一起.通过一个特殊字符,中间分隔,然后找到后缀数组的最长的公共前缀.然后在两个不同的串,最长是最长的公共子串. 注意的是:用第一个字符串来推断是不是在同一个字符中,刚 ...

  7. 自然语言处理之LCS最长公共子子序列

    #!一个序列S任意删除若干个字符得到的新序列T,则T叫做S的子序列 注意,这个和最长公共字串不一样,最长公共子串要求连续. 1.算法公式: def lcs(a,b): lena = len(a) le ...

  8. 动态规划算法——最长公共子序列问题(java实现)

    已知序列X=(A,B,C,A,B,D,A)和序列Y=(B,A,D,B,A),求它们的最长公共子序列S. /* * LCSLength.java * Version 1.0.0 * Created on ...

  9. [华为]查找两个字符串a,b中的最长公共子

    链接:https://www.nowcoder.com/questionTerminal/181a1a71c7574266ad07f9739f791506来源:牛客网 查找两个字符串a,b中的最长公共 ...

随机推荐

  1. Delphi DLL文件的动态调用

    樊伟胜

  2. 抓住“新代码”的影子 —— 基于GoAhead系列网络摄像头多个漏洞分析

    PDF 版本下载:抓住“新代码”的影子 —— 基于GoAhead系列网络摄像头多个漏洞分析 Author:知道创宇404实验室 Date:2017/03/19 一.漏洞背景 GoAhead作为世界上最 ...

  3. PHP-MYSQL中文乱码问题.

    从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行. 解决这个问题其实很简单. 1.在建表的时候设置编码类型为gb2312_chinese_ci. 2.在 ...

  4. nginx代理服务

    代理----介于客户端和服务器之间 ICMP\POP\IMAP是邮件的收/发相关协议;RTMP是视频,音频等流媒体协议 http代理 正向代理 如果一个公司多台电脑中只有一台电脑能上网,那么不能上网的 ...

  5. Oracle之:Function :getcurrdate()

    getdate()函数连接请戳这里 create or replace function getcurrdate(i_date date) return date is v_date date; v_ ...

  6. Python之抓取网页元素

    import urllib.request from bs4 import BeautifulSoup url = "http://www.wal-martchina.com/walmart ...

  7. Http中的三种请求处理模式(MPM)的区别

    MPM---包括基于事件/异步,线程化和预分叉 MPM(multi-processing module)多种请求处理模式,分为三种工作模式: prefork worker event prefork- ...

  8. 题解 【NOIP2011】计算系数

    [NOIP2011]计算系数 Description 给定一个多项式 (ax+by)^k ,请求出多项式展开后 x^n * y^m 项的系数. Input 共一行,包含 5 个整数,分别为 a,b,k ...

  9. 【Winform-自定义控件】可以使用2种半透明的颜色来填充Button

    制作一个自定义按钮,使用2种半透明的颜色来填充Button 1.添加一个自定义控件类,并改变基类,继承自Button public partial class CustomControl1 : But ...

  10. Linux 目录共享

    ## 安装 nfs 和 rpc yum install -y nfs-utils rpcbind ## ubuntu 安装 nfs 和 rpc ## apt-get install nfs-kerne ...