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. 网络中常见的ping命令协议

    ICMP是"Internet Control Message Ptotocol"(Internet控制消息协议)的缩写.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之 ...

  2. mac ssh 命令

    https://www.cnblogs.com/littleBit/p/5362806.html 1.终端命令 1.打开Mac的命令终端,检查是不是用root登陆,如果不是的话,就输入命令:sudo ...

  3. Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条)

    Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条) 本篇博客介绍Cocos2d-x中的进度条动画,进度条涉及以下几个重要的类和方法,笔者来给大家具体解说一 ...

  4. 点滴积累【SQL Server】---使用Kettle实时同步DB2数据到SQLserver

    效果: 描述: 此操作适用于单点登录的同步用户. 首先,使用kettle将DB2数据同步到SQL中,然后添加到windows的任务计划中.定时执行同步数据. 特殊说明:此工具涉及到公司版权,所以不方便 ...

  5. Atitit.软件开发的几大规则,法则,与原则。。。attilax总结

    Atitit.软件开发的几大规则,法则,与原则... 1. 设计模式六大原则 2 1.1. 设计模式六大原则(1):单一职责原则 2 1.2. 设计模式六大原则(2):里氏替换原则 2 1.3. 设计 ...

  6. 【flink training】 打车热点区域实时统计PopularPlaces

    http://training.data-artisans.com/是Apache Flink商业公司DataArtisans提供的一个flink学习平台,主要提供了一些业务场景和flink api结 ...

  7. python之字符串处理

    #!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: strformat.py #A ...

  8. hdu Distant Galaxy(遥远的银河)

    Distant Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. 谈一谈APP版本号问题

    如题:谈一谈APP版本号问题 为什么要谈这个问题,周五晚上11~12点,被微信点名,说APP有错,无效的版本号,商城无法下单.我正在准备收拾东西,周末回老家,结果看到这样问题,菊花一紧.我擦,我刚加的 ...

  10. linx 设备名字来由 sd sr sg st

    转载保留:http://blog.csdn.net/luoweifeng1989/archive/2011/05/17/6426193.aspx 一直不知道 sda sdb sr0 这些名字的前两两个 ...