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 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...
随机推荐
- CentOS添加新网卡network-scripts目录下找不到网卡配置文件
问题描述: 使用VMware Workstation虚拟机,安装好CentOS7虚拟机后(原本只有一张网卡ifcfg-ens33),重新添加了一个新的网卡. 进入CentOS7系统后,使用ip add ...
- A successful Git branching model/GIT分支管理是一门艺术
英文原文:http://www.nvie.com/posts/a-successful-git-branching-model/ 原文作者:Vincent Driessen 本文经Linux大棚博主总 ...
- 问题解决: Pandas and scikit-learn: KeyError: […] not in index
https://stackoverflow.com/questions/51091132/pandas-and-scikit-learn-keyerror-not-in-index The probl ...
- html5-canvas绘图操作方法
<script>function draw(){ var c=document.getElementById("mycanvas"); c.width=50 ...
- 【转载】Oracle数据字典详解
转自:http://czmmiao.iteye.com/blog/1258462 Oracle数据字典概述 数据库是数据的集合,数据库维护和管理这用户的数据,那么这些用户数据表都存在哪里,用户的信息是 ...
- python之读取Excel 文件
# -*- coding: utf-8 -*- """ Created on Thu May 24 13:53:10 2018 @author: Frank " ...
- Xampp + Zend Studio + xDebug 环境搭建 (Mac,Windows都适用)
这几天折腾了一下PHP开发环境的搭建,现总结一下安装步骤: 1. 安装 Zend Studio,然后破解. 2. 安装 Xampp 3. 配置 Xampp 3.1 配置 Apache服务端 ...
- java模拟从http上下载文件
1.依赖 Apache httpclient 包. 2.代码 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = ...
- Android WebView 笔记
WebView(网络视图)能载入显示载入网页.将网页内容载入到手机client,它使用了WebKit渲染引擎载入显示网页,实现WebView有下面方法: 首先要实如今手机client显示必需要求注冊一 ...
- 利用JMeter的beanshell进行接口的加密处理
最近项目中在做http协议的接口测试,其中接口请求报文数据有个字段值需要用到加密后的签名,即出于网络传输过程中,对数据安全的考虑,要对请求的数据进行特定的处理(加密),再进行请求. 刚开始由于项目赶进 ...