hdu 1159 Common Subsequence 【LCS 基础入门】
链接:
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17621 Accepted Submission(s): 7401
..., 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.
abcfbc abfcab
programming contest
abcd mnp
4
2
0
题意:
分析:
/**
求长度为 len1 的序列 A 和长度为 len2 的序列 B 的LCS
注意:序列下标从 0 开始
*/
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
else
{
int m1 = dp[i-1][j];
int m2 = dp[i][j-1];
dp[i][j] = max(m1, m2);
}
}
}
}
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
}
code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 500+10;
int dp[maxn][maxn];
char s1[maxn], s2[maxn]; /**
求长度为 len1 的序列 A 和长度为 len2 的序列 B 的LCS
注意:序列下标从 0 开始
*/
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
else
{
int m1 = dp[i-1][j];
int m2 = dp[i][j-1];
dp[i][j] = max(m1, m2);
}
}
}
} int main()
{
while(scanf("%s%s", s1,s2) != EOF)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
memset(dp,0,sizeof(dp)); LCS(len1, len2);
printf("%d\n", dp[len1][len2]);
}
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 500+10;
int dp[2][maxn];
char s1[maxn], s2[maxn]; void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
} int main()
{
while(scanf("%s%s", s1,s2) != EOF)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
memset(dp,0,sizeof(dp)); LCS(len1, len2);
printf("%d\n", dp[len1%2][len2]);
}
}
hdu 1159 Common Subsequence 【LCS 基础入门】的更多相关文章
- hdu 1159 Common Subsequence(LCS最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence (LCS)
题意:给定两行字符串,求最长公共子序列. 析:dp[i][j] 表示第一串以 i 个结尾和第二个串以 j 个结尾,最长公共子序列,剩下的就简单了. 代码如下: #pragma comment(link ...
- 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(裸LCS)
传送门: 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(最长公共子序列 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 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...
随机推荐
- Cocos2d-x开发---关于安卓打包所遇到的错误记录
非常久都没有在安卓打过包了.之前的项目因为某些问题没有考虑做安卓版本号,所以涉及到安卓打包的时候都是自己在折腾. 这段时间离职了,空余时间就有非常多了.所以我能够折腾点事了.想起来 ...
- spring mvc接收参数方式,json格式返回请求数据
1 使用方法形参使用变量接收提交的数据 2 在方法的形参中使用模型接收数据 3 如果在提交的表单中有多个数据模型,需要创建一个新的Bean,里面的属性是要接收的对象变量. 4 接收提交的日期字符串,转 ...
- unity, StopAllCoroutines导致bug的解决办法
StopAllCoroutines有时候不用不行. 但只要一用,就可能导致无穷无尽的bug. 原因是StopAllCoroutines会将当前脚本中所有coroutines都停掉,而没法做到只停掉我们 ...
- Hadoop-2.4.0分布式安装手冊
文件夹 文件夹 1 1. 前言 2 2. 部署 2 2.1. 机器列表 2 2.2. 主机名 2 2.2.1. 暂时改动主机名 3 2.2.2. 永久改动主机名 3 2.3. 免password登录范 ...
- [svc]几种访问google方案
最近老被人问起,有什么访问谷歌的方法可以推荐. 针对小白用户(使用sass式即可) iass sass pass区别 小白可以用(无需安装软件,些许收费):googlegae: https://m.2 ...
- mahout相关介绍
https://blog.csdn.net/xiaopihaierletian/article/details/72674592 https://www.cnblogs.com/zlslch/p/67 ...
- maven下载、配置和安装
Maven简介 #Apache Maven is a software project management and comprehension tool. Based on the concept ...
- 写一个php小脚本辅助渗透测试
因为一个注入要爬行一些数据,然后写的一个小脚本,能写脚本来辅助渗透,也算是里程碑.哈哈哈 <?php $num = 1; while ($num <= 39) { $web_url = & ...
- hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护
/** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...
- php-fpm 如果dm设置为 static,那么其实只有pm.max_children这个参数生效。系统会开启设置数量的php-fpm进程。
php-fpm未优化网友反映的问题 1.最近将Wordpress迁移至阿里云.由于自己的服务器是云服务器,硬盘和内存都比较小,所以内存经常不够使,通过ps ax命令查看后,发现启动php-fpm进程数 ...