当前位置:网站首页>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]
边栏推荐
- 简易版 微信小程序开发之for指令、上传图片及展示效果优化
- Docker install and start MySQL service
- MongoDB安装 & 部署
- The XML file generated by labelimg is converted to VOC format
- [pyg] understand the messagepassing process, GCN demo details
- Mongodb master profile
- Réglez la hauteur et lancez le système. Currenttimemillis catton
- Applet (continuous update)
- [Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
- VS 2019配置tensorRT
猜你喜欢

ffmpeg录制屏幕和截屏

VS 2019配置tensorRT

npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

Vs 2019 installation and configuration opencv

VS 2019安装及配置opencv

简易版 微信小程序开发之for指令、上传图片及展示效果优化

递归:快速排序,归并排序和堆排序

Limit of one question per day

Application of derivative in daily question

The idea cannot be loaded, and the market solution can be applied (pro test)
随机推荐
[embedded module] OLED display module
User value is the last word in the competition of mobile phone market
VS 2019 配置tensorRT生成engine
The file marked by labelme is converted to yolov5 format
LVGL使用心得
Use three JS make a simple 3D scene
@The difference between Autowired, @qualifier, @resource
FileZilla Client下載安裝
Idea format code idea set shortcut key format code
npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
softmax的近似之NCE详解
The series of hyperbolic function in daily problem
Section 26 detailed explanation and demonstration of IPSec virtual private network configuration experiment - simulation experiment based on packettracer8.0
Ansible introduction [unfinished (semi-finished products)]
MongoDB简介
Elsevier latex 提交文章 pdftex.def Error: File `thumbnails/cas-email.jpeg‘ not found: using draf
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(一)
递归使用和多维数组对象变一维数组对象
用Three.js做一个简单的3D场景
Summary of electromagnetic spectrum