Finding length of longest common substring
 /*Finding length of longest common substring using DP
* */
import java.util.*;
public class Solution {
/*
* Returns length of longest common substring of
* X[0...m-1] and Y[0...n-1]
* */
public static int LCSubStr(char X[], char Y[], int m, int n) {
/*Create a table to store lengths of longest common suffixes of substrings.
* Note that LCSuff[i][j] contains length of longest common suffix of X[0...i-1] and Y[0...j-1]
* The first row and first column entries have no logical meaning, they are only for the simplicity of program
* */ int LCStuff[][] = new int[m+1][n+1];
int result = 0;//to store the longest common substring //Following steps build LCStuff[m+1][n+1] in bottom up fashion
for(int i = 0; i <= m; i++) {
for(int j = 0; j <= n; j++) {
if(i == 0 || j == 0) {
LCStuff[i][j] = 0;
}else if(X[i-1] == Y[j-1]){
LCStuff[i][j] = LCStuff[i-1][j-1] + 1;
result = Integer.max(result, LCStuff[i][j]);
}else {
LCStuff[i][j] = 0;
}
} } return result;
} //Driver Program to test above function
public static void main(String[] args) {
String X = "GeeksforGeeks";
String Y = "GeeksQuiz"; int m = X.length();
int n = Y.length(); System.out.println("Length of longest common substring is " + LCSubStr(X.toCharArray(), Y.toCharArray(), m,n)); }
}

Print the longest common substring

 /*Print longest common substring using DP
* */
import java.util.*;
public class Solution {
/*
* Print longest common substring of
* X[0...m-1] and Y[0...n-1]
* */
public static void printLCSubStr(String X, String Y, int m, int n) {
/*Create a table to store lengths of longest common suffixes of substrings.
* Note that LCSuff[i][j] contains length of longest common suffix of X[0...i-1] and Y[0...j-1]
* The first row and first column entries have no logical meaning, they are only for the simplicity of program
* */ int LCStuff[][] = new int[m+1][n+1];
int len = 0;//to store the length of longest common substring
/* To store the index of the cell which contains the maxium value.
* The cell's index helps in building up the longest common sustring from right to left.
**/ int row = 0, col = 0; //Following steps build LCStuff[m+1][n+1] in bottom up fashion
for(int i = 0; i <= m; i++) {
for(int j = 0; j <= n; j++) {
if(i == 0 || j == 0) {
LCStuff[i][j] = 0;
}else if(X.charAt(i-1) == Y.charAt(j-1)){
LCStuff[i][j] = LCStuff[i-1][j-1] + 1;
if(len < LCStuff[i][j]) {
len = LCStuff[i][j];
row = i;
col = j;
}
}else {
LCStuff[i][j] = 0;
}
}
}
// System.out.println("fin row = " + row);
// System.out.println("fin col = " + col);
// if true, then no common substring exists
if(len == 0) {
System.out.println("No Common Substring");
return ;
} // allocate space for the longest common substring
String resultStr = ""; //traverse up diagonally from the (row, col) cell until LCStuff[row][col] ! = 0
//ex. len = 4, then (row, col) is 4 and then goes up diagonally, then you get 3-2-1-0 and append the chars from right to left in the result alongside the way
while(LCStuff[row][col] != 0) {
resultStr = X.charAt(row-1) + resultStr; //or Y[col-1]
--len; //move diagonally up to previous cell
row--;
col--;
} //print longest common substring
System.out.println("Longest common substring: " + resultStr);
} //Driver Program to test above function
public static void main(String[] args) {
String X = "ABCXYZAY";
String Y = "XYZABCB"; int m = X.length();
int n = Y.length(); printLCSubStr(X, Y, m, n); }
}

String Match的更多相关文章

  1. string.match(RegExp) 与 RegExp.exec(string) 深入详解

    string.match(RegExp) 与 RegExp.exec(string) 相同点与不同点对比解析: 1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null. 2. 当RegE ...

  2. LeetCode686——Repeated String Match

    题目:Given two strings A and B, find the minimum number of times A has to be repeated such that B is a ...

  3. LeetCode 942. 增减字符串匹配(DI String Match) 49

    942. 增减字符串匹配 942. DI String Match 题目描述 每日一算法2019/6/21Day 49LeetCode942. DI String Match Java 实现 and ...

  4. LeetCode 686. 重复叠加字符串匹配(Repeated String Match)

    686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...

  5. 【Leetcode_easy】942. DI String Match

    problem 942. DI String Match 参考 1. Leetcode_easy_942. DI String Match; 完

  6. 【Leetcode_easy】686. Repeated String Match

    problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...

  7. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

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

  8. RegExp.exec和String.match深入理解

    今天在重新阅读<JavaScript权威指南>的RegExp和String的时候,看到了2个比较容易混淆的函数:RegExp的exec和String的match 这2个函数都是从指定的字符 ...

  9. ACM Binary String Match

    #include <stdio.h> #include <string.h> #include <stdlib.h> void SubString(char sub ...

  10. [LeetCode] Repeated String Match 重复字符串匹配

    Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...

随机推荐

  1. 用java输入分数,得出分数等级

    import java.util.Scanner;public class F {  public static void main(String[] args) {  // TODO 自动生成的方法 ...

  2. Winform 中写代码布局中遇到的控件遮盖问题

    在winform中编程时,需要动态向主窗体中增加菜单栏和用户控件,菜单栏需要设置DockStyle为Top ,而设置用户控件的DockStyle为Fill,之后在加载的时候,出现了菜单栏遮盖用户控件的 ...

  3. 关于 early Z 与 z-prepass

    今天在考虑优化MOBA项目中的树木时(采用了ALPHATEST)时,与同事讨论中深入了解了这两个概念. 以前居然不知道有early z的存在,真是惭愧.... 上个链接: 深入剖析GPU Early ...

  4. linux上部署jenkins步骤小记

    一.部署jdk环境 1.下载jdk包,解压,放在选定的位置,我本次jdk包放置在“/usr/local/java/jdk” 目录下 2.配置环境变量 1)打开/etc/profile文件,在命令框中输 ...

  5. python学习Day5 几种数据类型的使用

    今日内容 数据类型分类: int | complex | float | str | bool | list | dict | tuple | set 3种数据类型的方法:(数字.字符串.列表 ) 可 ...

  6. SpringCloud-day08-Hystrix断路器

    8.Hystrix断路器 8.1.Hystrix简介 在分布式环境中,许多服务依赖项中的一些必然会失败.Hystrix是一个库,通过添加延迟容忍和容错逻辑,帮助你控制这些分布式服务之间的交互. Hys ...

  7. python 练习题(1-15)

    1.给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 2.生成双色球 3.逻辑运算(运算符优先级) 4.输入一个整数,判断这个数是几位数 5.用while循环计算 1-2+3-4...-9 ...

  8. GitHub下载提速

    通过修改hosts文件来提速(该方法也可加速其他因为CDN被屏蔽导致访问慢的网站) 第一步:获取GitHub的IP地址 通过访问:http://tool.chinaz.com/dns或者https:/ ...

  9. docker容器跑redis

    命令行配置: $ docker search redis $ docker pull docker.io/redis $ mkdir -p /redis/etc/conf /redis/lib  /r ...

  10. 配置ESP8266 NONOS SDK时由于工具链版本差异引发的故障

    前几天部署ESP8266_NONOS_SDK时遇到了一个看似奇怪的问题,描述如下: examples例程可以通过编译,但烧写到ESP8266模块后,程序无法正常运行,编译和烧写的相关配置均无误.在bo ...