UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence
Sequence 1:
Sequence 2:
Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences:
abcdgh
aedfhr
is adh of length 3.
Input consists of pairs of lines. The first line of a pair contains the first string and the second line contains the second string. Each string is on a separate line and consists of at most 1,000 characters
For each subsequent pair of input lines, output a line containing one integer number which satisfies the criteria stated above.
Sample input
a1b2c3d4e
zz1yy2xx3ww4vv
abcdgh
aedfhr
abcdefghijklmnopqrstuvwxyz
a0b0c0d0e0f0g0h0i0j0k0l0m0n0o0p0q0r0s0t0u0v0w0x0y0z0
abcdefghijklmnzyxwvutsrqpo
opqrstuvwxyzabcdefghijklmn
Output for the sample input
4
3
26
14
题意:给定两个序列,求最长公共子序列。
思路:dp中的LCS问题。。裸的很水。状态转移方程为
字符相同时: d[i][j] = d[i - 1][j - 1] + 1,不同时:d[i][j] = max(d[i - 1][j], d[i][j - 1])
代码:
#include <stdio.h>
#include <string.h> char a[1005], b[1005];
int d[1005][1005], i, j; int max(int a, int b) {
return a > b ? a : b;
}
int main() {
while (gets(a) != NULL) {
gets(b);
memset(d, 0, sizeof(d));
int lena = strlen(a);
int lenb = strlen(b);
for (i = 1; i <= lena; i ++)
for (j = 1; j <= lenb; j ++) {
if (a[i - 1] == b[j - 1]) {
d[i][j] = d[i - 1][j - 1] + 1;
}
else {
d[i][j] = max(d[i - 1][j], d[i][j - 1]);
}
}
printf("%d\n", d[lena][lenb]);
}
return 0;
}
UVA 10405 Longest Common Subsequence (dp + LCS)的更多相关文章
- UVA 10405 Longest Common Subsequence --经典DP
最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0 ...
- UVA 10405 Longest Common Subsequence
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...
- Longest Common Subsequence (DP)
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest common subsequence(LCS)
问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...
- [UVa OJ] Longest Common Subsequence
This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...
- [Algorithms] Longest Common Subsequence
The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm
''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...
随机推荐
- 【BZOJ 4169】 4169: Lmc的游戏 (树形DP)
4169: Lmc的游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 44 Solved: 25 Description RHL有一天看到lmc在 ...
- 我的OI生涯 第六章
开学了,但是我们并没有像一个正常的高二学生一样坐在教室里接受调研考试的洗礼. 暑假作业这种东西早已被甩在一旁,可以想象回去补文化课时该有多么狼狈. 大王给我们制定了周密的计划,每周两次测试,加上蔡老师 ...
- 【二项式定理】【DFS】UVALive - 7639 - Extreme XOR Sum
题意:一个序列,q次询问,每次问你某个指定区间内的EXtreme XOR值. 一个长度为l的区间的EXtreme XOR值被定义为,从左到右,将每相邻的两个数XOR起来,产生l-1个新的值,……如此循 ...
- Android UI设计规范之知识点
界面尺寸 android的尺寸众多,建议使用分辨率为720×1280的尺寸设计.这个尺寸720×1280中显示完美,在1080×1920中看起来也比较清晰;切图后的图片文件大小也适中,应用的内存消耗也 ...
- 【点分治】【路径小于等于k的条数】【路径恰好等于k是否存在】
POJ1741:Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29574 Accepted: 9915 Des ...
- HDU 5650 so easy 数学
so easy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5650 Description Given an array with n integ ...
- PID控制原理和算法
闭环控制是根据控制对象输出反馈来进行校正的控制方式,它是在测量出实际与计划发生偏差时,按定额或标准来进行纠正的.比如控制一个电机的转速,就得有一个测量转速的传感器,并将结果反馈到控制路线上.提到闭环控 ...
- C# MATLAB混合编程
我附带把matlab配置过程也给大家上传上来.[转载]终于学会C#调用matlab函数了,原来这么简单(也可以下载附件查看)自己的配置: (1)Microsoft Visual Studio 2005 ...
- [转译] AD RMS 安装最佳实践
在安装活动目录权限管理服务(ADRMS)时,请牢记以下几点: 将 AD RMS服务单独安装在一台服务器上——将 AD RMS与域控制器.微软邮件服务器(Microsoft Exchange Serve ...
- mysql表前缀
之前一直没明白,mysql有些规范里面,建议建表的时候添加前缀,它的意义究竟是为何.直到最近,我想学习一下Swift的网络请求,于是打算在新浪云新建个项目却发现新浪云免费用户最多只能建立5个项目.于是 ...