POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)
2024-08-29 00:35:23
题目链接:http://poj.org/problem?id=1458
Common Subsequence
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 55099 | Accepted: 22973 |
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
Source
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; char a[MAXN], b[MAXN];
int dp[MAXN][MAXN]; int main()
{
while(scanf("%s%s", a+, b+)!=EOF)
{
int n = strlen(a+);
int m = strlen(b+); ms(dp, );
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(a[i]==b[j])
dp[i][j] = dp[i-][j-]+;
else
dp[i][j] = max(dp[i][j-], dp[i-][j]);
}
printf("%d\n", dp[n][m]);
}
}
最新文章
- android Bundle savedInstanceState用途
- IE下new Date不支持传参数的解决
- hdu 1272 小希的迷宫 解题报告
- 【BZOJ】2178: 圆的面积并
- AWR快照管理
- MySQL中的WITH ROLLUP
- 单片机串口通讯RXD与TXD如何对接详解
- 在.NET下学习Extjs(第四个案例 Extjs扩展的原理)
- Python中的列表解析和生成表达式
- 广域网佰腾玩O2O笑话——它看起来很漂亮,注定不低于
- 【iOS发展-28】制造业UITabBarController标记控制器、定制UITabBarItem文字图像6途径和More评论
- IOS学习之路七(通过xib自定义UITableViewCell)
- [编织消息框架][JAVA核心技术]动态代理应用10-水平扩展方案
- Android官方命令深入分析之dmtracedump
- codeforces 13 D
- spring boot 如何添加拦截
- 由ngx.say和ngx.print差异引发的血案
- Java中输出正则表达式匹配到的内容
- Spark入门——什么是Hadoop,为什么是Spark?
- Redis学习第八课:Redis高级实用特性(一)