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 ...
随机推荐
- jsp连接MYSQL数据库教程(文字+图)
步骤: 1.在mysql官网下载JDBC驱动程序.网址:https://dev.mysql.com/downloads/connector/j/ 2.把里面的jar包(mysql-connector- ...
- #Leetcode# 700. Search in a Binary Search Tree
https://leetcode.com/problems/search-in-a-binary-search-tree/ Given the root node of a binary search ...
- Thinkphp5的使用phpmailer实现发邮件功能(163邮箱)
Thinkphp5本身并没有实现发邮件的功能,至少据我所知. 本文利用网易邮箱作为发邮件的邮箱.作为发送邮件的前提是需要开启SMTP服务,打开网易邮件,点击设置按钮,如下图所示 勾选smtp服务 保存 ...
- Apriori算法详解
一.Apriori 算法概述Apriori 算法是一种最有影响力的挖掘布尔关联规则的频繁项集的 算法,它是由Rakesh Agrawal 和RamakrishnanSkrikant 提出的.它使用一种 ...
- PHPcms企业黄页中,会员注册之后提示经营模式不得少于1个字符 的解决办法
后台--模块- 黄页模块 --- 企业库 --- 字段 --- 经营模式 --- 字符长度取值范围 1 改为 0.
- timer实现
实现一个 timer 前段时间写过一篇 blog 谈到 用 timer 驱动游戏 的一个想法.当 timer 被大量使用之后,似乎自己实现一个 timer 比用系统提供的要放心一些.最近在重构以前的代 ...
- Java问题排查工具单
前言 平时的工作中经常碰到很多疑难问题的处理,在解决问题的同时,有一些工具起到了相当大的作用,在此书写下来,一是作为笔记,可以让自己后续忘记了可快速翻阅,二是分享,希望看到此文的同学们可以拿出自己日常 ...
- android应用打前需要准备些啥?
发布之前我们需要准备的东西 参考了google官方和结合国内开发经验 1) 用户协议(本地.网络.API) 2) 签名文件(用于为APP加密,唯一标签) 3) 程序图标(第一个面对用户,准备不同的但合 ...
- Django+haystack实现全文搜索出现错误 ImportError: cannot import name signals
原因是在你的settings.py或者其他地方使用了 "import haystack" 当我们使用django-haysatck库时,表面上会有haystack库,但实际上并不 ...
- 后缀自动机SAM学习笔记
前言(2019.1.6) 已经是二周目了呢... 之前还是有一些东西没有理解到位 重新写一下吧 后缀自动机的一些基本概念 参考资料和例子 from hihocoder DZYO神仙翻译的神仙论文 简而 ...