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. MVC5 + EF6 + Bootstrap3 (10) 数据查询页面

    文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-search-page.html 系列教程:MVC ...

  2. PHP PC端接入支付宝和微信感悟

    想着中秋节的时候把异步线程学习完,同事说有个PHP的支付要帮忙做一下,虽然中秋节计划是把C#的异步学完,不过还是喜欢挑战,好久没有像大学一样这么认真的熬夜解决问题了.由于在大学学过asp,它和php有 ...

  3. Fiddler工具的基本功能

    Fiddler是一款用于网页数据分析,抓取的工具,里面集成了对网页强大的功能外,还可以通过设置,使其对手机的数据也可以进行抓取 Fiddler的原理是: 通过在客户端和服务器之间创建一个代理服务器来对 ...

  4. openwrt的路由器重置root密码

    家里路由器刷了openwrt,结果长期没登录,忘了root密码. 很容易就找到了这里介绍的办法  http://www.openwrt.org.cn/bbs/thread-12327-1-1.html ...

  5. Fluent NHibernate关系映射

    1.好处:Fluent NHibernate让你不再需要去写NHibernate的标准映射文件(.hbm.xml), 方便了我们的代码重构,提供了代码的易读性,并精简了项目代码 实现: (1).首先我 ...

  6. 19.C#逐一介绍IEnumerable和IEnumerable<T>中的扩展方法(10.3-10.5)

    今天没有太多的言语,只有代码,扩展方法多得太多,不能一一列完,书中一些,看多了也就会使用了. //Enumerable.Range 返回起始到结束范围,是一个Enumrable<int>类 ...

  7. 温故知新---重读C#InDepth(一)

    一本好书,或是一本比较有深度的书,就是每次研读的时候都会有新的发现. 好吧,我承认每次读的时候都有泛泛而过的嫌疑~~ 这几年一直专注于C#客户端的开发,逐步从迷迷糊糊,到一知半解,再到自以为是,最后沉 ...

  8. Bootstrap3.0学习第十三轮(导航条)

    详情请查看http://aehyok.com/Blog/Detail/20.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  9. AngularJs-指令1

    前言: 前面写的有些乱,并且有些罗嗦,以后会注意的.希望我写的文章能帮助大家. 1,什么是指令 简单的说,指令是angularjs在html页面中建立一套自己能识别的标签元素.属性.类和注释,用来达到 ...

  10. (转)在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持

    原文:http://www.cnblogs.com/lyongde/p/3745030.html 此文在原文的基础上改进了几个步骤,因为经本人实践,原文无法正确配置. WAMPServer可以让开发者 ...