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 ...
随机推荐
- php date函数 参数详细
time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就 ...
- css+html 关于文本的总结(整理中)
布局1:固定行数 <div> <p>示例文字示例文字示例文字示例文字</p> </div> <!-- CSS代码 --> div{ widt ...
- eclipse上安装abator插件
下面是我看了网上的有一点需要强调:网址 http://ibatis.apache.org/tools/abator然后全选,然后是==>重启就好了 eclipse上安装abator插件参考:ht ...
- 解决VS2010 C++ DLL不能断点调试的问题
问题产生的过程是这样的,向exe项目(CSharp)中添加dll工程(c++开发)的引用,并将引用工程的属性“Link Library Dependencies”的值设为true,这样,在不加入lib ...
- JS时间自动更新
js部分: <!--自动更新时间--><script>function show(){var date = new Date(); //日期对象var now = " ...
- NYOJ题目893十字架
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsYAAAQRCAIAAACl4dlPAAAgAElEQVR4nO3dO1Ljyv834P8myFkIsR
- Java集合源码学习(四)HashMap分析
ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...
- Linux获取当前用户信息函数
转自:http://net.pku.edu.cn/~yhf/linux_c/function/07.html endgrent(关闭组文件) 相关函数 getgrent,setgrent 表头文件 # ...
- 重温WCF之WCF抛出异常的处理SOAP Fault(十二)
1.(服务端)抛出和(客户端)捕获SOAP Fault 当我们需要客户端获取到WCF服务端的抛出的异常的时候,使用FaultException类 WCF类库在System.ServiceModel命名 ...
- html5 Canvas绘制图形入门详解
html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome. ...