求字符串的最长公共子串

找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的。而最长公共子序列则并不要求连续。

代码如下:

package string;

import java.util.ArrayList;
import java.util.List;

public class Main {
    // 求最长公共子串长度
    public int getMaxLen(String s1, String s2){
        if(s1 == null || s2 == null){
            return 0;
        }
        int m = s1.length();
        int n = s2.length();
        // a[i][j]表示以s1中以i-1结尾,s2中以j-1结尾的最长公共子串长度
        int[][] a = new int[m+1][n+1];
        int maxLen = 0;
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(s1.charAt(i-1) == s2.charAt(j-1)){
                    a[i][j] = a[i-1][j-1]+1;
                }else{
                    a[i][j] = 0;
                }
                maxLen = Math.max(maxLen, a[i][j]);
            }
        }
        return maxLen;
    }

    // 求最长公共子串
    public List<String> getMaxSubString(String s1, String s2){
        List<String> res = new ArrayList<String>();
        if(s1 == null || s2 == null){
            return res;
        }
        int m = s1.length();
        int n = s2.length();
        int maxLen = 0;
        // a[i][j]表示以s1中以i-1结尾,s2中以j-1结尾的最长公共子串的长度
        int[][] a = new int[m+1][n+1];
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                a[i][j] = s1.charAt(i-1) == s2.charAt(j-1) ? a[i-1][j-1]+1 : 0;
                if(a[i][j] == maxLen){
                    res.add(s1.substring(i-a[i][j], i));
                } else if(a[i][j] > maxLen){
                    res = new ArrayList<String>();
                    res.add(s1.substring(i-a[i][j], i));
                    maxLen = a[i][j];
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Main m = new Main();
//        String s1 = "bab";
//        String s2 = "caba";
        String s1 = "123456abcd567";
        String s2 = "234dddabc45678";
        System.out.println(m.getMaxLen(s1, s2));
        System.out.println(m.getMaxSubString(s1, s2));
    }

}

返回博客列表

转 动态规划算法之:最长公共子序列 & 最长公共子串(LCS)

xrzs

  • 发布时间: 2013/03/25 01:30
  • 阅读: 29656
  • 收藏: 24
  • 点赞: 6
  • 评论: 2

1、先科普下最长公共子序列 & 最长公共子串的区别:

找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的。而最长公共子序列则并不要求连续。

时间: 2024-08-10 15:02:13

求字符串的最长公共子串的相关文章

求两个字符串的最长公共子串——Java实现

要求:求两个字符串的最长公共子串,如"abcdefg"和"adefgwgeweg"的最长公共子串为"defg"(子串必须是连续的) public class Main03{ // 求解两个字符号的最长公共子串 public static String maxSubstring(String strOne, String strTwo){ // 参数检查 if(strOne==null || strTwo == null){ return null

后缀数组(多个字符串的最长公共子串)—— POJ 3294

对应POJ 题目:点击打开链接 Life Forms Time Limit:6666MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem C: Life Forms You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial tra

两个字符串的最长公共子串

import java.util.Scanner; /* 求两个字符串的最长公共子串*/ public class stringDemo {     public static void main(String[] args){      Scanner scanner = new Scanner(System.in);      System.out.println("请输入第一个字符串:");      String str1 = scanner.nextLine();     

算法练习:求字符串的最长重复子串(Java实现)

1. 求字符串的最长重复子串 例如:aaaaaaaaabbbbcccaaassscvvv这里面的最长重复子串为aaaaaaaaa 算法思路:算法时间复杂度(O(n)) 1. 将这一个字符串先转成char数组: 2. 将这一char数组进行遍历 3. 比较char数组中第i-1个与第i个的字符是否相等,如果不相等则进行截取字符串长度,然后将其进行比较,如果其长度比现有长度大,则进行替换,否则什么也不做 算法实现:(Java实现) private static String reSubStr(Str

[URAL-1517][求两个字符串的最长公共子串]

Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speech (this story is fully described in the problem "Freedom of speech"), another freedom - the freedom of choice - came down on them. In the near fu

面试宝典_Python.常规算法.0002.输出任意两个字符串中最长公共子串?

面试题目: 1. 用PY实现求任意两个字符串最长的公共子串? 解题思路: 1. 先求出长度最小的字符串,然后遍历其索引,这样可以避免字符串索引溢出,然后判断对应索引的值是否相同,相同的话就加到目标字典,不同的话就更新目标字典索引,但不存储,最后再按照值长度逆向排序取出第一个元素即可. 具体实现: #!/usr/bin/env python # -*- coding: utf-8 -*- """ # # Authors: limanman # OsChina: http://x

POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串

Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 2967 Description You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, e

[转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描:对所有串,从字符串第0位开始比较,全部相等则继续比较第1,2...n位,直到发生不全部相等的情况,则得出最长公共前缀串. 横向扫描算法实现: //LeetCode_Longest Common Prefix //Written by zhou //2013.11.22 class Solution

两个字符串的最长公共子串求法(C++、动态规划)

#include<iostream>#include<algorithm>#include<string>#include<stdlib.h>#include<cmath>using namespace std;int main(){ string str1, str2; string shortString, longString; //cin>>str1>>str2; str1 = "dabcf";