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. idea打包可执行文件

    背景: 有时候,我们会用IDEA来开发一些小工具,需要打成可运行的JAR 包:或者某些项目不是WEB应用,纯粹是后台应用,发布时,也需要打成可运行的JAR包.并且,如果依赖第三方jar时,又不希望第三 ...

  2. Go 语言简介(下)— 特性

    希望你看到这篇文章的时候还是在公交车和地铁上正在上下班的时间,我希望我的这篇文章可以让你利用这段时间了解一门语言.当然,希望你不会因为看我的文章而错过站.呵呵. 如果你还不了解Go语言的语法,还请你移 ...

  3. poj 2585 Window Pains 解题报告

    Window Pains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2027   Accepted: 1025 Desc ...

  4. WIDGET和鼠标特效的DEMO

    原创WIDGET和鼠标特效的DEMO, 键盘1 平移Widget键盘2 旋转Widget键盘3 缩放Widget DEMO中,实现对BOX的旋转缩放位移 下载地址: http://pan.baidu. ...

  5. 解决IP地址冲突

    1.重新启动路由器就可以. 要是网络上的每一个设备都被分配了动态IP地址,路由器重新启动.又一次分配IP地址给网络上的每一个设备后,这个问题就有望得到解决. 可是假设是在企业内就不可能随便的重新启动公 ...

  6. CI框架入门中的简单MVC样例

    最简单的CI模型: 注意:模型须要用到数据库 配置文件在appcation/config.php 这里我们要用到数据库,须要将databases.php中的 相关參数填写一下,详细不再赘述. 直接进入 ...

  7. Android -- onWindowFocusChanged

    Android中获取手机屏幕的高度和宽度,我们知道在onCreate方法中获取到的值都是为0的,有人说可以在onClick方法中获取值,这个也是个方法 ,但在onWindowFocusChanged方 ...

  8. 安装Oracle之后解决掉的问题分享

    TNS-03505: 无法解析名称                                                            在测试tnsping的时候始终显示这么个问题. ...

  9. A12_ListView & ExpandablelistView

    一.ListView 效果: 1.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/a ...

  10. 为什么空格拷贝到linux 会变成两个

    为什么空格拷贝到linux 会变成两个 学习了:https://zhidao.baidu.com/question/266438357.html 在vi界面内输入:set paste 然后进行拷贝: ...