求最长公共子序列LCS,用动态规划求解。

  UVa的字符串可能含有空格,开始用scanf("%s", s);就WA了一次...那就用gets吧,怪不得要一行放一个字符串呢。

  (本来想用fgets的,可是又放弃了,形式麻烦、代码长是一小方面,另一方面fgets把'\n'字符也读入,还要做额外的处理...虽然gets有传说中的缓冲区溢出漏洞,不过多加注意一下就好啦,个人认为代码还没大到要用那些工程性的东西的时候)

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 1000+10
using namespace std; char s1[MAXN], s2[MAXN];
int c[MAXN][MAXN]; int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
while (gets(s1) && gets(s2))
{
int m = strlen(s1);
int n = strlen(s2);
memset(c, , sizeof(c));
for (int i = ; i <= m; i++)
for (int j = ; j <= n; j++)
{
if (s1[i-] == s2[j-]) c[i][j] = c[i-][j-] + ;
else c[i][j] = max(c[i][j-], c[i-][j]);
}
printf("%d\n", c[m][n]);
}
return ;
}

  在POJ中字符串用空格分割,字符串中不含空格,并且两个字符串在同一行上,用scanf("%s", s)替换get(s)就可以了

  下面是打印LCS的代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 1000+10
using namespace std; char s1[MAXN], s2[MAXN];
int c[MAXN][MAXN]; void print_LCS(int i, int j)
{
if (i == || j == ) return;
if (s1[i-] == s2[j-])
{
print_LCS(i-, j-);
printf("%c", s1[i-]);
}
else if (c[i-][j] == c[i][j]) print_LCS(i-, j);
else if (c[i][j-] == c[i][j]) print_LCS(i, j-);
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
while (scanf("%s%s", s1, s2) != EOF)
{
int m = strlen(s1);
int n = strlen(s2);
memset(c, , sizeof(c));
for (int i = ; i <= m; i++)
for (int j = ; j <= n; j++)
{
if (s1[i-] == s2[j-]) c[i][j] = c[i-][j-] + ;
else c[i][j] = max(c[i][j-], c[i-][j]);
}
printf("%d\n", c[m][n]);
// print the LCS
print_LCS(m, n);
printf("\n");
}
return ;
}

2

UVa 10405 & POJ 1458 Longest Common Subsequence的更多相关文章

  1. 【POJ - 1458】Common Subsequence(动态规划)

    Common Subsequence Descriptions: A subsequence of a given sequence is the given sequence with some e ...

  2. POJ 1458:Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41957   Accepted: 16 ...

  3. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  4. [Algorithms] Longest Common Subsequence

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

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

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

  6. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  7. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

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

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

  9. Longest Common Subsequence

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

随机推荐

  1. opencv2 矩阵方式 resize图像缩放代码(转载)

    http://blog.sina.com.cn/s/blog_74a459380101r0yx.html opencv2 矩阵方式 resize图像缩放代码(转载) (2014-05-16 09:55 ...

  2. JSP直接调用一个action定向到页面

    方法:写function <script type="text/javascript"> function mainPas(){ window.location.hre ...

  3. angular指令系列---多行文本框自动高度

    angular.module('MyApp') .directive('autoTextare', ['$timeout', function ($timeout) { return { restri ...

  4. [转]makefile文件的编写规则及实例

    http://xueqi.iteye.com/blog/1567866   1.一个简单的makefile例子   假设一个程序有两个文件file1.c,file2.c,每个文件都包含head.h,生 ...

  5. ios复制到剪贴板

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  6. PHP文件夹操作

    文件:文件+目录 判断文件类型: filetype("路径"); //返回一个字符串 is_dir("路径"); //如果是目录会返回true 判断文件是不是目 ...

  7. 基于Verilog HDL 的数字电压表设计

    本次实验是在“基于Verilog HDL的ADC0809CCN数据采样”实验上进一步改进,利用ADC0809采集到的8位数据,进行BCD编码,以供查表方式相加进行显示,本次实验用三位数码管. ADC0 ...

  8. JAVA基础--异常

    异常的分类: 1. Throwable: 根类 1) Error:系统错误, 由java虚拟机生成并抛出, 无法处理 2) Exception: 所有异常类的父类, 可以处理的错误, 可以catch到 ...

  9. git archive

    git archive --format zip --output ../g.zip 3.4.2 git archive --format=tar \ --remote=ssh://remote_se ...

  10. ILayer

    ILayer http://127.0.0.1:47873/help/1-4452/ms.help?method=page&id=ESRICARTO-7E8C676F-000BCF&p ...