HDU 1159 Common Subsequence(POJ 1458)
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34819 Accepted Submission(s): 15901
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
Source
Southeastern Europe 2003
解析:最长公共子序列。
```
#include
#include
#include
using namespace std;
char s1[500], s2[500];
int dp[500][500];
int lcs()
{
int m = strlen(s1), n = strlen(s2);
for(int i = 0; i <= m; ++i)
dp[i][0] = 0;
for(int i = 0; i <= n; ++i)
dp[0][i] = 0;
for(int i = 1; i <= m; ++i){
for(int j = 1; j <= n; ++j){
if(s1[i-1] == s2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[m][n];
}
int main()
{
while(~scanf("%s%s", s1, s2)){
int res = lcs();
printf("%d\n", res);
}
return 0;
}
<br>
因为进行状态转移的时候,只与前一个状态有关,所以可以用[滚动数组](http://blog.csdn.net/insistgogo/article/details/8581215)进行优化来减少所需存储空间。
include
include
include
using namespace std;
char s1[500], s2[500];
int dp[2][500];
int lcs()
{
int m = strlen(s1), n = strlen(s2);
memset(dp, 0, sizeof dp);
for(int i = 1; i <= m; ++i){
for(int j = 1; j <= n; ++j){
if(s1[i-1] == s2[j-1])
dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
dp[i%2][j] = max(dp[i%2][j-1], dp[(i-1)%2][j]);
}
}
return dp[m%2][n];
}
int main()
{
while(~scanf("%s%s", s1, s2)){
int res = lcs();
printf("%d\n", res);
}
return 0;
}
HDU 1159 Common Subsequence(POJ 1458)的更多相关文章
- HDU 1159 Common Subsequence
HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- HDU 1159 Common Subsequence 公共子序列 DP 水题重温
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- hdu 1159 Common Subsequence(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence(裸LCS)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence【dp+最长公共子序列】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- (最长公共子序列 暴力) Common Subsequence (poj 1458)
http://poj.org/problem?id=1458 Description A subsequence of a given sequence is the given sequence w ...
- hdu 1159 Common Subsequence 【LCS 基础入门】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...
随机推荐
- java - day08 - PrimeNumLoop
质数循环查找 package day07_addition; //范围查找质数 public class PrimeNumLoop { public static void main(String[] ...
- 我的第九个java程序--spring和mybatis整合(java project)
思路:入口程序读spring的配置文件-配置文件注入给程序bean--程序拿到bean以操作对象的手法查出程序 入口程序HelloWorld.java package HelloWorld; impo ...
- Spring MVC简单URL处理程序映射
以下示例显示如何使用Spring Web MVC框架来实现一个简单URL处理程序映射. SimpleUrlHandlerMapping类分别显式地将URL映射到相应的控制器上. 所下所示配置 - &l ...
- Struts2 结果和结果类型
正如前面提到的,<results>标签在Struts2的MVC框架的视图中所扮演的角色.动作是负责执行业务逻辑.执行业务逻辑后,接下来的步骤是使用<results>标签显示的视 ...
- 自定义实现wcf的用户名密码验证
目前wcf分为[传输层安全][消息层安全]两种,本身也自带的用户名密码验证的功能,但是ms为了防止用户名密码明文在网络上传输,所以,强制要求一旦使用[用户名密码]校验功能,则必须使用证书,按照常理讲, ...
- POI Excel 冷冻线
冷冻线 Sheet.createFreezePane data bar and color scale SheetConditionalFormatting scf = sheet.getSheet ...
- python3 - 生成器genarator
在Python中,这种一边循环一边计算的机制,称为生成器:generator. 生成器保存的是算法,每次调用 next() ,就计算出 下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出 ...
- unison+inotify 同步web代码并排除指定目录不同步
unison + inotify 实现web 数据双向同步 unison 是一款跨平台的文件同步对象,不仅支撑本地对本地同步,也支持通过SSH,RSH和Socket 等网络协议进行同步.unis ...
- hdu 1853 (费用流 拆点)
// 给定一个有向图,必须用若干个环来覆盖整个图,要求这些覆盖的环的权值最小. 思路:原图每个点 u 拆为 u 和 u' ,从源点引容量为 1 费用为 0 的边到 u ,从 u' 引相同性质的边到汇点 ...
- 【BZOJ3043】IncDec Sequence 乱搞
[BZOJ3043]IncDec Sequence Description 给定一个长度为n的数列{a1,a2...an},每次可以选择一个区间[l,r],使这个区间内的数都加一或者都减一.问至少需要 ...