要求:求两个字符串的最长公共子串,如“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. STM8 亮灯程序

    开发环境:ST Visual Develop+STM32 ST-LINK Utility+开发板 原理:定时向指定针脚输出高电平信号 /* MAIN.C file * * Copyright (c) ...

  2. Struts2通配符

    action: struts: or: 请求路径:

  3. JDK(五)JDK1.8源码分析【集合】HashMap

    本文转载自无始无终,原文连接 HashMap 在 JDK 1.8 后新增的红黑树结构 传统 HashMap 的缺点 JDK 1.8 以前 HashMap 的实现是 数组+链表,即使哈希函数取得再好,也 ...

  4. openstack neutron 简单理解

    分析1)位于最上层的Neutron Server充当一个门派中的“掌门人”角色(RESTful Server),负责接受来自外部门派(项目)的API请求,比如Nova API创建网络的请求.2)位于中 ...

  5. python动态调用函数

    callmap = {ts.get_stock_basics: 'D:/dxw/code/all.csv', ts.get_sz50s: 'D:/dxw/code/50.csv', ts.get_hs ...

  6. myeclipse2014黑色主题风格设置

    http://jingyan.baidu.com/article/915fc41494db8451384b2043.html?st=2&os=0&bd_page_type=1& ...

  7. java SSM 框架 多数据源 代码生成器 websocket即时通讯 shiro redis 后台框架源码

    A 调用摄像头拍照,自定义裁剪编辑头像 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统]B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技 ...

  8. ES6读书笔记(一)

    前言 前段时间整理了ES5的读书笔记:<你可能遗漏的JS知识点(一)>.<你可能遗漏的JS知识点(二)>,现在轮到ES6了,总共分为四篇,以便于知识点的梳理和查看,本篇内容包括 ...

  9. @property & @synthesize & @dynamic 及相关属性作用探究

    @property : iOS6 引入关键词. @property name; 指示编译器自动生成 name 的 setter 和 getter 方法 : - (NSString *)name; - ...

  10. C++练习 | 运算符重载练习(字符串相关)

    #include <iostream> #include <cmath> #include <cstring> #include <string> #i ...