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)的更多相关文章

  1. UVA 10405 Longest Common Subsequence --经典DP

    最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0                 ...

  2. UVA 10405 Longest Common Subsequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...

  3. Longest Common Subsequence (DP)

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  4. Longest common subsequence(LCS)

    问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...

  5. [UVa OJ] Longest Common Subsequence

    This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...

  6. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  9. 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm

    ''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...

随机推荐

  1. 【转】全面了解Mysql中的事务

    为什么要有事务? 事务广泛的运用于订单系统.银行系统等多种场景.如果有以下一个场景:A用户和B用户是银行的储户.现在A要给B转账500元.那么需要做以下几件事: 1. 检查A的账户余额>500元 ...

  2. WC2018伪题解

    NOIP分数过低的场外选手,一个月之后才有幸膜到这套卷子.感觉题目质量很不错啊,可惜了T1乱搞可过,T2题目出锅非集训队员没有通知到,导致风评大幅被害. 感觉Cu的话随手写两个暴力就稳了,Ag的话T3 ...

  3. []APC001

    题目质量都好高啊... A:求一个是$X$的倍数但不是$Y$的倍数的数,无解输出$-1$ 无解就是$Y|X$,否则输出$X$即可 B:给定$a_{1\cdots n},b_{1\cdots n}$,求 ...

  4. ACM -- 算法小结(九)DP之Humble numbers

         DP -- Humble numbers  //一开始理解错题意了,题意是是说一些只有唯一一个质因数(质因数只包括2,3,5,7)组成的数组,请找出第n个数是多少 //无疑,先打表,否则果断 ...

  5. 思科DCHP解决方案

    DHCP功能平时用的不算少,而且本人的几乎所有的DHCP功能都是在交换机上实现的,虽然也可以通过PC实现,而且PC的DHCP Server功能还更完善,比如可以不受DHCP作用域的限制.IP分配情况直 ...

  6. windows2008服务器连接Oracle慢的问题。

    昨天发布程序到2008服务器的IIS,从Sql Server数据库取数没问题,但是从Oracle数据库取数,非常的慢,同样的程序在2003服务器上没问题,本机也没问题.一开始怀疑是这台机器有问题,后来 ...

  7. nodejs 导入导出模块module.exports向外暴露多个模块 require导入模块

    .moudel.exports 导出模块 导出单个模块 // user.js moudel.exports = 函数名或者变量名: //app.js 导入 require('user.js') 当然. ...

  8. 解决数据库 Table 'content_tags' is marked as crashed and should be repaired 表损坏问题

    今天突然网站TAG页面打不开了,打开debug,发现提示 Table 'content_tags' is marked as crashed and should be repaired 这样的错误 ...

  9. Circuit provides reference for multiple ADCs

    The achievable accuracy for systems with multiple ADCs depends directly on the reference voltages ap ...

  10. 开源Word读写组件DocX介绍与入门

    来源:http://i.cnblogs.com/EditPosts.aspx?opt=1 读写Offic格式的文档,大家多少都有用到,可能方法也很多,组件有很多.这里不去讨论其他方法的优劣,只是向大家 ...