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. Opencv学习笔记1:安装opencv和VS2015并进行环境配置

    用了Opencv一段时间了,简单记录一下opencv在vs2015下的配置. 第一部分:OpenCV3.2.0的下载 OpenCV官方下载地址: https://opencv.org/releases ...

  2. 【BZOJ 4070】【APIO 2015】雅加达的摩天楼

    http://www.lydsy.com/JudgeOnline/problem.php?id=4070 分块建图. 对每个\(P_i\)分类讨论,\(P_i>\sqrt N\)则直接连边,边数 ...

  3. NOIP2017 D2T2宝藏

    考场上写的prim一遍过了大样例也没想什么别的,反例也没举出来. 后来才知道由于要乘上深度所以无法贪心. 正解是状压但我不会,考后一个爆搜碾过去了. 心凉. #include<bits/stdc ...

  4. BZOJ 1497 JZYZOJ 1344 [NOI2006]最大获利 网络流 最大权闭合图

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 http://172.20.6.3/Problem_Show.asp?id=1344   思路 ...

  5. bzoj 3594

    题解见: http://blog.csdn.net/qpswwww/article/details/44407371 收获: 1.对于一个问题,看似不可做,但一定存在一定特点,我们要做的就是找出一些特 ...

  6. scrapy--将爬取得数据保存到数据库中

    首先要做的: 建库 article 建表 article 在cmd中的工作环境中安装mysql的驱动 mysqlclient pip install mysqlclient #如果是使用centos ...

  7. JDK源码(1.7) -- java.util.Queue<E>

    java.util.Queue<E> 源码分析(JDK1.7) -------------------------------------------------------------- ...

  8. 一步一步部署WPF浏览器应用程序

    WPF浏览器应用程序与Silverlight,Asp.net相比,同是发布到服务器,在IE中运行.WPF浏览器应用程序部署起来却相对困难. 相信很多朋友在第一次部署WPF浏览器应用程序时,都遇到&qu ...

  9. zend studio配置调试(Xdebug方式)

    1.下载xdebug http://xdebug.org/download.php 我下的是PHP 5.4 VC9 (32 bit) [当前系统php是php5.4.14(win32)版本] 2.配置 ...

  10. 【采集层】Kafka 与 Flume 如何选择(转)

    原文链接:[采集层]Kafka 与 Flume 如何选择 采集层 主要可以使用Flume, Kafka两种技术. Flume:Flume 是管道流方式,提供了很多的默认实现,让用户通过参数部署,及扩展 ...