HDU 1159 Common Subsequence (dp)
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
分析:
给出两个序列S1和S2,求出的这两个序列的最大公共部分S3就是就是S1和S2的最长公共子序列了。公共部分
必须是以相同的顺序出现,但是不必要是连续的。
1、字符相同,则指向左上,并加1
2、字符不同,则指向左边或者上边较大的那个
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char ch1[1000],ch2[1000];
int dp[1000][1000];
int k1,k2;
void zhixul()
{
int i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=k1; i++)
{
for(j = 1; j<=k2; j++)
{
if(ch1[i-1] == ch2[j-1])///字符相同,则指向左上,并加1
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);///字符不同,比较左边和上边那个大取最大
}
}
}
int main()
{
while(~scanf("%s%s",ch1,ch2))
{
k1 = strlen(ch1);
k2 = strlen(ch2);
zhixul();
printf("%d\n",dp[k1][k2]);
}
return 0;
}
HDU 1159 Common Subsequence (dp)的更多相关文章
- HDU 1159——Common Subsequence(DP)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题解 #include<iostream> #include<cstring> ...
- hdu 1159 Common Subsequence (dp乞讨LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1159:Common Subsequence(动态规划)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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(最长公共子序列)
题目链接: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 (最长公共子序列 +代码)
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 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...
- HDU 1159 Common Subsequence (动态规划、最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- erlang+thrift配合开发
I think, thrift is a tcp/ip based Client-Server architecture multi-languages supported RPC framewo ...
- 【Docker 命令】- create命令
docker create :创建一个新的容器但不启动它 语法 docker create [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS同run命令 实例 使用 ...
- (转)linux IO 内核参数调优 之 参数调节和场景分析
1. pdflush刷新脏数据条件 (linux IO 内核参数调优 之 原理和参数介绍)上一章节讲述了IO内核调优介个重要参数参数. 总结可知cached中的脏数据满足如下几个条件中一个或者多个的时 ...
- Dojo初探
Dojo 是一个由 Dojo 基金会开发的 Javascript 工具包, 据说受到 IBM 的永久支持,其包括四个部分: dojo, dijit, dojox, util dojo: 有时也被称作 ...
- 使用tc来控制网络流量
https://blog.csdn.net/qinyushuang/article/details/46611709 tc实际操控网络的流量 解释网络tc的架构,从架构上分析tc,与netfilter ...
- 如何设计好的RESTful API之安全性
保证RESTful API的安全性,主要包括三大方面: a) 对客户端做身份认证 b) 对敏感的数据做加密,并且防止篡改 c) 身份认证之后的授权 1.对客户端做身份认证,有几种常见的做法: 1)在请 ...
- jquery animate() stop() finish() 方法使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【bzoj1705】[Usaco2007 Nov]Telephone Wire 架设电话线 dp
题目描述 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= N < ...
- 在Linux上编译TCMalloc
TCMalloc(Thread-Caching Malloc)与标准glibc库的malloc实现一样的功能,但是TCMalloc在效率和速度效率都比标准malloc高很多.TCMalloc是goog ...
- [JSOI2010]部落划分 最小生成树
一道最小生成树经典题 由于是最靠近的两个部落尽可能远,如果我们先处理出任意两个居住点之间的距离并将其当做边,那么我们可以发现,因为在一个部落里面的边是不用计入答案的,所以应该要尽量把小边放在一个部落里 ...