Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 24489    Accepted Submission(s): 10823

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
 
#include<stdio.h>
#include<string>
#include<vector>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
string a,b;
while(cin>>a>>b)
{
vector<vector<int> > c;
int x=a.size();
int y=b.size();
int SIZE=x>y?x:y;
SIZE+=1;
c.resize(SIZE);
for(int i=0;i<SIZE;++i)
c[i].resize(SIZE,0);
for(int i=0;i<=x;++i)
c[i][0]=0;
for(int i=0;i<=y;++i)
c[0][i]=0;
for(int i=1;i<=x;++i)
for(int j=1;j<=y;++j)
{
if(a[i-1]==b[j-1])
{
c[i][j]=c[i-1][j-1]+1;
}
else
{
c[i][j]=c[i][j-1]>c[i-1][j]?c[i][j-1]:c[i-1][j];
}
}
cout<<c[x][y]<<endl; }
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

hdu 1159 Common Subsequence (dp乞讨LCS)的更多相关文章

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

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

  2. HDU 1159 Common Subsequence

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

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

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

  4. HDU 1159 Common Subsequence(裸LCS)

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

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

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

  6. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

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

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

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

  8. hdu 1159 Common Subsequence 【LCS 基础入门】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...

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

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

随机推荐

  1. Callable 获取线程返回值

    allable与 Future 两功能是Java在兴许版本号中为了适应多并法才增加的,Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它 ...

  2. android 泰国/缅甸语/捷克较低,Contacts联系出现精神错乱之类的问题清单

    更改ContactsProvider2.java文件 public static final String SECTION_HEADING = "SUBSTR(%1$s,1,1)" ...

  3. WPF 3D: MeshGeometry3D纹理坐标的正确定义

    原文 WPF 3D: MeshGeometry3D纹理坐标的正确定义 为了使基于2D的纹理显示在3D对象中,我们必须定义3D Mesh对象的纹理贴图坐标.在WPF中,此项功能则通过MeshGeomet ...

  4. Ewebeditor最新漏洞和漏洞指数

    Ewebeditor最新漏洞和漏洞指数[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...

  5. [原创] linux 下上传 datapoint数据到yeelink 【golang版本】同时上传2个数据点

    /* Create by sndnvaps<sndnvaps@gmail.com> * data: 2015-04-12* upload 2 datapoint to yeelink.ne ...

  6. BZOJ 1150 CTSC2007 数据备份Backup 堆+馋

    标题效果:给定一个长度n−1n-1的序列,要求选出kk个不相邻的数使得和最小 费用流显然能跑.并且显然过不去- - 考虑用堆模拟费用流 一个错误的贪心是每次取最小.这样显然过不去例子 我们把[每次取最 ...

  7. 解析DBR操作系统引导记录数据

    理解文件系统.你必须要熟悉DBR,下面我们就来看看文件系统解析DBR数据. Dos Boot Record(DBR)操作系统引导记录是由操作系统的格式化程序建立的.在文件系统驱动操作不论什么一个磁盘卷 ...

  8. asp.net访问网络路径方法(模拟用户登录)

    public class IdentityScope : IDisposable { // obtains user token [DllImport("advapi32.dll" ...

  9. Java 的布局管理器GridBagLayout的使用方法(转)

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...

  10. StringUtils.isNumeric(String str) 的一个坑(转)

    在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...