HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence【DP】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 44280 Accepted Submission(s): 20431
Problem Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
题意
求解两个字符串的最长公共子序列
思路
如果两个字符串的最后一个字符相等,那么由这最后一个字符组成的最长公共子序列就是 前面的最长公共子序列长度+ 1 然后往前推 就可以了
DP[i][j] = DP[i - 1][j - 1] + 1
如果不相等
DP[i][j] = max(DP[i - 1][j], DP[i][j - 1])
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
int dp[maxn][maxn];
int main()
{
string a, b;
while (cin >> a >> b)
{
int len_a = a.size(), len_b = b.size();
memset(dp, 0, sizeof(dp));
LL ans = 0;
for (int i = 0; i < len_a; i++)
{
if (b[0] == a[i])
{
dp[i][0] = 1;
ans = 1;
}
else if (i)
dp[i][0] = dp[i - 1][0];
}
for (int i = 0; i < len_b; i++)
{
if (a[0] == b[i])
{
dp[0][i] = 1;
ans = 1;
}
else if(i)
dp[0][i] = dp[0][i - 1];
}
for (int i = 1; i < len_a; i++)
{
for (int j = 1; j < len_b; j++)
{
if (a[i] == b[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
cout << dp[len_a - 1][len_b - 1] << endl;
}
}
HDOJ 1159 Common Subsequence【DP】的更多相关文章
- hdoj 1159 Common Subsequence【LCS】【DP】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence【dp+最长公共子序列】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...
- HDU 1159.Common Subsequence【动态规划DP】
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- hdu 1159 Common Subsequence 【LCS 基础入门】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDOJ --- 1159 Common Subsequence
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- HDU 1159 Common Subsequence (dp)
题目链接 Problem Description A subsequence of a given sequence is the given sequence with some elements ...
随机推荐
- WebAPI发布IIS报错问题
1.看IIS中处理程序映射中有没有注册:ExtensionlessUrlHandler-Integrated-4.0 没有的话需要在[运行]中注册:aspnet_regiis.exe 2.配置文件中要 ...
- 那些年我读过的Blog(Ⅰ)
序 近期发现自己已经很久很久没有写过文章,其中包括公开的和非公开的Blog,于是自己去翻了以前很多关注的人的Blog,发现也已经有很大一部分没有更新了,遂有了本文,梳理一下自己那些年关注过并且现在已经 ...
- 利用jsonrpc技术包装uiautomator
昨天一天在网上搜索解决上一篇文章中的exception: monkeyrunner内置uiautomator出错的原因 尽管没找到解决办法.可是让我无意中发现了一个好工具,比sl4a更好用的工具.直接 ...
- (转)使用.NET Reflector 查看Unity引擎里面的DLL文件
当你查看unity里面API的时候,是不是有时候追踪了一两步就碰到DLL文件走不下去了呢?很是不爽吧. 这种问题我也是经常碰到.这是人家商业引擎不想让你看到底层代码啦,所以着急不得. 不过,今天我终于 ...
- 【分享】DevDocs API Documentation
http://devdocs.io/ 这是一份综合性的在线API列表,很全,方便查找.
- django用户认证系统——重置密码7
当用户不小心忘记了密码时,网站需要提供让用户找回账户密码的功能.在示例项目中,我们将发送一封含有重置用户密码链接的邮件到用户注册时的邮箱,用户点击收到的链接就可以重置他的密码,下面是具体做法. 发送邮 ...
- Setting up PhoneGap on Ubuntu for Android app development
This is just some notes I made, mostly taken from http://docs.phonegap.com/en/3.0.0/guide_overview_i ...
- linux 环境 tomcat 莫名奇妙挂掉
::-exec-] org.apache.coyote.http11.Http11Processor.service Error processing request java.lang.NullPo ...
- 56、LeakCanary——直白的展现Android中的内存泄露
转载:http://blog.csdn.net/watermusicyes/article/details/46333925 DEMO下载地址:https://github.com/SOFTPOWER ...
- 5秒后跳转到另一个页面的js代码
今天看视频学习时学习了一种新技术,即平时我们在一个页面点击“提交”或“确认”会自动跳转到一个页面. 在网上搜了一下,关于这个技术处理有多种方法,我只记下我在视频里学到的三种: 1.用一个respons ...