lcs

Find common substrings between two character variables

☆樱花仙子☆ 提交于 2020-08-13 03:10:14
问题 I have two character variables (names of objects) and I want to extract the largest common substring. a <- c('blahABCfoo', 'blahDEFfoo') b <- c('XXABC-123', 'XXDEF-123') I want the following as a result: [1] "ABC" "DEF" These vectors as input should give the same result: a <- c('textABCxx', 'textDEFxx') b <- c('zzABCblah', 'zzDEFblah') These examples are representative. The strings contain identifying elements, and the remainder of the text in each vector element is common, but unknown. Is

How can we calculate longest common substring with one atmost one skip?

杀马特。学长 韩版系。学妹 提交于 2020-06-29 04:45:08
问题 Here is the code: I am able to find the longest common substring but I would like to find the longest common substring with at most one skip ? #include <iostream> using namespace std; string X,Y; int lcs(int i, int j, int count) { if (i == 0 || j == 0) return count; if (X[i-1] == Y[j-1]) { count = lcs(i - 1, j - 1, count + 1); } count = max(count, max(lcs( i, j - 1, 0), lcs( i - 1, j, 0))); return count; } int main() { int n,m; X = "AACBkkkk"; Y = "AABDkkkks"; n=X.size(); m=Y.size(); cout<

leetcode 14.最长公共前缀

放肆的年华 提交于 2020-02-02 04:57:24
14.最长公共前缀 执行用时 :0 ms, 在所有 Java 提交中击败了100.00%的用户 LCS为这些字符串的最长公共前缀,那么 L C S ( . . . L C S ( L C S ( L C S ( S 0 , S 1 ) , S 2 ) S 3 ) . . . S n ) LCS(...LCS(LCS(LCS(S0,S1),S2)S3)...Sn) L C S ( . . . L C S ( L C S ( L C S ( S 0 , S 1 ) , S 2 ) S 3 ) . . . S n ) 随意取 strs[0] 为初始p,那么strs[]的最长前缀必是 strs[0] 或及其子串,从 strs[1] 开始遍历,找出 p 与 strs[i] 的最长公共前缀前缀并存到p中。 class Solution { public String longestCommonPrefix ( String [ ] strs ) { if ( strs . length == 0 ) return "" ; String p = strs [ 0 ] ; for ( int i = 1 ; i < strs . length ; i ++ ) { while ( strs [ i ] . indexOf ( p ) != 0 ) { p = p . substring ( 0

1006 最长公共子序列Lcs

荒凉一梦 提交于 2020-01-27 11:05:06
1006 最长公共子序列 Lcs 基准时间限制: 1 秒 空间限制: 131072 KB 给出两个字符串 A B ,求 A 与 B 的最长公共子序列(子序列不要求是连续的)。 比如两个串为: abcicba abdkscab ab 是两个串的子序列, abc 也是, abca 也是,其中 abca 是这两个字符串最长的子序列。 Input 第 1 行:字符串 A 第 2 行:字符串 B (A,B 的长度 <= 1000) Output 输出最长的子序列,如果有多个,随意输出 1 个。 Input 示例 abcicba abdkscab Output 示例 abca import java.util.Scanner; import java.util.Stack; public class Main { static int dp[][]; static char a[]; static char b[]; static void LCS(int n,int m){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i-1]==b[j-1])dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=Math.max(dp[i][j-1], dp[i-1][j]); } } } public static void

Using diff in an array of objects that conform to a protocol

吃可爱长大的小学妹 提交于 2020-01-24 12:58:27
问题 I'm experimenting with using Composition instead of Inheritance and I wanted to use diff on an array of objects that comply with a given protocol. To do so, I implemented a protocol and made it comply with Equatable : // Playground - noun: a place where people can play import XCPlayground import Foundation protocol Field:Equatable { var content: String { get } } func ==<T: Field>(lhs: T, rhs: T) -> Bool { return lhs.content == rhs.content } func ==<T: Field, U: Field>(lhs: T, rhs: U) -> Bool

动态规划-Minimum Insertion Steps to Make a String Palindrome

拜拜、爱过 提交于 2020-01-07 01:25:56
2020-01-05 11:52:40 问题描述: 问题求解: 好像多次碰到类似的lcs的变种题了,都是套上了回文的壳。这里再次记录一下。 其实本质就是裸的lcs,就出结果了。 public int minInsertions(String s) { StringBuffer sb = new StringBuffer(s); String b = sb.reverse().toString(); return s.length() - lcs(s, b); } public int lcs(String str1, String str2) { int len1 = str1.length(); int len2 = str2.length(); int c[][] = new int[len1+1][len2+1]; for (int i = 0; i <= len1; i++) { for( int j = 0; j <= len2; j++) { if(i == 0 || j == 0) { c[i][j] = 0; } else if (str1.charAt(i-1) == str2.charAt(j-1)) { c[i][j] = c[i-1][j-1] + 1; } else { c[i][j] = Math.max(c[i - 1][j], c[i][j - 1]

LCS ALGORITHM ( example )

…衆ロ難τιáo~ 提交于 2020-01-03 02:32:07
问题 There's a dynamic programming algorithm to find the Longest Common Subsequence of two sequences. How can I find the LCS algorithm of two sequences X and Y. (Test of correctness) (a) X = ABEDFEESTYH Y=ABEDFEESTYHABCDF (b) X = BFAAAABBBBBJPRSTY Y=ABCDEFGHIJKLMNOPRS (c) X = ϕ (Empty Sequence), Y = BABADCAB 回答1: Here is an online calculator http://igm.univ-mlv.fr/~lecroq/seqcomp/node4.html Java public class LCS { public static void main(String[] args) { String x = StdIn.readString(); String y =

How to find Longest Common Substring using C++

爱⌒轻易说出口 提交于 2019-12-28 05:34:06
问题 I searched online for a C++ Longest Common Substring implementation but failed to find a decent one. I need a LCS algorithm that returns the substring itself, so it's not just LCS. I was wondering, though, about how I can do this between multiple strings. My idea was to check the longest one between 2 strings, and then go check all the others, but this is a very slow process which requires managing many long strings on the memory, making my program quite slow. Any idea of how this can be