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 ...
随机推荐
- 关于 devbridge
目前据我所知最好用的 autocomplete 插件就是 jquery-ui 的 autocomplete 以及 devbridge 的 autocomplete 插件. 我最终选择了 devbrid ...
- HDU 3339 In Action【最短路+01背包】
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...
- Linux Shall命令入门
Linux Shall命令入门 ifconfig //查看ip信息 service network start ...
- [AHOI2009]同类分布
题目大意: 问在区间[l,r]内的正整数中,有多少数能被其个位数字之和整除. 思路: 数位DP. 极端情况下,每一位都是9,所以各位数字之和不超过9*18.(为了方便这里用了9*19) f[i][j] ...
- poj 1733 并查集+hashmap
题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...
- HDU 4612 Warm up tarjan 树的直径
Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...
- Codeforces Beta Round #5 A. Chat Server's Outgoing Traffic 水题
A. Chat Server's Outgoing Traffic 题目连接: http://www.codeforces.com/contest/5/problem/A Description Po ...
- Percona-Toolkit学习之安装和配置
http://blog.chinaunix.net/uid-26446098-id-3390779.html
- unity基础开发----unity游戏速度更快的简易检查表
让游戏速度更快的简易检查表 保持顶点数在 200K 下面,针对 PC 时每帧应为 3M,主要取决于目标 GPU. 若使用内置着色器,请在移动 (Mobile) 或未点亮 (Unlit) 的类别中选择. ...
- /etc/fstab格式的问题
[root@localhost etc]# cat fstab /dev/VolGroup00/LogVol00 / ext3 defaults ...