[UVa OJ] Longest Common Subsequence
This is the classic LCS problem. Since it only requires you to print the maximum length, the code can be optimized to use only O(m) space (see here).
My accepted code is as follows.
#include <iostream>
#include <string>
#include <vector> using namespace std; int lcs(string s, string t) {
int m = s.length(), n = t.length();
vector<int> cur(m + , );
for (int j = ; j <= n; j++) {
int pre = ;
for (int i = ; i <= m; i++) {
int temp = cur[i];
cur[i] = (s[i - ] == t[j - ] ? pre + : max(cur[i], cur[i - ]));
pre = temp;
}
}
return cur[m];
} int main(void) {
string s, t;
while (getline(cin, s)) {
getline(cin, t);
printf("%d\n", lcs(s, t));
}
return ;
}
Well, try this problem here and get Accepted :)
[UVa OJ] Longest Common Subsequence的更多相关文章
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- UVA 10405 Longest Common Subsequence
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...
- UVA 10405 Longest Common Subsequence --经典DP
最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0 ...
- [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 ...
随机推荐
- glassfish3操作命令
glassfish3操作命令 启动:C:\Java\glassfish\bin>asadmin start-domain domain1 停止:C:\Java\glassfish\bin&g ...
- unity3d之NGUI学习流水账
博主是跟着视频教程学的,所以最新版的u3d是否已经自带这个功能博主没有考究过. 但是视频是2015下半年的教程,当时的u3d还是需要自行导入NGUI包的. 1.首先需要下载NGUI包.点此进入ngui ...
- JS正则表达式(转载)
在JavaScript中,RegExp对象表示正则表达式,用来对字符串进行匹配. 一.两种定义方法: 1.直接量法: /pattern/attribute 2.对象法: new RegExp(patt ...
- WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!问题解决
用mac终端ssh连接Linux服务器,提示以下错误: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: RE ...
- Office 365 离线安装
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://yueque.blog.51cto.com/4580340/1707479 有个O ...
- C 语言文件拷贝
相关的方法: int fputs(const char*s,FILE *stream); int gets(char *s,int size,FILE *stream); 具体代码如下 /** *@a ...
- window下安装Node.js NPM
一.安装Node.js 下载地址:http://nodejs.org/download/ 1..msi文件,直接安装,包括了npm,结束. 2..exe文件,把node.exe所在主目录,加入到系统P ...
- CentOS下安装man手册
1.命令: yum install man 后发现,有的函数仍然没有.经过一番查找,原来安装的不完全,还要执行下面命令: yum install man-pages 2.总结:cengos下安装man ...
- LINQ----1
Student[] stAry ={ ), ), ), ), ), ), }; var query1 = from vall in stAry select vall; foreach (Studen ...
- redis数据类型[string 、list 、 set 、sorted set 、hash]
1. Keys redis本质上一个key-value db,所以我们首先来看看他的key. 首先key也是字符串类型,但是key中不能包括边界字符:由于key不是binary safe的字符串, ...