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学习前的一些准备

    1.JDK - (Java SE Development Kit) JDK是Java开发所需要的环境,就跟我们想玩某个网游一样,玩之前一定是需要先安装相应的程序包的.那这个JDK就是我们准备登陆Jav ...

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

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

  3. C# ConfigurationManager不存在问题解决

    在做串口通信的时候,需要使用"ConfigurationManager"类,但是添加"Using System.Configuration"命名空间后编译器依旧 ...

  4. inpu控件接受pipe的处理结果

    input控件绑定的变量,要接受用户的输入值,一般只要使用   [(ngModel)]  就可以. 但是,pipe处理结果如何反映到变量里去呢?不知道吧?嘿嘿 这样就可以了 :  <input ...

  5. java将错误信息写入文件

    第一种办法可以通过字符串,也就是先把错误信息写入字符串,再将字符串写入文件 import java.io.*; public class Demo { public static void main( ...

  6. centos7.4下的KVM虚拟机安装使用

    本来是用的vmware,不过后来想试下KVM,想着装个ZSTACK也行,结果zstack使用网络安装没搞明白,把物理机系统毁了,这下彻底完蛋了,只好还装个centos了,但是又不想用VMWARE就想起 ...

  7. 解决laravel使用QQ邮箱发邮件失败

    在 laravel 中使用 QQ 发送邮件的时候莫名其妙的出现了如下错误:Connection could not be established with host smtp.exmail.qq.co ...

  8. 【读书笔记】segment routing mpls数据平面-2

  9. .NET大批量插入数据到Oracle

    跟一个第三方系统做接口,需要插入几百万条数据到Oracle数据库. 下载Oracle的Managed版本的ODP.NET组件,只需要一个Oracle.ManagedDataAccess.dll这个DL ...

  10. 如何激活已经运行过的Activity, 而不是重新启动新的Activity

    Intent i=new Intent(this,Activity1.class);   i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);   st ...