LCS POJ 1458 Common Subsequence
题意:输出两字符串的最长公共子序列长度
分析:LCS(Longest Common Subsequence)裸题。状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; (s[i] == t[i])dp[i+1][j+1] = max (dp[i][j+1], dp[i+1][j]); (s[i] != t[i])
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; const int MAXN = 3e2 + 10;
const int INF = 0x3f3f3f3f; char s[MAXN];
char t[MAXN];
int dp[MAXN][MAXN]; void LCS(int len1, int len2)
{
for (int i=0; i<len1; ++i)
{
for (int j=0; j<len2; ++j)
{
if (s[i] == t[j])
{
dp[i+1][j+1] = dp[i][j] + 1;
}
else
{
dp[i+1][j+1] = max (dp[i][j+1], dp[i+1][j]);
}
}
}
} int main(void) //POJ 1458 Common Subsequence
{
#ifndef ONLINE_JUDGE
freopen ("LCS.in", "r", stdin);
#endif while (~scanf ("%s%s", &s, &t))
{
memset (dp, 0, sizeof (dp));
int len1 = strlen (s);
int len2 = strlen (t); LCS (len1, len2);
printf ("%d\n", dp[len1][len2]);
} return 0;
}
LCS POJ 1458 Common Subsequence的更多相关文章
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- Poj 1458 Common Subsequence(LCS)
一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- OpenJudge/Poj 1458 Common Subsequence
1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...
- POJ 1458 Common Subsequence (动态规划)
题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...
- poj 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46387 Accepted: 19 ...
随机推荐
- MVC 使用Jquery EasyUI分页成功
先上图吧
- VS2010编译Qt4.8.2的64版本库
安装qt-win-opensource-4.8.2-vs2010.exe(从http://download.qt.io/archive/qt/4.8/4.8.2/下 载),这个是32位的,里面有编译好 ...
- grep如何忽略.svn目录,以及如何忽略多个目录
grep如何忽略.svn目录,以及如何忽略多个目录 这是我在网上看到的文章,不过里面还有问题,我的不支持,需要更换架包 grep -r 'function_name' * (*表示当前目录下所有文件, ...
- Android 录音
想要实现wav格式的编码时我们也就不能再使用MediaRecorder,而只能使用AudioRecord进行处理
- XPath的基本使用
XPath XPath 使用路径表达式来选取 XML 文档中的节点或节点集. 路径表达式 结果 bookstore 选取 bookstore 元素的所有子节点. /bookstore 选取根元素 bo ...
- "稀奇古怪的"delete this
myClass::foo(){ delete this; } .. void func(){ myClass *a = new myClass(); a->foo(); ...
- 直接放个DB2 SQL STATEMENT大全好了!
SQL statements This topic contains tables that list the SQL statements classified by type. SQL sch ...
- monkey测试
一.理解monkey测试 1.Monkey测试是Android自动化测试的一种手段.Monkey测试本身非常简单,就是模拟用户的按键输入,触摸屏输入,手势输入等,看设备多长时间会出异常. 2.当Mon ...
- 与你相遇好幸运,Linux常用命令
开机挂载硬盘 cat /etc/fstab /dev/sda1 /mnt/sda1 ext3 defaults 0 0 挂载硬盘 mount /dev/sdb5 /home/dis ...
- 对Object类中方法的深入理解
看一下API中关于Object的介绍: 类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法. 那么Object中到底有哪些方法,各自有什 ...