求最长公共子序列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. index.do为后缀的是什么开发语言? 有什么技术特点?

    @Override 重写父类的方法.@Nullable 表示定义的字段可以为空. 一般情况下扩展名可以体现出一个网站使用的技术,***.html?id=***,这个就是普通的html页面,然后通过ja ...

  2. 看详细的tomcat报错信息

    WEB-INF/classes目录下新建一个文件叫logging.properties handlers = org.apache.juli.FileHandler, java.util.loggin ...

  3. Struts2 语法--action

    xml的注释: <!--叨叨叨叨--> web.xml注释格式": <?xml version="1.0" encoding="UTF-8&q ...

  4. 求余区间的求和类问题 离线+线段树 HDU4228

    题目大意:给一个数组a,他的顺序是严格的单调增,然后有如下三个操作 ①加入一个val到a数组里面去,加入的位置就是a[i-1]<val<a[i+1] ②删除一个a[i]=val的值 ③查询 ...

  5. 个人学习FPGA的初步过程

    对于FPGA,完全是从零开始学习,简单讲述一下我个人学习FPGA的经历吧: 没有开发板的日子.说真的要我掏腰包买开发板觉得是一件非常奢侈的事情.理由1:现成的东西,背后影藏诸多诡异的事情我们是无法体会 ...

  6. git分支综述

    对git不是很熟悉,有个问题要弄清楚一下. 假如远程仓库有 dev 和 master 两个分支,master 作为一个稳定版分支,可用于直接发布产品,日常的开发则 push 到 dev 分支,那我本地 ...

  7. Eclipse 配置工程

    Eclipse改UTF-8 Window->Preferences->General->Workspace->Text file Encoding Eclipse配置tomca ...

  8. makefile里有哪些target?

    make help or grep ^[a-z].*\:$ Makefile | sed s,:,,g

  9. w3school之CSS学习笔记

    由于web自动化测试中,会用到比较复杂的定位方式:CSS定位,这种定位方式比较简洁,定位速度较快,所以继续学习前端的CSS知识,总结下学习笔记,以便后续查看.同时,也希望能帮助到大家. 学习网址:ht ...

  10. PAT (Advanced Level) 1013. Battle Over Cities (25)

    并查集判断连通性. #include<iostream> #include<cstring> #include<cmath> #include<algorit ...