UVa 10405 & POJ 1458 Longest Common Subsequence
求最长公共子序列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的更多相关文章
- 【POJ - 1458】Common Subsequence(动态规划)
Common Subsequence Descriptions: A subsequence of a given sequence is the given sequence with some e ...
- POJ 1458:Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41957 Accepted: 16 ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- [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中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
随机推荐
- 在安装mysqli的时候,出现error: ext/mysqlnd/mysql_float_to_double.h: No such file or direc
这个属于路径问题 我直接修改mysqli_api.h文件 # vim mysqli_api.h把第36行的#include "ext/mysqlnd/mysql_float_to_doubl ...
- java sni support result in svn fail
svn: E175002: handshake alert: unrecognized_name http://stackoverflow.com/questions/7615645/ssl-han ...
- 在数据库里面有这么一个表:用m代表男,用f代表女,现在我要输出格式为中文的:男和女,sql语句该怎么写
在数据库里面有这么一个表:用m代表男,用f代表女,现在我要输出格式为中文的:男和女, sql语句该怎么写 select case sex when 'm' then '男' else '女' a ...
- zf-关于调用页面提示找不到className的原因
多亏了蒋杰 还好他上次告诉我 关于节点的问题 我一看到这个函数就想到了他以前教我的 我这里一开始就调用js函数了 所以没获取到节点 后来把方法换到这里就OK了
- 在ubuntu 上创建 ssl 证书
soap webservice 调试工具: soap UI, 可以下载下来玩一玩. Introduction TLS, or transport layer security, and its pre ...
- FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和XmlWebApplicationContext简介
今天在用Spring时遇到一个问题,提示找不到applicationContext.xml文件.原来是在加载这个文件时调用的方法不太合适,所以造成了程序找不到项目下的xml配置文件. 我们常用的加载c ...
- 解决BT5不能使用putty连接问题
root@bt:~# cd /etc/sshroot@bt:/etc/ssh# sshd-generate Generating public/private rsa1 key pair.Your i ...
- CodeForces 139C Literature Lesson(模拟)
这个题,读懂了就是水,读不懂就没办法下手,论英语阅读的重要性...只有五种形式,第一种万能型aaaa,是另外3种的特殊情况,第二种克莱里林四行打油诗aabb形式,第三种是交替的abab形式,第四种是封 ...
- c# winform 点击按钮切换tabcontrol标签
this.tabControl1.TabPages.Remove(tabPage1); this.tabControl1.TabPages.Remove(tabPage2); this.tabCont ...
- 关于BOM 的详细介绍
原文地址:http://blog.csdn.net/u011526599/article/details/51419182