当前位置:网站首页>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;
}
边栏推荐
- A case study of college entrance examination prediction based on multivariate time series
- Shutter: action feedback
- Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
- Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
- TCP congestion control details | 2 background
- 博客主题 “Text“ 夏日清新特别版
- HDU - 1114 Piggy-Bank(完全背包)
- Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
- 从收集到输出:盘点那些强大的知识管理工具——优秀笔记软件盘点(四)
- 剑指 Offer 26. 树的子结构
猜你喜欢
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
关于我
剑指 Offer 22. 链表中倒数第k个节点
智能垃圾桶(五)——点亮OLED
Eye of depth (II) -- matrix and its basic operations
[fluent] dart data type map type (create map set | initialize map set | traverse map set)
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
871. 最低加油次数
A case study of college entrance examination prediction based on multivariate time series
si446使用记录(二):使用WDS3生成头文件
随机推荐
Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
常用SQL语句(完整范例)
Microservice architecture practice: Construction of scalable distributed database cluster
ROS知识点——消息过滤器 ( message_filters)
[fluent] dart data type map type (create map set | initialize map set | traverse map set)
Vscode + eslint configuration
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
Experience home office, feel the completion of the project | community essay solicitation
One year is worth ten years
Eye of depth (III) -- determinant of matrix
SAP commerce cloud storefront framework selection: accelerator or Spartacus?
OpenHarmony如何启动FA(本地和远程)
Configure lamp+supervisor
Microservice architecture practice: using Jenkins to realize automatic construction
默认浏览器设置不了怎么办?
Solving simple differential equations
871. 最低加油次数
How to quickly distinguish controlled components from uncontrolled components?
chmod命令原理及用法详解[通俗易懂]
What is agile development process