当前位置:网站首页>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;
}
边栏推荐
- Uniapp H5 page calls wechat payment
- 书包网小说多线程爬虫[通俗易懂]
- Idea2021.1 installation tutorial
- Shutter: action feedback
- Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
- Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
- Map集合详细讲解
- The difference of message mechanism between MFC and QT
- Sword finger offer 24 Reverse linked list
- 剑指 Offer 24. 反转链表
猜你喜欢
随机推荐
Eye of depth (III) -- determinant of matrix
Example nonlinear integer programming
13、Darknet YOLO3
【目标跟踪】|SiamFC
Common SQL statements (complete example)
executescalar mysql_ ExecuteScalar()
VScode知识点——常见报错
Experience home office, feel the completion of the project | community essay solicitation
Blog theme "text" summer fresh Special Edition
ROS knowledge points -- the difference between ros:: nodehandle N and NH ("~")
简单介绍BASE64Encoder的使用
Uniapp H5 page calls wechat payment
dstat使用[通俗易懂]
Helm kubernetes package management tool
Qwebengineview crash and alternatives
Idea2021.1 installation tutorial
TCP拥塞控制详解 | 2. 背景
QWebEngineView崩溃及替代方案
uniapp H5页面调用微信支付
Introduce the scrollintoview() method attribute in detail









