当前位置:网站首页>POJ - 1458 common subsequence (longest common subsequence)
POJ - 1458 common subsequence (longest common subsequence)
2022-07-02 17:42:00 【WA_ automata】
POJ - 1458 Common Subsequence( Longest 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;
}
边栏推荐
- Wechat applet - arrows floating up and down
- Uniapp H5 page calls wechat payment
- Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
- RK1126平台项目总结
- [shutter] dart data type (dynamic data type)
- Meanings of SNAT, DNAT and masquerade in iptables
- Eye of depth (II) -- matrix and its basic operations
- Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
- 【网络是怎样连接的】第六章 请求到达服务器以及响应给客户端(完结)
- 简单介绍BASE64Encoder的使用
猜你喜欢

简单线性规划问题

Si446 usage record (I): basic data acquisition
![链表求和[dummy+尾插法+函数处理链表引用常见坑位]](/img/08/30e8ca2376104d648a82dca8a72c42.png)
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
![[web technology] 1233 seconds understand web component](/img/42/c98d8112dc4ece0a92dda97647be5c.jpg)
[web technology] 1233 seconds understand web component

每日一题——小乐乐改数字

Experience home office, feel the completion of the project | community essay solicitation

si446使用记录(一):基本资料获取

Eye of depth (III) -- determinant of matrix

freemarker+poi实现动态生成excel文件及解析excel文件
![[非线性控制理论]7_High gain and High Frequency](/img/e5/6c5ca4a89c97d9613cddccb281b35b.png)
[非线性控制理论]7_High gain and High Frequency
随机推荐
微信小程序 —— 上下浮动的箭头
Leetcode question brushing record | 933_ Recent requests
Daily question - xiaolele changes the number
HBuilderX运行到手机或模拟器提示没有找到设备
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
将您的基于 Accelerator 的 SAP Commerce Cloud Storefront 迁移到 Spartacus
求简单微分方程
Map集合详细讲解
Wechat applet - arrows floating up and down
Idea2021.1 installation tutorial
云通信接口更新迭代——SUBMAIL API V4正式上线
The beginning of life
Séparateur JS3 de niuke
When the industrial Internet began to enter the deep-water area, it appeared more in the form of industry
Vscode + eslint configuration
Si446 usage record (II): generate header files using wds3
Schoolbag novel multithreaded crawler [easy to understand]
How to create a new page for SAP Spartacus storefront
PCL知识点——体素化网格方法对点云进行下采样
Five reasons to choose SAP Spartacus as the implementation framework of SAP commerce cloud storefront