当前位置:网站首页>(POJ - 1458) common subsequence (longest common subsequence)
(POJ - 1458) common subsequence (longest common subsequence)
2022-07-06 16:23:00 【AC__ dream】
Topic link :http://poj.org/problem?id=1458
The question : Give you two strings at a time , Find the length of the longest common subsequence
This is a template problem for finding the longest common subsequence , This model is classic , Let me introduce the solution of this problem
This problem is solved by dynamic programming , set up dp[i][j] Represents the first string i Characters and the first... Of the second string j The length of the longest common subsequence of characters , Now let's start to analyze the state transition
If s1[i]==s2[j] that dp[i][j]=dp[i-1][j-1]+1( The default is... In the first string i Characters and the second string Of the j Match characters )
If not satisfied s1[i]==s2[j], that dp[i][j]=max(dp[i-1][j],dp[i-1][j]), How to understand this equation , Imagine , If dp[i][j] The last bit in the longest public string of the contained result corresponds to s1[m] and s2[n], Then it must be satisfied m!=i perhaps n!=j, because s1[i]!=s2[j], Now if n!=j Well , Then there must be dp[i][j-1]=dp[i][j], Because at this time, the second string j Characters do not belong to the characters in the longest common subsequence , So there is dp[i][j-1]=dp[i][j], Similarly, you can get if m!=i, At this time there is dp[i-1][j]=dp[i][j], therefore dp[i][j]=max(dp[i-1][j],dp[i-1][j])
At this time, some students may have doubts , Why to be s1[i]==s2[j] when dp[i][j]=dp[i-1][j-1]+1, Don't dp[i-1][j-1]+1>=max(dp[i-1][j],dp[i-1][j]), In fact, this inequality is true , According to our definition of state, we can get dp[i-1][j-1]+1>=dp[i-1][j] as well as dp[i-1][j-1]+1>=dp[i][j-1], Therefore, the above inequality will hold
The implementation is relatively simple , Here is the code :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1003;
char a[N],b[N];
int dp[N][N];//dp[i][j] Express a Middle front of string i Characters and b Middle front of string j The number of elements in the longest common subsequence of characters
int main()
{
while(scanf("%s%s",a+1,b+1)!=EOF)
{
int lena=strlen(a+1),lenb=strlen(b+1);
for(int i=1;i<=lena;i++)
for(int j=1;j<=lenb;j++)
{
if(a[i]==b[j])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
printf("%d\n",dp[lena][lenb]);
}
return 0;
}
边栏推荐
- “鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
- 双向链表—全部操作
- 1903. Maximum odd number in string
- 树莓派4B64位系统安装miniconda(折腾了几天终于解决)
- 409. Longest palindrome
- Summary of FTP function implemented by qnetworkaccessmanager
- Programmers, what are your skills in code writing?
- Codeforces - 1526C1&&C2 - Potions
- The concept of C language array
- QWidget代码设置样式表探讨
猜你喜欢
860. Lemonade change
Codeforces Round #799 (Div. 4)A~H
Borg maze (bfs+ minimum spanning tree) (problem solving report)
读取和保存zarr文件
Codeforces Round #801 (Div. 2)A~C
QT implementation fillet window
921. Minimum additions to make parentheses valid
Advancedinstaller安装包自定义操作打开文件
2078. Two houses with different colors and the farthest distance
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
随机推荐
Ball Dropping
807. Maintain the urban skyline
Codeforces Round #799 (Div. 4)A~H
1903. Maximum odd number in string
TCP's three handshakes and four waves
Summary of FTP function implemented by qnetworkaccessmanager
Frida hook so layer, protobuf data analysis
Interval sum ----- discretization
Programmers, what are your skills in code writing?
浏览器打印边距,默认/无边距,占满1页A4
Problem - 1646C. Factorials and Powers of Two - Codeforces
input 只能输入数字,限定输入
Flask框架配置loguru日志庫
Study notes of Tutu - process
628. Maximum product of three numbers
Opencv learning log 27 -- chip positioning
Configuration du cadre flask loguru log Library
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
Write web games in C language
QT realizes window topping, topping state switching, and multi window topping priority relationship