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

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();
    	System.out.println("请输入第二个字符串:");       
        String str2 = scanner.nextLine();
        System.out.println("最大公共子串为"+commonMaxSubstring(str1, str2));
    }
    public static String commonMaxSubstring(String str1,String str2){
        int len = str1.length()<str2.length()?str1.length():str2.length();
        String str3 = null;//str3保存最大公共子串
        outer:
            //i为最长公共子串的长度(从最长依次递减)
            for(int i = len;i>0;i--){
                //j为子串的脚标
                for(int j=0;j<len-i+1;j++){
                    str3=str1.substring(j,j+i);
                    if(str2.contains(str3))
                        break outer;
                }
            }
        return str3;
    }
}

本题涉及3个知识点

1、public String substring(int beginindex,int endindex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex

明白该方法就能很好的理解第20条语句

2、contains()方法返回true,当且仅当此字符串包含指定的char值序列;

3、outer外层嵌套循环,用break outer可退出外层循环

时间: 2024-08-10 00:07:01

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

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

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

[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

两个字符串的最长公共子串求法(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";

hihocoder-1415 后缀数组三&#183;重复旋律3 两个字符串的最长公共子串

把s1,s2拼接,求Height.相邻的Height判断左右串起点是否在两个串中,另外对Height和s1.length()-SA[i-1]取min. #include <iostream> #include <cstring> #include <string> #include <queue> #include <vector> #include <map> #include <set> #include <st

【java】求两个字符串的最长公共子串

这个是华为OJ上的一道题目.首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下: (1)一定不可以有包名: (2)主类名只能为Main: (3)不可以输出与结果无关的信息. 好了,按照以上规则,我们写出来的代码如下(此代码不是最优的,只是用来记录华为OJ上java代码的书写规则): import java.util.Scanner; public class Main { public static void main(String[] ar

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

public static String findLongestOfTheSame(String s1,String s2) { char[] c1=s1.toCharArray(); char[] c2=s2.toCharArray(); int l1=c1.length; int l2=c2.length; int count=0,maxLength=0,start=0,end=0; boolean hasTheSame=false; for(int i=0;i<l1;i++) { coun

求字符串的最长公共子串

找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的.而最长公共子序列则并不要求连续. 代码如下: 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

Java算法——求出两个字符串的最长公共字符串

问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. 例如:“acbbsdef”和"abbsced"的最长公共字符串是“bbs” 算法思路: 1.把两个字符串分别以行和列组成一个二维矩阵. 2.比较二维矩阵中行和列对应的每个点的字符是否相同,是设置这个点为1,否设置这个点为0. 3.通过查找值为1的最长对角线来找到最长公共字符串. 通过上面str1和str2两个字符串,分别得出以行和列组成的一个二维矩阵如下图: 从上图可以看到,str1和str2共有3个公共子串&qu