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. openssl实践总结

    openssl实验总结 OPENSSL简介 OpenSSL项目是一个协作开发一个健壮的,商业级的,全功能的,并且开放源代码工具包,它实现了安全套接字层(SSL v2/v3)和传输层安全(TLS v1) ...

  2. Orchard用LiveWriter写博客

    本文链接:http://www.cnblogs.com/souther/p/4544241.html Orchard本身提供一个内建的管理面板来写博客,许多人更喜欢采用客户端提交的方式,例如Windo ...

  3. Flask 开发全球化应用

    从C#世界迈入python总是令人有一种如释重负之感,同样的效果同样的功能,只需要付出1/10不到的代价,可能正是Python所倡导的简美哲学所带来的威力. 我还深深地记得在ASP.NET中做全球化的 ...

  4. iOS简易柱状图(带动画)--新手入门篇

    叨逼叨 好久没更新博客了,才几个月,发生了好多事情,处理了好多事情.不变的是写代码依然在继续. 做点啥子 看看objective-c的书,学着写了个柱状图,只是练习的demo而已,iOS上的图表控件已 ...

  5. IT技术的进化道路

    IT技术是人类发明的,同时也是给人类服务的,这项技术的进化,就是距离人越来越近,大体上是: 大型机时代: 离人类的距离超过100米. 小型机时代: 离人类的距离前进到10米. PC机时代: 离人类的距 ...

  6. 2、面向对象以及winform的简单运用(面向对象的四大基本特性)

    面向对象的四大基本特性 面向对象的核心概念就是封装.抽象.继承.多态这四大基本特性,在这里先解释一下它们的定义: 封装:是一种隐藏信息的特性.找到变化并且把它封装起来,你就可以在不影响其它部分的情况下 ...

  7. 第九课:js的类与继承

    因为本书是js的框架设计,因此观看本书的必须有js基础,看不懂,请不要觉得自己差.我也是看了5遍js高级程序设计,才能看懂这本书的. 有关js的构造函数,继承的方法大家可以去看js的高级程序设计,我这 ...

  8. Javascript基础系列之(八)Javascript的调试与优化

    Javascript的错误主要是语法错误和运行时的错误,前者在代码解析时就会出错,影响程序的运行.后者称为异常,影响它所运行的线程.下面就Javascript常见错误进行分析 1.常见的错误和异常 i ...

  9. Moqui学习Day2

    用户 本地化  消息和日志门面 用户门面用于管理当前用户和访问,登陆,授权及登出的信息.用户信息包括区域设置,时区以及币种/ec.user.nowTimestamp设置日期. 消息门面用于追踪用户的消 ...

  10. “耐撕”团队 2016.04.11 站立会议

    1. 时间 : 19:30--19:50. 共计20分钟. 2. 成员 : Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客: ...