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. storm如何保证at least once语义?

    背景 前期收到的问题: 1.在Topology中我们可以指定spout.bolt的并行度,在提交Topology时Storm如何将spout.bolt自动发布到每个服务器并且控制服务的CPU.磁盘等资 ...

  2. 使用spring cloud实现分布式配置管理

    <7天学会spring cloud系列>之创建配置管理服务器及实现分布式配置管理应用. 本文涉及到的项目: 开源项目:http://git.oschina.net/zhou666/spri ...

  3. c# r3 inline hook

    前言 老婆喜欢在QQ游戏玩拖拉机,且安装了一个记牌器小软件,打开的时候弹出几个IE页面加载很多广告,于是叫我去掉广告.想想可以用OD进行nop填充,也可以写api hook替换shellexecute ...

  4. State Pattern -- 状态模式原理及实现(C++)

    主要参考<大话设计模式>和<设计模式:可复用面向对象软件的基础>两本书.本文介绍命令模式的实现. 问题出发点 在实际开发中,我们经常会遇到这种情况:一个对象有多种状态,在每一个 ...

  5. 记录nginx 搭建集群环境踏过的坑

    因为生产环境做了负载均衡,为了尽可能跟线上环境一致,所以想在本地也搭集群,这样测试更靠谱.首先就想到nginx 这货,搭个简单的web 服务器实在太简单了. nginx 这玩意11年简单玩过,那是版本 ...

  6. asp.net网站安全常见问题与防范

    1:SQL 注入 2:XSS 3:CSRF 4:文件上传 1:SQL 注入 引起原因: 其实现在很多网站中都存在这种问题.就是程序中直接进行SQL语句拼接.可能有些读者不太明白. 下面通过一个登录时对 ...

  7. 使用TinkPHP实现品字形布局

    一.后台管理模版 后台管理模版通常使用frameset/iframe来布局.例如: <!DOCTYPE html> <html> <head> <title& ...

  8. session的一个问题

    <%@ page language="java" import="java.util.*,javax.servlet.http.Cookie.*" pag ...

  9. OC基础--成员变量的封装

    一.封装的作用: 1.重用 2.不必关心具体的实现 3.面向对象三大特征之一 4.具有安全性 二.OC中成员变量的命名规范以及注意事项 1.命名规范--.成员变量都以下划线“_”开头 1)为了跟get ...

  10. C基础--结构体

    C语言,结构体语法: 1.定义结构体类型: struct 结构体名称 { 成员类型 成员名称1; 成员类型 成员名称2; ... }; 例:struct Date { int year ; int m ...