Common Subsequence

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

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
 
Source
简单的lcs
lcs 模板:
int lcs(const char *a,const char *b)
{
int i,j;
int m=strlen(a),n=strlen(b);
mar[][]=;
for(i=;i<=m;i++)
mar[i][]=;
for(i=;i<=n;i++)
mar[][i]=;
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
if(a[i-]==b[j-])
mar[i][j]=mar[i-][j-]+;
else
mar[i][j]=mar[i-][j]>mar[i][j-]?mar[i-][j]:mar[i][j-];
}
}
return mar[m][n];
}

此题的代码:

 #include<stdio.h>
#include<string.h>
#define maxn 1000
int mar[maxn][maxn];
char x[maxn],y[maxn]; int lcs(const char *a,const char *b)
{
int i,j;
int m=strlen(a),n=strlen(b);
mar[][]=;
for(i=;i<=m;i++)
mar[i][]=;
for(i=;i<=n;i++)
mar[][i]=;
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
if(a[i-]==b[j-])
mar[i][j]=mar[i-][j-]+;
else
mar[i][j]=mar[i-][j]>mar[i][j-]?mar[i-][j]:mar[i][j-];
}
}
return mar[m][n];
}
int main()
{
while(scanf("%s%s",&x,&y)!=EOF)
printf("%d\n",lcs(x,y));
return ;
}

HDUOJ--1159Common Subsequence的更多相关文章

  1. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  2. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  3. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  4. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  5. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  6. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  9. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. elimination-game

    https://leetcode.com/problems/elimination-game/ // 一行代码就可以,不过原理有些复杂 // https://discuss.leetcode.com/ ...

  2. iOS开发-UITableView常用方法

    UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewData ...

  3. 直播【95秀】JNI 基本实现 简洁

    2017-2-8 基本架构 1.使用SurfaceView在UI层面展示视频 2.通过JNI调用C代码控制视频的播放.停止 基本功能 1.从服务器获取正在直播的主播的列表信息 2.进入直播界面 3.可 ...

  4. IntPtr 转 string

    假设有 intPtr pBuffer 方法一: 直接使用Marshal.PtrToStringAnsi方法: string ss = Marshal.PtrToStringAnsi(pBuffer); ...

  5. JavaScript使用技巧精萃

    (一).确认删除用法:   1. BtnDel.Attributes.Add("onclick","return confirm('"+"确认删除?& ...

  6. IOS中Key-Value Coding (KVC)的使用详解

    kvc,键值编码,是一个非正式的协议,它提供一种机制来间接访问对象的属性.直接访问对象是通过调用访问器的方法实现,而kvc不需要调用访问器的设置和获取方法,可以直接访问对象的属性. 下面介绍一下kvc ...

  7. [Backbone] Customzing Backbone

    Convert the AppointmentForm view below to use Mustache templating. Make sure you remember to change ...

  8. 如何使用FF的Firebug组件中的net工具查看页面元素加载消耗时间

    1.安装FF的Firebug组件:点击FF的Tools的Add-ons菜单,输入Firebug关键字,并选择合适的版本Install. 2.安装完毕后地址栏右边会出现一个小虫图标,右边还有一个下拉箭头 ...

  9. 将War发布到Tomcat7上遇到的问题及其解决

    用MyEclipse做了一个app,在其自带的Tomcat里运行正常,做成war后却出现如下错误: [ServletException in:/page/jsp/template/block.jsp] ...

  10. (剑指Offer)面试题61:按之字形顺序打印二叉树

    题目: 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 思路: 按照广度优先遍历来遍历二叉树,但是需要 ...