要求:求两个字符串的最长公共子串,如“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序列翻转,求出两者最长公共子串

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

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

    Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...

  2. 求两个字符串的最长公共子串(LCS)

    http://tianyunpu2008.blog.163.com/blog/static/6559379920089162236915/

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

    这个是华为OJ上的一道题目.首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下: (1)一定不可以有包名: (2)主类名只能为Main: (3)不 ...

  4. SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)

    http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...

  5. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  6. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

  7. 【Java例题】5.5 两个字符串中最长公共子串

    5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...

  8. hihocoder-1415 后缀数组三·重复旋律3 两个字符串的最长公共子串

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

  9. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

随机推荐

  1. Dubbo实践(十五)消费者引用服务

    Refer取得invoker的过程 <!-- 指定了哪种的注册中心,是基于zookeeper协议的,指定了注册中心的地址以及端口号 --> <dubbo:registry proto ...

  2. Eclipse配置多个Tomcat服务器

    我们在开发大型web项目时,经常需要在eclipse中同时启动多个tomcat服务器来开启多个服务.这里讲解一下如何在eclipse中配置多个tomcat服务器. 配置步骤 1. 在tomcat官网( ...

  3. App测试需要测什么

    App,做为当下最热的手机安装软件,无论是产品本身的设计还是性能,易用性等都是非常受考验.一个app能在用户的手机上使用,并作为一个长期用户是非常不容易的.那么,App的测试中我们到底要测试什么呢? ...

  4. HDU 3038 How Many Answers Are Wrong(带权并查集,真的很难想到是个并查集!!!)

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  5. 翻译 TI SerialBLEbridge V 1.4.1

    原文地址:http://processors.wiki.ti.com/index.php/SerialBLEbridge_V_1.4.1 Sample App Overview This page d ...

  6. ionic ios 打包

    1.安装Xcode   从appstore 安装就行 2.安装node.js 3.安装cordova  由于权限问题  网络问题  可以考虑一下方式 1️⃣使用淘宝镜像    npm install ...

  7. jQuery|简单tab栏切换

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. js 变量声明易混淆的几点知识

    这是我 JavaScript 学习过程中遇到的一些容易混淆的地方,趁着有时间,做了一个整理. 变量提升 变量与函数名提升优先级 js 作用域内有变量,这个很好理解,但有一些细节需要注意. consol ...

  9. Angular4 自制分页控件

    过年后第一波,自制的分页控件,可能功能没有 PrimeNG 那么好,但是基本可以实现自定义翻页功能,包括:首页/最后一页/上一页/下一页. 用户可以自定义: 1. 当前默认页码(如未提供,默认为第一页 ...

  10. SQL逻辑查询处理的步骤序号

    (8)SELECT (9) DISTINCT <select_list> (1)FROM <left_table> (3)<join_type>JOIN<ri ...