当前位置:网站首页>Longest common subsequence
Longest common subsequence
2022-06-10 06:17:00 【Xiao--Ming】
【 Problem description 】
A subsequence of a given sequence is a sequence obtained by deleting some elements from the sequence .
To be precise , If you give a sequence X=<x1,x2,…,xm>,
Then another sequence Z=<z1,z2,…,zk> yes X The subsequence of is the existence of a strictly increasing subscript sequence <i1,i2,…,ik>, Such that for all j=1,2,…,k Yes : Xij=Zj
for example , Sequence z=<B,C,D,B> It's a sequence X=<A,B,C,B,D,A,B> The subsequence , The corresponding increasing subscript sequence is <2,3,5,7>.
【 The sample input 】
ABCBDAB
BDCABA
【 Sample output 】
4
BCBA
Refer to the answer :
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
const int maxlen=201;
int c[maxlen][maxlen]; // The length of the longest common subsequence
int n,i,j,l1,l2;
char x[maxlen],y[maxlen];
string z="";
int main()
{
scanf("%s%s",x,y);
l1=strlen(x);
l2=strlen(y);
for (i=1; i<=l1; i++)
for (j=1; j<=l2; j++)
if(x[i-1]==y[j-1])
c[i][j]=c[i-1][j-1]+1;
else
c[i][j]=max(c[i-1][j],c[i][j-1]);
printf("%d\n",c[l1][l2]);
i=l1;
j=l2;
while (i && j)
if(x[i-1]==y[j-1])
{
z=x[--i]+z; //string Class overloads the operator “+”、“=”, Can be used directly
j--;
}
else if (c[i-1][j]>c[i][j-1])
i--;
else
j--;
printf("%s\n",z.c_str()); //string Class should add .c_str() Get the corresponding string value
return 0;
}
边栏推荐
- 阿里云OCR图片识别使用流程
- What is reverse domain name resolution?
- What to say and what not to say during the interview - May 23, 2022
- Crash problem
- 如何通过netstat命令分析网络情况
- Seven-bit(七比特) ASCII是什么意思
- Teacher lihongyi's notes on machine learning -4.2 batch normalization
- Flink 系例 之 CountWindowAll
- Transformer-XL 模型详解
- IP地址0.0.0.0是什么意思?
猜你喜欢

CREMB Pro 后台子管理员403问题分析

Learning notes of introduction to deep learning

Jacobo accurate to line collation

SZT mr- in depth understanding, configuration file, exception case, load

Time complexity and space complexity

Getting started with pytorch: training a deep neural network (DNN)

Canape XCP on can project creation

Raspberry pie 4B compiling kernel module

Teacher lihongyi's notes on machine learning -4.2 batch normalization

MPC——模型预测控制
随机推荐
PM2 installation and common commands
jacoco精确到行整理
ArcGIS应用(十九)Arcgis 统计分析计算多波段图像最大值、最小值、平均值等
LAS转换为PCD两种方式
Countwindow of Flink system
阿里云OCR图片识别使用流程
CANape CAN工程的创建
error NU1202: Package Volo. Abp. Cli 5.2.1 is not compatible with netcoreapp3.1
面试当中该说的和不该说的——2022-05-23
Business topic: user growth analysis
反向域名解析是什么?
4. software engineering: Calculation of air baggage check-in fee
What does network edge mean?
ARP表是什么
Flink 系例 之 CountWindow
取消自动编号的方法
解决wireshark中npcap安装失败问题
Crash problem
Word vector: detailed explanation of glove model
SQL practice: SQL optimization problems