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 ...
随机推荐
- drf框架之跨域问题的解决与缓存问题
什么是跨域问题呢: 1. 跨域问题: CORS 跨域资源共享: 有简单请求 和非简单请求 简单请求: 只要符合如下两条,就是简单请求,否则则是非简单请求 (1) 请求方法是以下三种方法之一: HEAD ...
- activiti官网实例项目activiti-explorer之获取流程节点
如上图在保存步骤中添加获取节点信息方法nodes(); 方法如下: //获取所有节点 JsonNode modelNode = new ObjectMapper().readTree(repos ...
- tensorflow 升级到1.9-rc0,tensorboard 报错:TypeError: GetNext() takes exactly 1 argument (2 given)
Exception in thread Reloader:Traceback (most recent call last): File "/usr/lib/python2.7/threa ...
- json和jquery中的ajax
JSON: java script Object otation:js对象标记 声明一个json对象,使用key:value对应,中间用冒号连接,键值对之间用逗号连接,最外面用{}包含 声明方式: 语 ...
- Linux Apache虚拟主机配置方法
apache 虚拟主机配置 注意: 虚拟主机可以开很多个 虚拟主机配置之后,原来的默认/etc/httpd/httpd.conf中的默认网站就不会生效了 练习: 主机server0 ip:172.25 ...
- 牛客小白月赛13 小A的回文串(Manacher)
链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...
- Axis通过方法获取webService请求报文
MessageContext messageContext = _call.getMessageContext(); Message reqMsg = messageContext.getReques ...
- vue学习笔记(WebStorm安装)
慕课网:https://www.imooc.com/video/18553 一.前往官网下载:https://www.jetbrains.com/webstorm/download/#section= ...
- Android面试准备20190422
1.即时推送原理,采用的push推送模式,保持一个长连接,服务端和客户端连接后不再断开.所谓长连接,即是在一个TCP上可以连续发送多个数据包,在TCP连接保持期间,如果没有数据包发送,需要双方发送检测 ...
- JavaScript获取扫码枪相关资料
https://blog.csdn.net/jiongxian1/article/details/78906124 https://blog.csdn.net/jifengdalu/article/d ...