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. dp和px转换

    在编写自定义view的时候,通常会在onTouchEvent回调方法中进行一些关乎距离的判断逻辑,这里的距离常量如果适配到多分辨率的不同设备上时可能会出现一些错乱的问题. 所以一般来说,常常需要dp到 ...

  2. 0505--鲜花售卖网之“NABCD模型”

    一.NABCD 1) N (Need 需求)--(分析人:梁植淋) --简介 我们的鲜花售卖系统主要是给需要买花或者是养花的顾客提供一个购花平台,买花时通过位置识别给用户提供附近的花店购花,花店接到订 ...

  3. 团队作业--Beta版本冲刺

    项目冲刺随笔 第一天 第二天 第三天 第四天 第五天 第六天 第七天

  4. Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]

    WARNING: Failed to register in JMX: javax.naming.NamingException: Could not load resource factory cl ...

  5. 在Windows和UNIX下利用PHP和LDAP进行身份验证

    我现在的老板曾要求我为企业内部的Web服务提供一种标准的身份验证方法.我遇到的一个主要问题就是我们公司主要使用了两种平台:UNIX和.所以,我的第一个想法并不很成功:它要求每个员工都使用UNIX或者L ...

  6. Tarjan算法

    SCC即强连通分量,即一个图的子图,其中的点能相互到达,全称是strongly connected component. Tarjan算法是用来找出图的SCC. 伪代码 int index = 0; ...

  7. GPUImage学习

    1.GLProgram--加载vertex和fragment的shader. 好处是完全将shader模块的加载过程独立出来. 学习:每个函数处理一件事,且函数的粒度刚好 在glLinkProgram ...

  8. Java编程思想学习(十三) java I/O

    Java中使用流来处理程序的输入和输出操作,流是一个抽象的概念,封装了程序数据于输入输出设备交换的底层细节.JavaIO中又将流分为字节流和字符流,字节流主要用于处理诸如图像,音频视频等二进制格式数据 ...

  9. poj 3463 最短路与次短路&&统计个数

    题意:求最短路和比最短路长度多1的次短路的个数 本来想图(有)方(模)便(版)用spfa的,结果妹纸要我看看dijkstra怎么解.... 写了三遍orz Ver1.0:堆优化+邻接表,WA //不能 ...

  10. 洛谷P1661 扩散

    题目描述 一个点每过一个单位时间就会向四个方向扩散一个距离,如图. 两个点a.b连通,记作e(a,b),当且仅当a.b的扩散区域有公共部分.连通块的定义是块内的任意两个点u.v都必定存在路径e(u,a ...