这个是华为OJ上的一道题目。首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下:

(1)一定不可以有包名;
(2)主类名只能为Main;
(3)不可以输出与结果无关的信息。

  好了,按照以上规则,我们写出来的代码如下(此代码不是最优的,只是用来记录华为OJ上java代码的书写规则):

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Main mainObj = new Main();
int len = mainObj.getCommonStrLength(sc.next(),sc.next());
System.out.println(len);
} int getCommonStrLength(String str1, String str2) {
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
int len1 = str1.length();
int len2 = str2.length();
String min = null;
String max = null;
String target = null;
min = len1 <= len2 ? str1 : str2;
max = len1 > len2 ? str1 : str2;
//最外层:min子串的长度,从最大长度开始
for (int i = min.length(); i >= 1; i--) {
//遍历长度为i的min子串,从0开始
for (int j = 0; j <= min.length() - i; j++) {
target = min.substring(j, j + i);
//遍历长度为i的max子串,判断是否与target子串相同,从0开始
for (int k = 0; k <= max.length() - i; k++) {
if (max.substring(k,k + i).equals(target)) {
return i;
}
}
}
}
return 0;
}
}

【java】求两个字符串的最长公共子串的更多相关文章

  1. 求两个字符串的最长公共子串——Java实现

    要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...

  2. [URAL-1517][求两个字符串的最长公共子串]

    Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...

  3. 求两个字符串的最长公共子串(LCS)

    http://tianyunpu2008.blog.163.com/blog/static/6559379920089162236915/

  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. [转] webpack3.0踩坑:postcss-loader的使用

    解决方案: 只是换了一种引入方式,解决了 1,创建postcss.config.js文件,添加如下代码:(引入autoprefixer插件)   1 2 3 4 module.exports = {  ...

  2. vtiger7新模块的创建和配置

    vtiger出7.0了,以前的那些配置方法已经不管用了 下面是新的 模块创建及一些页面及功能配置的方法 下面介绍三个点 1.新建一个模块 2.实现单图片上传的功能 3.实现页面summary显示的功能 ...

  3. Python列表split方法

    转载自:https://www.cnblogs.com/douzi2/p/5579651.html 1.split翻译为分裂.  split()就是将一个字符串分裂成多个字符串组成的列表. 2.spl ...

  4. Linux !的使用

    转自:https://www.linuxidc.com/Linux/2015-05/117774.htm 一.history 78 cd /mnt/ 79 ls 80 cd / 81 history ...

  5. 第二章HTML,JavaScript简介

    概念: URL:网上标准资源的地址. HTTP协议:客户端发出请求和得到回应的标准协议. HTML:超文本标记语言.是网络上的通用语言,也是网络web语言基础. 2.1服务器与浏览器 举个例子:A同学 ...

  6. ubuntu16系统中pycharm下使用git将代码提交到github仓库

    1 在系统中安装git,在terminal中输入以下命令 sudo apt-get update sudo apt-get install git 2 对git进行配置,在terminal中输入以下命 ...

  7. 如何通过<include/>标签重用Mybatis的代码段

    1.首先定义一个sql标签,一定要定义唯一id<sql id="Base_Column_List" >name,age</sql>2.然后通过id引用< ...

  8. python-docx

    pip install python-docx 注意不要直接下载docx包 from docx import Document from docx.shared import RGBColor,Inc ...

  9. CKEditor 4.4.1 添加代码高亮显示插件功能--使用官方推荐Code Snippet插件

    随着CKEditor4.4.1的发布,以前一直困扰的代码高亮问题终于完美的得到解决,在CKEditor4.4中官方发布了Code Snippet这个代码片段的插件,终于可以完美的内嵌使用代码高亮了,以 ...

  10. 安卓工作室 Android studio 或 Intellij IDEA 美化 修改 汉化 酷炫 装逼 Android studio or Intellij IDEA beautify modify Chinesization cool decoration

    安卓工作室 Android studio 或 Intellij IDEA 美化 修改 汉化 酷炫 装逼 Android studio or Intellij IDEA beautify modify ...