String Match
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的更多相关文章
- string.match(RegExp) 与 RegExp.exec(string) 深入详解
string.match(RegExp) 与 RegExp.exec(string) 相同点与不同点对比解析: 1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null. 2. 当RegE ...
- 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 ...
- LeetCode 942. 增减字符串匹配(DI String Match) 49
942. 增减字符串匹配 942. DI String Match 题目描述 每日一算法2019/6/21Day 49LeetCode942. DI String Match Java 实现 and ...
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- 【Leetcode_easy】942. DI String Match
problem 942. DI String Match 参考 1. Leetcode_easy_942. DI String Match; 完
- 【Leetcode_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- RegExp.exec和String.match深入理解
今天在重新阅读<JavaScript权威指南>的RegExp和String的时候,看到了2个比较容易混淆的函数:RegExp的exec和String的match 这2个函数都是从指定的字符 ...
- ACM Binary String Match
#include <stdio.h> #include <string.h> #include <stdlib.h> void SubString(char sub ...
- [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 ...
随机推荐
- Xilinx Zynq ZC-702 开发(02)—— 通过 Xilinx SDK 调试 Linux 应用
远程调试环境由 PC 上运行的 System Debugger(集成在 Xilinx SDK 中) 和 Zynq 板上运行的 Linux TCF Agent 共同构成, 两者通过 TCP 连接,架构图 ...
- JSFL元件类型判断 转载于 https://blog.csdn.net/linking530/article/details/8364600
//获取舞台上第一层第一帧上的全部元件 var els = fl.getDocumentDOM().getTimeline().layers[0].frames[0].elements; //遍历元件 ...
- JS写一个简单日历
JS写一个日历,配合jQuery操作DOM <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- 统计不同渠道的的UV,再合计渠道总计UV
( SELECT `统计时间`, `贷款公司名称`, `推广员姓名`, UV FROM ac_statistics WHERE `贷款公司名称` = "金腰袋" AND `统计时间 ...
- ArrayList的addAll方法
方法实现如下: public boolean addAll(Collection c) { Object[] a = c.toArray(); int numNew = a.length; ensur ...
- Alpha冲刺
第一天 日期:2018/6/16 1.今日完成任务情况以及遇到的问题 张天旭:根据系统的需求,完成数据库的设计 周甜甜:完成系统后台登录界面的设计及登录功能的实现 李蕾:完成系统后台首页的设计 张海鑫 ...
- 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)
承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行 在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...
- Linux seq_printf输出内容不完整的问题
Linux seq_printf输出内容不完整的问题 写在前面的话:这是多年前在项目中遇到的问题,作为博客的开篇之作,有不足之处,请各位大侠斧正!谢谢! seq_file接口介绍 有许多种方法能够实现 ...
- 了解一下SQL映射文件
1:SQL映射文件 MyBatis真正强大之处就在于SQL映射语句,相对于强大的功能,SQL映射文件的配置非常简单,与JDBC相比减少了50%的代码.下面是关于SQL映射文件的几个顶级元素配置 map ...
- 使用css3实现动画来开启GPU加速
参考文章: https://www.w3cplus.com/css3/introduction-to-hardware-acceleration-css-animations.html http:// ...