当前位置:网站首页>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;
}
边栏推荐
- Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
- How openharmony starts fa (local and remote)
- Qstype implementation of self drawing interface project practice (II)
- si446使用记录(一):基本资料获取
- [fluent] dart data type map type (create map set | initialize map set | traverse map set)
- Use the API port of the bridge of knowledge and action to provide resources for partners to access
- IPtables中SNAT、DNAT和MASQUERADE的含义
- Qwebengineview crash and alternatives
- SAP Commerce Cloud 架构概述
- Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
猜你喜欢

Eye of depth (II) -- matrix and its basic operations

Does digicert SSL certificate support Chinese domain name application?
![[web technology] 1233 seconds understand web component](/img/42/c98d8112dc4ece0a92dda97647be5c.jpg)
[web technology] 1233 seconds understand web component

剑指 Offer 25. 合并两个排序的链表

QStyle实现自绘界面项目实战(二)

Qstype implementation of self drawing interface project practice (II)

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

线性规划例题 投资的收益与风险

例题 非线性整数规划

A case study of college entrance examination prediction based on multivariate time series
随机推荐
深度之眼(三)——矩阵的行列式
ETH数据集下载及相关问题
【目标跟踪】|数据集汇总
How to create a new page for SAP Spartacus storefront
【GAMES101】作业4 Bézier 曲线
关于我
Migrate your accelerator based SAP commerce cloud storefront to Spartacus
Eye of depth (II) -- matrix and its basic operations
uniapp H5页面调用微信支付
Un an à dix ans
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
牛客 JS3 分隔符
[shutter] dart data type (dynamic data type)
vector的底层模拟实现
executescalar mysql_ ExecuteScalar()
云通信接口更新迭代——SUBMAIL API V4正式上线
si446使用记录(二):使用WDS3生成头文件
Sword finger offer 25 Merge two sorted linked lists
What is agile development process
871. Minimum refueling times