Common Subsequence

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, x ij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, 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 the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

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 题解出处:http://blog.csdn.net/a_eagle/article/details/7213236

题目大意:给出两个字符串,求两个字符串的最长公共字串。

思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例。abcfbc abfcab。

辅助空间变化示意图

可以看出:

F[i][j]=F[i-1][j-1]+1;(a[i]==b[j])

F[i][j]=max(F[i-1][j],F[i][j-1])(a[i]!=b[j]);

n由于F(i,j)只和F(i-1,j-1), F(i-1,j)和F(i,j-1)有关, 而在计算F(i,j)时, 只要选择一个合适的顺序, 就可以保证这三项都已经计算出来了, 这样就可以计算出F(i,j). 这样一直推到f(len(a),len(b))就得到所要求的解了.
 
ps:本题求不连续LCS,注释部分为连续LCS。
#include<stdio.h>
#include<string.h> int f[][];
char s1[],s2[]; int max(int x,int y)
{
return x>y?x:y;
} int main()
{
int n,i,j;
while(~scanf("%s %s",s1,s2)){
memset(f,,sizeof(f));
//int maxx=0;
for(i=;i<=strlen(s1);i++){
for(j=;j<=strlen(s2);j++){
if(s1[i-]==s2[j-]){
f[i][j]=f[i-][j-]+;
//if(f[i][j]>maxx) maxx=f[i][j];
}
//不加else
else f[i][j]=max(f[i-][j],f[i][j-]);
}
}
//printf("%d\n",maxx);
printf("%d\n",f[strlen(s1)][strlen(s2)]);
}
return ;
}

POJ - 1458 Common Subsequence DP最长公共子序列(LCS)的更多相关文章

  1. POJ 1458 Common Subsequence 【最长公共子序列】

    解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序 ...

  2. POJ 1458 Common Subsequence(最长公共子序列)

    题目链接Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description A subsequence o ...

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

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

  4. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  5. hdu 1159 Common Subsequence(最长公共子序列,DP)

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

  6. hdu 1159 Common Subsequence (最长公共子序列 +代码)

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  7. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  8. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  9. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

随机推荐

  1. formData.append("username", "Groucho"); input 文件大小

    formData.append("username", "Groucho"); https://developer.mozilla.org/en-US/docs ...

  2. exception_action

    for i in range(3, -2, -1): try: print(4 / i) except Exception as e: print(Exception) print(e)

  3. apache 301重定向到带www的二级域名

    Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^nlike.cn [nc] rewriterule ^(.*)$ ...

  4. 我的Android进阶之旅------>自己写个Activity来调节Android系统背光亮度Brightness

    今天终于算初步写好了一个调节系统背光亮度Brightness的代码,本来不看Android源代码以为可以直接调用某个Action来启动系统的那个调节Brightness的对话框,但是看了代码后发现系统 ...

  5. ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

    mysql 删除表时提示有外键 mysql> drop tables auth_group;ERROR 1217 (23000): Cannot delete or update a paren ...

  6. nvl()与regexp_replace()

    NVL(字段,0):将空值转化为0 regexp_replace(字段, ‘[1-9]‘ ,'') is not null; 将数字转化为0

  7. 20170325 ABAP调用webservice

    转自:http://www.cnblogs.com/SolisOculus/archive/2013/04/01/2993198.html 在ABAP中调用Webservice     1.创建Pro ...

  8. PAT 天梯赛 L2-028. 秀恩爱分得快 【数据处理】

    题目链接 https://www.patest.cn/contests/gplt/L2-028 思路 0.只处理被询问的情侣的亲密度,否则会超时 1.要注意输入数字要用字符串,还要标记性别 因为 输出 ...

  9. SDUT OJ I样(0-1背包问题 【模板】)

    I样 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 这是个什么问题呢?DP,贪心,数据结构,图论,数论还是计算几何?管他呢,反正 ...

  10. POJ 2348 Euclid Game (模拟题)

    Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7942   Accepted: 3227 Des ...