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. 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset

    bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...

  2. CVPR14 图像检索papers

    CVPR14年关于图像检索方面的papers,汇总成一个list,方便阅读. 图像检索 Triangulation embedding and democratic aggregation for i ...

  3. Informatica 常用组件Lookup之六 查询

    PowerCenter 基于您在查找转换中配置的端口和属性来查询查找.当第一行输入到查找转换时,PowerCenter 运行一个默认的 SQL 语句.如果使用关系查找,您可以在"查找 SQL ...

  4. 基础学习:C#中float的取值范围和精度

    float类型的表现形式: 默认情况下,赋值运算符右侧的实数被视为 double. 因此,应使用后缀 f 或 F 初始化浮点型变量,如以下示例中所示: float x = 3.5F; 如果在以上声明中 ...

  5. 关于ARM的B,BL跳转指令

    .text:00001260 A3 04 00 EB                             BL      sub_24F4 当前地址+ (偏移 << 2 + 8) =  ...

  6. linux中sort命令

    功能说明:将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序. 参 数: -b 忽略每行前面开始出的空格字符. -c 检查文件是否已经按照顺序排序. -d 排序时,处理英文字母.数 ...

  7. spring data 自定义查询

    spring data 自定义查询 https://www.cnblogs.com/airycode/p/6535635.html 在方法接口上面使用@Query

  8. Cognos Report Studio 链接查询需要注意的地方2

    在Report Studio里面用SQL设计报表,查询2,查询3 要链接一般按条件  a1=b1 在选择链接方式需要注意的地方: 默认链接 外部链接 需要设置打开FM,打开报表设计引用的数据包(FM- ...

  9. 笔记本建立wifi热点的实用详细步骤

    准备工作: (1) 首先要打开开始菜单--->搜索“services.msc” 并打开(或者用win+R快捷键打开“运行”输入“service.msc”,点击确定)--->找到“WLAN ...

  10. Mongo读书笔记2 -- 数据类型

      Mongo有多种类型的collection, 默认的是随着document 数目的增多自动增大; 还有一些collection被称为capped collection, 只能包含固定数目的docu ...