LCS problem using Binary-Search\Rolling hash

One TopCoder article introduces a very interesting alternative solution to Longest Common Sequences problem.

It is based on this statement (http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=stringSearching):

The important point that allows us to use BS is the fact that if the given strings have a common substring of length n, they also have at least one common substring of any length m < n. And if the two strings do not have a common substring of length n they do not have a common substring of any length m > n

So we can start b-search on string length of s1, for s2, since current substr len is fixed, we use rolling-hash to check match. Also, another interesting point to handle collision in a typical BK algorithm: we use double hashing to avoid collision, instead of a O(n) brutal-force string comp mentioned in EPI-300.

时间: 2024-10-16 14:36:55

LCS problem using Binary-Search\Rolling hash的相关文章

[LeetCode&amp;Python] Problem 704. Binary Search

Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1. Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Out

2018.3.5-6 knapsack problem, sequence alignment and optimal binary search trees

这周继续dynamic programming,这三个算法都是dynamic programming的. knapsack problem有一种greedy的解法,虽然简单但是不保证正确,这里光头哥讲的是dynamic的解法.其实和上次那个max weight independent set的算法差不多,同样是每个物件都判断一遍有这个物件和没这个物件两种情况,用bottom-up的方法来解,然后得到一个最大的value值,这时因为没有得到具体的选择方案,所以最后还需要一部重构的步骤得到具体方案.

[LeetCode&amp;Python] Problem 700. Search in a Binary Search Tree

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL. For exampl

[LeetCode&amp;Python] Problem 235. Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

[leetcode-95-Unique Binary Search Trees II]

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: This probl

九章算法系列(#2 Binary Search)-课堂笔记

前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一节课,说真的很实用,特别是对于我这种算法不扎实,并且又想找工作,提高自己的情况. 那就不多说废话了,以后每周都写个总结吧,就趁着这一个月好好把算法提高一下.具体就从:课堂笔记.leetcode和lintcode相关习题.hdu和poj相关习题三个方面来写吧.希望自己能够坚持下来,给大家分享一些好的东

【Leetcode 96】96. Unique Binary Search Trees

This is a BST(binary search tree) numbers counting problem but solved in dynamic programing. https://discuss.leetcode.com/category/104/unique-binary-search-trees This solution really gives me a shock. Before solving u need do some mathematical deduce

[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ? k ? BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

UVA1264 &amp;&amp; UVAlive4847 Binary Search Tree

A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties: (1) Every node has a key, and no two nodes have the same key. (2) The keys in a nonempty left subtree must be smaller than the key in t