求两个字符串的最长公共子串——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序列翻转,求出两者最长公共子串
求两个字符串的最长公共子串——Java实现的更多相关文章
- [URAL-1517][求两个字符串的最长公共子串]
Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...
- 求两个字符串的最长公共子串(LCS)
http://tianyunpu2008.blog.163.com/blog/static/6559379920089162236915/
- 【java】求两个字符串的最长公共子串
这个是华为OJ上的一道题目.首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下: (1)一定不可以有包名: (2)主类名只能为Main: (3)不 ...
- SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)
http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- poj 2774 后缀数组 两个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 31904 Accepted: 12 ...
- 【Java例题】5.5 两个字符串中最长公共子串
5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...
- hihocoder-1415 后缀数组三·重复旋律3 两个字符串的最长公共子串
把s1,s2拼接,求Height.相邻的Height判断左右串起点是否在两个串中,另外对Height和s1.length()-SA[i-1]取min. #include <iostream> ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
随机推荐
- druid相关资料
官方资料直达地址: Druid 首页 https://github.com/alibaba/druid/wiki/%E9%A6%96%E9%A1%B5 Druid 常见问题 https://githu ...
- Tomcat中Pipeline
Pipeline 节选部分源码.源码版本 Tomcat8.5 处理模式 Pipeline--Valve是一种责任链模式,它和普通责任链模式有两点区别: 每个Pipeline都是有特定的Valve,而且 ...
- CSU - 1581 Clock Pictures (KMP的变形题,难想到)
题目链接: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1581 题目意思:告诉你现在有两个钟,现在两个钟上面都有n个指针,告诉你指针的位置, ...
- Vue学习—组件的学习
1.什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能 ...
- 非空校验在oracle和mysql中的用法
oracle判断是否为null nvl(参数1,参数2) :如果参数1为null则返回参数2,否则返回参数1 mysql判断是否为null ifnull(参数1,参数2) :如果参数1为null则返回 ...
- MySQL->索引的维护[20180504]
学习MySQL数据库中表的索引维护(新增和删除) 索引的好处: 提高查询的效率 可限定特定的资料(如唯一) 索引的不足: ...
- CSS3 过渡、变形和动画
一.我们来给按钮增加一个悬停效果:#content a:hover {border: 1px solid #000000;color: #000000;text-shadow: 0px 1px whi ...
- appache 端口 更改
外网访问---->hosts文件映射服务名(127.0.0.1 xiaotian.cn)-->appache中httpd文件监听相关端口号(*:8080)--->appache中的v ...
- pomelo vscode 调试配置
步骤 config/server.js 配置 .vscode/launch.json 配置 详细 1. 在server的配置中添加 args 参数,此参数为node开启此服务器时命令参数 " ...
- vue 项目中px转rem转换问题(postcss-px2rem)
1.安装postcss-px2rem npm install postcss-px2rem --save npm install postcss-px2rem --save 2.配置px2rem 在配 ...