当前位置:网站首页>Mengxin summary of LCs (longest identical subsequence) topics
Mengxin summary of LCs (longest identical subsequence) topics
2022-07-05 08:51:00 【Qizi K】
Mengxin about LCS( Longest identical subsequence ) Summary of topics
LCS It's also a classic DP The problem is ~O(nm) Time complexity of , Scrolling array Optimize space complexity
A.Common Subsequence POJ - 1458
LCS The template question of ~
tips: When reading a string, it is subscript 0 At the beginning , No 1 Oh
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char str1[1005],str2[1005];
int dp[1005][1005];
int main(){
while(~scanf("%s %s",str1,str2)){
memset(dp,0,sizeof(dp));
int len1 = strlen(str1), len2 = strlen(str2);
for(int i = 1; i <= len1; ++i){
for(int j = 1; j <= len2; ++j){
if(str1[i - 1] == str2[j - 1]) 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[len1][len2]);
}
return 0;
}
B.Palindrome POJ - 1159 / Luogu P1435 Back string
tips: Reverse the original string to get another string , Find their longest common subsequence , In this way, we can find the longest number of characters that can form a palindrome .( Positive and reverse order “ public ” The part of is the palindrome part )
Be careful POJ 1159 open 5005*5005 Of int It will explode ~ To open short!
In addition, the reverse order string can be used without opening a new array , You can also cycle directly from back to front on the original array .( Be careful algorithm Inside reverse There is no return value )
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int len;
short dp[5005][5005];
string s1,s2;
int main(){
scanf("%d",&len);
cin >> s1;
s2 = s1;
reverse(s1.begin(), s1.end());
for(int i = 1; i <= len; ++i){
for(int j = 1; j <= len; ++j)
if(s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
printf("%d",len - dp[len][len]);
return 0;
}
In fact, space can be optimized ~ Wanted 01 It's like a backpack ,dp Arrays only use i - 1 Row data ,0-(i-2) Row data is useless . We can use a rolling array to solve the problem :
Space optimized code :
//LCS Space optimization
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int len,dp[2][5005],cur,pre;
string s1,s2;
int main(){
scanf("%d",&len);
cin >> s1;
s2 = s1;
reverse(s1.begin(), s1.end());
cur = 1, pre = 0;
for(int i = 1; i <= len; ++i){
for(int j = 1; j <= len; ++j)
if(s1[i - 1] == s2[j - 1]) dp[cur][j] = dp[pre][j - 1] + 1;
else dp[cur][j] = max(dp[pre][j], dp[cur][j - 1]);
swap(cur,pre);
}
printf("%d",len - dp[pre][len]);// Notice that it has to be pre! Because I will finish pre and cur In exchange for
return 0;
}
C.Compromise POJ - 2250
LCS Print sequence ~ Just take the word as a whole , The essence is LCS. May adopt dfs To output , Pay attention only when
ans == dp[x - 1][y - 1] + 1 && ans == dp[x - 1][y] + 1 && ans == dp[x][y - 1] + 1 It is certain to find ~
p.s. This question has multiple groups of input .
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int dp[105][105],len1,len2,cnt;
string str1[105],str2[105],s,road[105];
void dfs(int ans, int x, int y){
if(!x || !y) return ;
if(ans == dp[x - 1][y - 1] + 1 && ans == dp[x - 1][y] + 1 && ans == dp[x][y - 1] + 1) road[ans] = str1[x], dfs(ans - 1,x - 1, y - 1);
else if(ans == dp[x - 1][y]) dfs(ans,x - 1, y);
else if(ans == dp[x][y - 1]) dfs(ans, x, y - 1);
}
int main(){
while(cin >> s){
len1 = len2 = 0;
while(s != "#"){
str1[++len1] = s;
cin >> s;
}
cin >> s;
while(s != "#"){
str2[++len2] = s;
cin >> s;
}
memset(dp,0,sizeof(dp));
for(int i = 1; i <= len1; ++i){
for(int j = 1; j <= len2; ++j){
if(str1[i] == str2[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = max(dp[i - 1][j],dp[i][j - 1]);
}
}
dfs(dp[len1][len2],len1,len2);
cout << road[1];
for(int i = 2; i <= dp[len1][len2]; ++i)
cout << " " << road[i];
putchar('\n');
}
return 0;
}
边栏推荐
- Programming implementation of ROS learning 2 publisher node
- 整形的分类:short in long longlong
- Guess riddles (5)
- 图解八道经典指针笔试题
- Infix expression evaluation
- C [essential skills] use of configurationmanager class (use of file app.config)
- asp.net(c#)的货币格式化
- Count of C # LINQ source code analysis
- Explore the authentication mechanism of StarUML
- Search data in geo database
猜你喜欢

Programming implementation of subscriber node of ROS learning 3 subscriber

Classification of plastic surgery: short in long long long

Ros-11 common visualization tools

Redis实现高性能的全文搜索引擎---RediSearch

EA introduction notes

猜谜语啦(3)

The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis

Guess riddles (11)

Guess riddles (5)

Business modeling of software model | vision
随机推荐
319. 灯泡开关
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
Halcon wood texture recognition
U8g2 drawing
Configuration and startup of kubedm series-02-kubelet
[formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
Bit operation related operations
Xrosstools tool installation for X-Series
Meta标签详解
696. 计数二进制子串
Characteristic Engineering
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
MPSoC QSPI flash upgrade method
Run menu analysis
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
Business modeling of software model | overview
696. Count binary substring
Guess riddles (7)
C#【必备技能篇】ConfigurationManager 类的使用(文件App.config的使用)
location search 属性获取登录用户名