当前位置:网站首页>POJ - 1458 Common Subsequence(最长公共子序列)
POJ - 1458 Common Subsequence(最长公共子序列)
2022-07-02 15:18:00 【WA_自动机】
POJ - 1458 Common Subsequence(最长公共子序列)
Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 1010;
char a[N],b[N];
int main()
{
while(scanf("%s%s",a+1,b+1)!=EOF)
{
int n=strlen(a+1),m=strlen(b+1);
int f[n+1][m+1];
memset(f,0,sizeof f);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
f[i][j]=max(f[i-1][j],f[i][j-1]);
if(a[i]==b[j]) f[i][j]=max(f[i][j],f[i-1][j-1]+1);
}
printf("%d\n",f[n][m]);
}
return 0;
}
边栏推荐
- Séparateur JS3 de niuke
- MATLAB中nexttile函数使用
- The difference of message mechanism between MFC and QT
- QWebEngineView崩溃及替代方案
- chrome浏览器快速访问stackoverflow
- Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
- Common SQL statements (complete example)
- 云通信接口更新迭代——SUBMAIL API V4正式上线
- JS20 array flattening
- 线性规划例题 投资的收益与风险
猜你喜欢
![链表求和[dummy+尾插法+函数处理链表引用常见坑位]](/img/08/30e8ca2376104d648a82dca8a72c42.png)
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
![[fluent] dart data type map type (create map set | initialize map set | traverse map set)](/img/02/75d21470ea0ae4cd3d17696a93d1ca.jpg)
[fluent] dart data type map type (create map set | initialize map set | traverse map set)

从收集到输出:盘点那些强大的知识管理工具——优秀笔记软件盘点(四)

Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!

After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning

阿里天池SQL学习笔记——DAY3

【目标跟踪】|数据集汇总

Connect Porsche and 3PL EDI cases

Believe in yourself and finish the JVM interview this time

What if the default browser cannot be set?
随机推荐
Does digicert SSL certificate support Chinese domain name application?
Blog theme "text" summer fresh Special Edition
Geoserver: publishing PostGIS data sources
Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
阿里天池SQL学习笔记——DAY3
Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
Eye of depth (III) -- determinant of matrix
SAP commerce Cloud Architecture Overview
Configure lamp+supervisor
VScode知识点——常见报错
ssb门限_SSB调制「建议收藏」
Qstype implementation of self drawing interface project practice (II)
微信小程序 —— 上下浮动的箭头
Ocio V2 reverse LUT
Microservice architecture practice: Construction of scalable distributed database cluster
Shutter: action feedback
泡椒凤爪制作教程
TCP congestion control details | 2 background
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程