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的更多相关文章

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

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

  2. UVA 10405 Longest Common Subsequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...

  3. UVA 10405 Longest Common Subsequence --经典DP

    最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0                 ...

  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. 关于wcf,webservice,webapi或者其他服务或者接口有什么区别 WCF、WebAPI、WebService之间的区别 【转载】HTTP和SOAP完全就是两个不同的协议 WebService学习总结(一)——WebService的相关概念

    wcf,webservice采用的是rpc协议,这个协议很复杂,所以每次要传递.要校验的内容也很复杂,别看我们用的很简单,但实际是frame帮我们做掉了rpc生成.解析的事情webapi遵循是rest ...

  2. 【java设计模式】之 单例(Singleton)模式

    1. 单例模式的定义 单例模式(Singleton Pattern)是一个比較简单的模式.其原始定义例如以下:Ensure a class has only one instance, and pro ...

  3. sql server中Join有几种

    JOIN: 如果表中有至少一个匹配,则返回行 (也就是 inner join)LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有 ...

  4. DOS命令 bat-call的用法

    call 从批处理程序调用另一个批处理程序 call有几种用法 第一种用法,也就是最常用的一种,调用另一个批处理,在被调用的批处理执行完后在执行call下面的命令.如: @echo off call ...

  5. Android--&gt;Realm(数据库ORM)使用体验,lambda表达式

    Realm,为移动设备而生.替代 SQLite 和 Core Data. 非常庆幸,官方帮助文档有中文: https://realm.io/cn/docs/java/latest/ 尽管眼下最新的版本 ...

  6. [svc]linux文件特殊权限

    这是老以前写的文章, 断断续续的可见那时候的心态还是不稳的. 生产使用: g1,g2组2个组的员工,  g2组要访问g1组/home下的文件,rx权限.  这个setfacl就有用. 方法1: 修改普 ...

  7. cacheManager载入问题

    net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please pr ...

  8. erlang supervisor simple_one_for_one实例

    simple_one_for_one vs one_for_one: 相同点: 这种Restart Strategy和one_for_one基本相同(即当一个child process挂掉后,仅仅重启 ...

  9. 利用docker搭建测试环境--安装

    软件测试过程中,总会碰到测试环境不够用的尴尬情况.即时有了机器还要经历装系统,配置环境,调试等一系列繁琐的问题.虽然市面上也有一些批处理话的工具(如salt,fabric等),但是还是需要实体机器作为 ...

  10. linux引导模式两种

    https://www.ibm.com/developerworks/cn/linux/l-bootload.html