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 ...
随机推荐
- java输出日期时间
Calendar类下方法 c.add(Calendar.YEAR,4);//加4年 c.add(Calendar.MONTH,-1);// 月份减1 c.set(2012,2,23); //把时间设置 ...
- zf-关于分页必写的代码
1 存储过程 ALTER PROCEDURE [dbo].[getStatForXXGKWeb] ), ), ), @page int, -- 必写的 @pageRows int,-- 必写的 @al ...
- map转换成list
Java代码如下: package Test01; import java.util.ArrayList; import java.util.HashMap; import java.util.Ite ...
- drawRect & 内存 -> 深究
转载自:http://bihongbo.com/2016/01/03/memoryGhostdrawRect/ 在平日的开发中,随意覆盖drawRect方法,稍有不慎就会让你的程序内存暴增.下面我们来 ...
- ms08_067利用过程
进入msf. show exploits. use exploit/windows/smb/ms08_067_netapi. show playloads. set PLAYLOAD windows/ ...
- Android音频系统之AudioFlinger(二)
1.1.1 音频设备的管理 虽然AudioFlinger实体已经成功创建并初始化,但到目前为止它还是一块静态的内存空间,没有涉及到具体的工作. 从职能分布上来讲,AudioPolicyService是 ...
- mariadb cache1
http://www.percona.com/blog/2006/07/27/mysql-query-cache/ MySQL Query Cache July 27, 2006 by Peter Z ...
- javascript 数组合并
javascript 中两个数组合并,当然可以遍历其中一个数组,通过push()方法将元素插入到另一个数组中. 另外,也可以使用内置的方法(javascript Array 对象上就具有的方法,或许比 ...
- bootstrap中的居左和居右
1.pull-left和pull-right 2.text-left.text-center和text-right
- Nginx http_user_agent 防御 ab 等
日志出现大量: xxxxxxxxxxxxx - - [04/Jul/2013:23:37:49 +0800] "GET /1000.html HTTP/1.0" 200 56471 ...