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

要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的)

public class Main03{
	// 求解两个字符号的最长公共子串
	public static String maxSubstring(String strOne, String strTwo){
		// 参数检查
		if(strOne==null || strTwo == null){
			return null;
		}
		if(strOne.equals("") || strTwo.equals("")){
			return null;
		}
		// 二者中较长的字符串
		String max = "";
		// 二者中较短的字符串
		String min = "";
		if(strOne.length() < strTwo.length()){
			max = strTwo;
			min = strOne;
		} else{
			max = strTwo;
			min = strOne;
		}
		String current = "";
		// 遍历较短的字符串,并依次减少短字符串的字符数量,判断长字符是否包含该子串
		for(int i=0; i<min.length(); i++){
			for(int begin=0, end=min.length()-i; end<=min.length(); begin++, end++){
				current = min.substring(begin, end);
				if(max.contains(current)){
					return current;
				}
			}
		}
		return null;
	}

	public static void main(String[] args) {
		String strOne = "abcdefg";
		String strTwo = "adefgwgeweg";
		String result = Main03.maxSubstring(strOne, strTwo);
		System.out.println(result);
	}
}

  

总觉得这题,输出结果和题意不相符合,结果2,是不是把B序列翻转,求出两者最长公共子串

原文地址:https://www.cnblogs.com/hgc-bky/p/9545355.html

时间: 2024-08-09 21:48:36

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

[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

【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

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

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();     

SPOJ 1811 Longest Common Substring(求两个串的最长公共子串)

http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 分析: 以A建立SAM 让B在SAM上匹配可以类比于kmp思想,我们知道在Parent树上,fa是当前节点的子集,也就是说满足最大前缀,利用这个就可以做题了 #include <bits/stdc++.h> #define LL long long #define P pair<int, int> #define lowbit(x) (x & -x) #define mem(a

动态规划之----求两个字符串的最长公共子序列

package DongtaiGuihua; /** * Created by hunk on 2015/9/13. */public class LongestCommonSubstring { public static void main(String[] args){ String str1="BDCABA"; String str2="ABCBDAB"; System.out.println(findMaxCommonSubstring(str1,str2

面试宝典_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";

c++求两个字符串的最长公共子序列and子串

https://blog.csdn.net/ggdhs/article/details/90713154 #include <iostream> using namespace std; int main() { char str1[] = "helloworld"; char str2[] = "loop"; int arr[11][5] = {0}; for(uint32_t i=1; i<11;i++) { cout << str