题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18201    Accepted Submission(s): 7697

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
 

解题思路:

开一个二维数组f[N][M]记录第一个数组取前N个数和第二个数组取前M个数的时候公共子序列的最大长度,最后输出f[l1][l2]即为最后结果。

 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char keng1[],keng2[];
int con[][];
int getmax(int a,int b)
{if(a>b)return a;else return b;}
int main()
{
int i,j,l1,l2,Max;
keng1[]=keng2[]='#';
while(scanf("%s%s",keng1+,keng2+)!=EOF)
{
Max=;
l1=strlen(keng1);
l2=strlen(keng2);
memset(con,,sizeof(con));
for(i=;i<l1;i++)
{
for(j=;j<l2;j++)
{
if(keng1[i]==keng2[j])
con[i][j]=getmax(con[i][j],con[i-][j-]+);
else
con[i][j]=getmax(con[i-][j],con[i][j-]);
}
}
printf("%d\n",con[l1-][l2-]);
}
return ;
}
 
 

HDU 1159 Common Subsequence 公共子序列 DP 水题重温的更多相关文章

  1. HDU 1159.Common Subsequence【动态规划DP】

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  2. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  3. HDU 1159 Common Subsequence

    HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...

  4. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  5. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  6. HDU 1159 Common Subsequence(裸LCS)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  7. HDU 1159 Common Subsequence【dp+最长公共子序列】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1159 Common Subsequence --- DP入门之最长公共子序列

    题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...

  9. hdu 1159 Common Subsequence(最长公共子序列,DP)

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

随机推荐

  1. docker中搭建jenkins环境

    想在docker中搭建一个jenkins环境.开始的时候我想在先pull centos,然后在里面自己搭建环境,搭建后的环境可以运行,但是不知道挂载哪的数据,这也是我不熟悉jenkins的原因. 同事 ...

  2. JS 页面加载触发事件 document.ready和onload的区别(转)

    原博地址:http://blog.163.com/zhaoyanping_1125/blog/static/20132915320111129113723710/ * document.ready和o ...

  3. ~/.vimrc config

    runtime! debian.vim "设置编码 set encoding=utf- set fencs=utf-,ucs-bom,shift-jis,gb18030,gbk,gb2312 ...

  4. 使用 windows 计划任务播放音乐文件

    这个问题网上可以搜到很多答案,但都有一些小细节没有交代,而我平时又很少使用计划任务,所以配置中出了点问题,特此备注. 1.播放器 检查当前系统下目标文件的默认播放器是什么,并且确保可以运行. 比如首次 ...

  5. c# .net使用SqlDataReader注意的几点

    转自:http://blog.knowsky.com/258608.htm 1.当SqlDataReader没有关闭之前,数据库连接会一直保持open状态,所以在使用SqlDataReader时,使用 ...

  6. iOS9 升级设置

    今天升级了iOS9, Xcode7.1 ; 打开之前的工程发现网络请求出错了, 参照UM开发文档, 对info.plist进行了配置如下: 1. 以iOS9 SDK编译的工程会默认以SSL安全协议进行 ...

  7. bzoj 3052: [wc2013]糖果公园 带修改莫队

    3052: [wc2013]糖果公园 Time Limit: 250 Sec  Memory Limit: 512 MBSubmit: 506  Solved: 189[Submit][Status] ...

  8. javascript mvc

    http://www.webjx.com/javascript/jsajax-15996.html http://blog.csdn.net/jjfat/article/details/7963608 ...

  9. 关于键盘冲突那点事(3键冲突/7键冲突/PS2/USB的各种原理)

    转自关于键盘冲突那点事(3键冲突/7键冲突/PS2/USB的各种原理) 最近闲得无聊,正好看到有人发帖提问,于是就来详细说说所谓键位冲突和无冲突的各种原理--基本上这也是个老生常谈的话题了,但相关的技 ...

  10. NODE.JS玩玩

    按一个网页的来,最好最后能到EXPRESS.JS. http://www.nodebeginner.org/index-zh-cn.html 这样就能对比DJANGO,看看两者的WEB框架,加深认识. ...