题目描述

查找两个字符串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. Python学习笔记_week3_函数

    一.介绍 1.面向对象(华山派)--->类(独门秘籍)--->class(定义的关键字) 2.面向过程(少林派)--->过程--->def 3.函数式编程(逍遥派)---> ...

  2. linux 考试2

  3. JVM jmap dump 分析dump文件 / 如何使用Eclipse MemoryAnalyzer MAT 排查线上问题

    jhat简介 jhat用来分析java堆的命令,可以将堆中的对象以html的形式显示出来,包括对象的数量,大小等等,并支持对象查询语言 这个工具并不是想用于应用系统中而是用于"离线" ...

  4. 白鹭引擎 - 资源文件的加载 ( RES, loadConfig, loadGroup )

    class Main extends egret.DisplayObjectContainer { public constructor() { super(); this.addEventListe ...

  5. 关于阿里云图片识别接口的demo

    服务器处理过程 $host = "https://dm-53.data.aliyun.com"; $path = "/rest/160601/ocr/ocr_vehicl ...

  6. c# group by list

    ViewBag.PnlTotal = pnlTotal; // 柱形图 string data = ""; string cat = ""; string bu ...

  7. xlwt模块

    python使用xlwt模块操作Excel 该模块安装很简单 $ pip install xlwt 语法 import xlwt # 创建一个workbook 设置编码 workbook = xlwt ...

  8. day10-列表生成式

    列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 1.生成一个列表 a = [i for i in range(1,100) if ...

  9. JSONArray

    action层 js alert(result);   result返回的是[{"countryId":"","id":"1&qu ...

  10. 2018SDIBT_国庆个人第三场

    A - A CodeForces - 1042A There are nn benches in the Berland Central park. It is known that aiai peo ...