题目描述

查找两个字符串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爬虫实战】微信爬虫

    所谓微信爬虫,即自动获取微信的相关文章信息的一种爬虫.微信对我们的限制是很多的,所以我们需要采取一些手段解决这些限制主要包括伪装浏览器.使用代理IP等方式http://weixin.sogou.com ...

  2. uva-10391-枚举

    题意:对于输入的字符串,判断是否存在一个单词a=b+c 俩种方法,枚举每一个单词进行拼接,复杂度是n*n 枚举每一个单词,对单词进行substr,判断substr出来的是不在map里面 #includ ...

  3. 修改json文件

    第三方库jq https://stedolan.github.io/jq/manual/ cat old_deploy.json \ | jq --arg cpu_limit $cpu_limit ' ...

  4. 25.纯 CSS 创作一个慧星拖尾效果的 loader 动画

    原文地址:https://segmentfault.com/a/1190000014916281 简化地址:https://codepen.io/pen/?editors=1100 HTML代码: & ...

  5. 《算法》第六章部分程序 part 4

    ▶ 书中第六章部分程序,包括在加上自己补充的代码,利用后缀树查找最长重复子串.查找最大重复子串并输出其上下文(Key word in context,KWIC).求两字符串的最长公共子串 ● 利用后缀 ...

  6. python中的replace

    replace用于修改列表.元组.字典中的元素, 例子: 1 li = ["alec", " aric", "Alex", "To ...

  7. hive 索引

    hive 有限的支持索引,不支持主键外键,可以对表添加索引,也可以为某个分区添加索引.维护索引也要额外的存储空间和计算资源. 创建索引需要指定索引处理器 如 as 'org.apache.hadoop ...

  8. 5. Tomcat窗口标题修改

    具体修改方法如下: 进入tomcat的bin目录,打开catalina.bat .找到下面的内容: if not "%OS%" == "Windows_NT" ...

  9. List转数组

    eg: List<Product> products = new ArrayList<Product>(); Product[] array = products.toArra ...

  10. JVM虚拟机宕机_java.lang.OutOfMemoryError: unable to create new native thread

    原因:当前用户的系统最最大程序数数已达到最大值,使用ulimit -u可以看到是1024   解决办法:在当前用户下使用ulimit -u 65535 然后再执行jsp,一切ok     功能说明:控 ...