题目描述

查找两个字符串a,b中的最长公共子串。若有多个,输出在较短串中最先出现的那个。

输入描述:

输入两个字符串

输出描述:

返回重复出现的字符
示例1

输入

abcdefghijklmnop
abcsafjklmnopqrstuvw

输出

jklmnop

代码如下:

 package com.yzh.hehe;

 import java.util.Scanner;

 public class LongestSubStrBetweenAB {

     public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while (scanner.hasNext()) {
String a=scanner.nextLine();
String b=scanner.nextLine();
System.out.println(longestSubBetweenAB2(a, b));
}
scanner.close(); } private static String longestSubBetweenAB(String a, String b) {
int alength=a.length();
int blength=b.length();
//保证a串为较短串
if (alength>blength) {
String temp=a;
int tempLength=alength;
alength=blength;
blength=tempLength;
a=b;
b=temp;
}
//从较短串整串开始,长度依次递减从左向右得到较短串子串,如果较长串中包含此子串,即为目标子串
for (int i=alength; i> 0; i--) {
int count=alength-i+1;
for (int j = 0; j < count; j++) {
if (b.contains(a.substring(j, j+i))) {
return a.substring(j,j+i);
}
}
}
return null;
} // 动态规划解决最长子串问题
private static String longestSubBetweenAB2(String a,String b) {
int alength=a.length();
int blength=b.length();
//保证a串为较短串
if (alength>blength) {
String temp=a;
int tempLength=alength;
alength=blength;
blength=tempLength;
a=b;
b=temp;
}
int[][]arr=new int[alength+1][blength+1];
String[][] sArr=new String[alength+1][blength+1];
StringBuilder stringBuilder=new StringBuilder();
String resultString=null;
int result=0;
for (int i = 0; i <= alength; i++) {
for (int j = 0; j <= blength; j++) {
if (i==0||j==0) {
arr[i][j]=0;
sArr[i][j]="";
}else if (a.charAt(i-1)==b.charAt(j-1)) {
arr[i][j]=arr[i-1][j-1]+1;
// stringBuilder.append(b.charAt(j-1));
sArr[i][j]=sArr[i-1][j-1]+a.charAt(i-1);
if (arr[i][j]>result) {
result=arr[i][j];
resultString=sArr[i][j];
} }else {
arr[i][j]=0;
sArr[i][j]="";
// stringBuilder.delete(0, stringBuilder.length());
}
}
}
if (resultString.length()==0) {
return null;
}
return resultString; }
}

解题10(LongestSubStrBetweenAB)的更多相关文章

  1. linux系统无法正常启动,故障排查恢复

    linux内核启动修复 首先看linux内核重要文件grub.conf # grub.conf generated by anaconda # # Note that you do not have ...

  2. linux内核启动修复

    linux内核启动修复 首先看一下linux内核重要文件grub.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # gru ...

  3. 10.30 NFLS-NOIP模拟赛 解题报告

    总结:今天去了NOIP模拟赛,其实是几道USACO的经典的题目,第一题和最后一题都有思路,第二题是我一开始写了个spfa,写了一半中途发现应该是矩阵乘法,然后没做完,然后就没有然后了!第二题的暴力都没 ...

  4. SICP 习题 (1.10)解题总结

    SICP 习题 1.10 讲的是一个叫“Akermann函数”的东西,去百度查可以查到对应的中文翻译,叫“阿克曼函数”. 就像前面的解题总结中提到的,我是一个数学恐惧者,看着稍微复杂一点的什么函数我就 ...

  5. SICP 习题 (2.10)解题总结: 区间除法中除于零的问题

    SICP 习题 2.10 要求我们处理区间除法运算中除于零的问题. 题中讲到一个专业程序猿Ben Bitdiddle看了Alyssa的工作后提出了除于零的问题,大家留意一下这个叫Ben的人,后面会不断 ...

  6. 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【九度OJ】题目1208:10进制 VS 2进制 解题报告

    [九度OJ]题目1208:10进制 VS 2进制 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1208 题目描述: 对于一 ...

  8. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

    18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c  ...

  9. 2016/10/28 很久没更了 leetcode解题 3sumcloset

    16.3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closes ...

随机推荐

  1. git push异常

    git push异常:! [remote rejected] HEAD -> refs/for/master ([3149246] missing Change-Id in commit mes ...

  2. 【Social listening实操】作为一个合格的“增长黑客”,你还得重视外部数据的分析!

    本文转自知乎 作者:苏格兰折耳喵 ----------------------------------------------------- 在本文中,作者引出了"外部数据"这一概 ...

  3. mybatis四(动态sql)

    <1><select id="selectUserByConditions" parameterType="user" resultType= ...

  4. python setattr

    #object生成的对象不能使用setattr函数>>> o=object() >>> setattr(o,'name','ok') Traceback (most ...

  5. 转载:用Source Insight中看Python代码

    在Source Insight中看Python代码 http://blog.csdn.net/lvming404/archive/2009/03/18/4000394.aspx SI是个很强大的代码查 ...

  6. centos7下安装python3.7

    记录在2018年最后一个工作日: Linux环境坑爹得要死,环境本身有python2和python3.7两个版本:安装django2的时候,发现默认是python2:把python软连接到python ...

  7. c++builder 6 [Linker Fatal error] Unable to open file 'PROXIES.OBJ'

    c++builder 6 [Linker Fatal error] Unable to open file 'PROXIES.OBJ' http://blog.csdn.net/cb168/artic ...

  8. ESXi 上创建CentOS虚拟机

    之前介绍了ESXi上添加存储.本篇介绍一下在ESXi上创建CentOS虚拟机. 方法/步骤   登陆ESXi,选择“创建/注册虚拟机” 选择“创建新的虚拟机” 给装的虚拟机命名,并选择操作系统及版本 ...

  9. Excel和Word 简易工具类,JEasyPoi 2.1.7 版本发布

    JEasyPOI 简介 EasyPOI 功能如同名字easy,追求的就是简易,让一个没接触过poi的人员,可以傻瓜化的快速实现Excel导入导出.Word模板导出,可以仅仅5行代码就可以完成Excel ...

  10. day01-Python输出

    输出 用print加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world'>>>print 'hello, world' print语句也可以跟上多个字符串,用 ...