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. nodejs + express + express-session + redis

    nodejs + express + express-session + redis 标题似乎又是不太对,大家领会精神哈 Express 安装express-generator,然后用它来创建一个工程 ...

  2. HTML: Dom event

    转自:https://developer.mozilla.org/zh-CN/docs/Web/API/Event Event接口表示在DOM中发生的任何事件; 一些是用户生成的(例如鼠标或键盘事件) ...

  3. ThinkPHP实现支付宝接口功能 代码实例

    我们这里用的是即时到帐的接口,具体实现的步骤如下: [title]一.下载支付宝接口包[/title]下载地址:https://doc.open.alipay.com/doc2/detail?tree ...

  4. anujs1.4.0发布

    经过三个月的埋头苦干,终于完成Fiber版的anujs. 主要特性有: 测试全部改成jest, 迁移官方测试用例.有许多迷你React吹得怎么天花乱坠,但是生命周期钩子的执行顺序无法与官方保持一致,那 ...

  5. LevelDB源码分析-Write

    Write LevelDB提供了write和put两个接口进行插入操作,但是put实际上是调用write实现的,所以我在这里只分析write函数: Status DBImpl::Write(const ...

  6. git 之连接tfs的git服务器

    tfs中的git的管理,注意区分是主页地址,还是代码地址,代码地址中会有   _git http://ip:8080/tfs/p/elasticsearch6.2.0 http://ip:8080/t ...

  7. 提升lua代码效率

    local test = {} , do test[ i ] = {} end local t1 = os.clock( ) , do test[ ].mValue = end local t2 = ...

  8. 【第二组】Hunter-alpha版本发布报告

    Alpha版本测试报告 一  BUG汇总 1.暂时无法进行注册.(打算修复) 2.用户发布任务界面图标按钮存在显示bug.(打算修复) 3.主界面下拉菜单暂无内容,无法弹出.(打算修复) 二  场景测 ...

  9. C# 加载静态资源问题

    加载的格式是这样:

  10. com.android.support:design

    Error:Could not find com.android.support:design:27.3.1.Required by: project :app Please install the ...