1.链接地址:

http://poj.org/problem?id=1458

http://bailian.openjudge.cn/practice/1458/

2.题目:

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35411   Accepted: 14080

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.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

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

3.思路:

4.代码:

 #include "stdio.h"

 //#include "stdlib.h"

 #include "string.h"

 #define N 1000

 char a[N],b[N];

 int c[N+][N+];

 int dp(int lena,int lenb)

 {

     int i,j;

     for(i=;i<=lena;i++) {c[i][]=;}

     for(j=;j<=lenb;j++) {c[][j]=;}

     for(i=;i<=lena;i++)

     {

        for(j=;j<=lenb;j++)

        {

            if(a[i-] == b[j-]) {c[i][j]=c[i-][j-]+;}

            //if(i==1 && j==1){printf("%c %c\n",a[0],b[0]);}

            else { c[i][j]=(c[i][j-]>c[i-][j])?c[i][j-]:c[i-][j]; }

            //printf("%c %c %d\n",a[i-1],b[j-1],c[i][j]);

        }

     }

     return c[lena][lenb];

 }

 int main()

 {

     int i,j;

     while(scanf("%s%s",a,b) != EOF)

     {

        printf("%d\n",dp(strlen(a),strlen(b)));

        //for(i=0;i<strlen(a);i++){for(j=0;j<strlen(b);j++){printf("%d ",c[i][j]);}printf("\n");}

     }

     //system("pause");

     return ;

 }

OpenJudge/Poj 1458 Common Subsequence的更多相关文章

  1. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  2. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  3. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  4. POJ 1458 Common Subsequence (动态规划)

    题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...

  5. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  6. poj 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46387   Accepted: 19 ...

  7. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  8. (线性dp,LCS) POJ 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65333   Accepted: 27 ...

  9. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

随机推荐

  1. innobackupex 单脚本循环7天一全备6增备脚本更新

    #!/bin/bash #日期转为天数 function date2days { echo "$*" | awk '{ z=-$)/); y=$+-z; m=$+*z-; j=*m ...

  2. hbase运维

    NoSQL现在风生水起,hbase的使用也越来越广,但目前几乎所有的NoSQL产品在运维上都没法和DB相提并论,在这篇blog中来总结下我们在运维hbase时的一些问题以及解决的方法,也希望得到更多h ...

  3. 用CSS让网页背景图片居中的方法

    网页背景居中的方法有很多种的.这里介绍一些用CSS让背景图片居中的方法. 让背景图片居中的第一个方法是用像素设定,很多都用这种,但是也是最麻烦的: <div style="width: ...

  4. jquery回车提交表单

    jquery回车提交表单,比较方便的功能. 三个事件keydown,keypress,keyup,分别是按下,按着没上抬,上抬键盘 ,所以用keyup. $(document).keyup(funct ...

  5. MySQL使用hugepage

    http://blog.csdn.net/dba_waterbin/article/details/9669929http://www.cnblogs.com/LMySQL/p/4689868.htm ...

  6. IE jquery mouseenter,mouseover超奇葩问题

    做了个项目,结构很简单 <div class="index-main" data-url="./img/index_default.jpg"> &l ...

  7. JS 笔记

    如何定义一个函数呢?基本语法如下: function 函数名() {      函数代码; } 说明: 1. function定义函数的关键字. 2. "函数名"你为函数取的名字. ...

  8. iOS语言国际化

    参考网站:http://blog.sina.com.cn/s/blog_7b9d64af0101jncz.html   语言国际化:根据系统不同的语言自动切换 Xcode6.2   一.在不同语言下工 ...

  9. 深入理解计算机系统第二版习题解答CSAPP 2.18

    将32位补码表示的数转换为10进制数. 32位补码 十进制 0x1b8 0x14 0xFFFFFE58 -424 0xFFFFFE74 -396 0x44 0xFFFFFEC8 -312 0x10 0 ...

  10. 关于快速排序的Java代码实现

    快速排序(Quicksort)是对冒泡排序的一种改进.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别 ...