当前位置:网站首页>Dynamic programming: longest common substring and longest common subsequence
Dynamic programming: longest common substring and longest common subsequence
2022-07-03 03:32:00 【My family Dabao is the cutest】
The longest public substring
Given two strings , Find the longest identical substring , For example, stator string asdfas
and werasdfaswer
, The longest substring shared by the two substrings is asdfas
This string .
Again , At first glance, this problem is dynamic programming , We first list the state transition equation , Suppose two strings are separated by i,j
The length of the longest common substring at the end is dp[i][j]
, When A[i]==B[j]
when ,dp[i][j]=dp[i-1][j-1]+1
, If these two characters are not equal , That is to say A[i],B[j]
The two strings ending with are not common substrings , here dp[i][j]=0
a | s | d | f | a | s | ||||
w | e | r | a | s | d | f | a | s | w |
s1 = input()
s2 = input()
n1 = len(s1)
n2 = len(s2)
dp = [[0 for _ in range(n2)] for _ in range(n1)]
max_len = 0
for i,x in enumerate(s1):
for j,y in enumerate(s2):
if x == y:
if i == 0 or j == 0:
dp[i][j] = 1
else:
dp[i][j] = dp[i-1][j-1] + 1
if dp[i][j] > max_len:
max_len = dp[i][j]
print(max_len)
Longest common subsequence
Common subsequences do not need to be continuous , therefore , Even if two strings are not equal ,dp[i][j]
Not for 0. The current two characters are not equal ,t1[i]!=t2[j]
Words , Then the minimum length is also dp[i-1][j-1]
, But that's not enough , Because we want to get as much length as possible in the previous comparison . Then when the current characters are not equal , You should put the current character into the previous comparison , Then there must be dp[i][j-1] and dp[i-1][j]>=dp[i][j]
. Simply speaking ,dp[i][j]
The value of should be from dp[i-1][j-1],dp[i][j-1],dp[i-1][j]
Choose from the three , but dp[i][j]≤ The other two , So compare the other two , Take the larger .
The state representation we define f Array and text Array subscripts are from 1 At the beginning , And the title gives text Array subscript is from 0 At the beginning , For one A corresponding , In judging text1 and text2 When the last bit of the array is equal , One wrong place forward , That is to use text1[i - 1] and text2[j - 1] To judge .
Here's why f Array and text Arrays are defined as subscripts from 1 Start . The reason is because of the state transition equation f[i][j] = max(f[i - 1][j],f[i][j - 1]), When our f Array defined as subscript from 1 After the start , We can make no extra judgment on the problem of subscript out of bounds in the code . In fact, we can also find a problem , Is the original array given by the topic , such as text Array , If subscript from 1 At the beginning , The state representation will be clearer , The process of deriving the state transition equation will also be better understood .
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if text1[i - 1] == text2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n]
边栏推荐
- The idea setting code is in UTF-8 idea Properties configuration file Chinese garbled
- Docker install and start MySQL service
- Don't use the new Dede collection without the updated Dede plug-in
- 为什么线程崩溃不会导致 JVM 崩溃
- C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 05 data input and output
- C# WebRequest POST模式 ,基于“Basic Auth”口令认证模式,使用multipart/form-data方式上传文件及提交其他数据
- Pat class B "1104 forever" DFS optimization idea
- MongoDB复制集【主从复制】
- 2020-01-01t00:00:00.000000z date format conversion
- Summary of electromagnetic spectrum
猜你喜欢
Lvgl usage experience
Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
小程序获取用户头像和昵称
PAT乙级“1104 天长地久”DFS优化思路
Use three JS make a simple 3D scene
用Three.js做一個簡單的3D場景
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
PHP generates PDF tcpdf
Mysql Mac版下载安装教程
Hutool动态添加定时任务
随机推荐
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
Nce detail of softmax approximation
User value is the last word in the competition of mobile phone market
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
MongoDB复制集【主从复制】
ffmpeg录制屏幕和截屏
Vs 2019 configure tensorrt to generate engine
The difference between static web pages and dynamic web pages & the difference between Web1.0 and Web2.0 & the difference between get and post
MongoDB简介
[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
Pat class B common function Usage Summary
Bigvision code
渤、黄海的潮汐特征
Docker install and start MySQL service
Introduction à mongodb
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
softmax的近似之NCE详解
MySQL MAC download and installation tutorial
Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
VS克隆时显示403错误