Common Subsequence

Time Limit: 2 Sec  Memory Limit: 64 MB
Submit: 951  Solved: 374

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, f, c 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 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.The length of the string is less than 1000.

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
 #include<stdio.h>
#include<string.h>
#define Max( a, b ) (a) > (b) ? (a) : (b) char s1[], s2[]; int dp[][]; int main()
{
int len1, len2;
while( scanf( "%s %s", s1+, s2+ ) != EOF )
{
memset( dp, , sizeof(dp) );
len1 = strlen( s1+ ), len2 = strlen( s2+ );
for( int i = ; i <= len1; ++i )
{
for( int j = ; j <= len2; ++j )
{
if( s1[i] == s2[j] )
{
dp[i][j] = dp[i-][j-] + ;
}
else
{
dp[i][j] = Max ( dp[i-][j], dp[i][j-] );
}
}
}
printf( "%d\n", dp[len1][len2] );
}
return ;
}

AC

Common Subsequence(dp)的更多相关文章

  1. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  2. POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)

    题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Tot ...

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

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

  4. Longest Common Subsequence (DP)

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

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

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

  6. CF 346B. Lucky Common Subsequence(DP+KMP)

    这题确实很棒..又是无想法..其实是AC自动机+DP的感觉,但是只有一个串,用kmp就行了. dp[i][j][k],k代表前缀为virus[k]的状态,len表示其他所有状态串,处理出Ac[len] ...

  7. POJ 1458 Common Subsequence DP

    http://poj.org/problem?id=1458 用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS. 1.如果str[i] == sub[j],那么LCS长 ...

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

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

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

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

随机推荐

  1. 转 一篇关于sql server 三种恢复模式的文章,从sql server 的机制上来写的,感觉很不错,转了

    简介 SQL Server中的事务日志无疑是SQL Server中最重要的部分之一.因为SQL SERVER利用事务日志来确保持久性(Durability)和事务回滚(Rollback).从而还部分确 ...

  2. brew-cask之本地安装应用

    cask 固然好用,但是无奈很多资源在墙外,能下载的非常有限,就是能下载,也慢的要死.但是很多下载软件却可以下载这些资源,很奇怪,要么是有人FQ下载了,缓存到他们的服务器了,要么就是软件可以FQ下载. ...

  3. 定制类自己的的new_handler

    C++中的new操作符首先使用operator new函数来分配空间,然后再在此空间上调用类的构造函数构造对象.当operator new无法分配所需的内存空间时,默认的情况下会抛出一个bad_all ...

  4. Android--下拉框

    一. 实现效果图如下 Android 中的下拉框为Spinner 组件,其效果图如上图片 二. 实现代码 布局代码如下 <?xml version="1.0" encodin ...

  5. 模态窗口插件之Jbox

    $.jBox.tip("报损数量不能大于库存数!", 'error'); $.jBox.tip("请选择要报损的产品", "warn"); ...

  6. groovyConsole — the Groovy Swing console

    1. Groovy : Groovy Console The Groovy Swing Console allows a user to enter and run Groovy scripts. T ...

  7. c#批量插入示例

    var sql = @"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20131029153010;I ...

  8. GIthub的小技巧

    目录: 一.快捷键一览表 二.快速搜索项目文件功能 三.使用Github Pages搭建项目网站 一.快捷键一览表 具体操作:         在各个页面下按下shift+/也就是?都可以打开键盘快捷 ...

  9. 在windows 下使用git

    首先安装好在windows下的linux模拟交互器 这里我选择的是cygwin 这里我是参考:http://book.51cto.com/art/201107/278731.htm 这里还要注意我这里 ...

  10. 5.9-3 用正则表达式判断字符串text是否为合法的手机号

    package zfc; public class Zfc { public static void main(String[] args) { //判断手机号格式是否合法 String text = ...