/*Common Subsequence
Time Limit: 1000MS

Memory Limit: 10000K
Total Submissions: 56416

Accepted: 23516
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, 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*/
题意:求两个字符串的最大公共子序列。
公共子序列:顺序相同的序列,不一定是紧挨着,但是顺序一定相同。
假定我们有如下两个序列
S1: 1 2 3 4 5 6
S2: 4 5 6 7 8 9
S1和S2有一个最长公共子序列为 4 5 6一个子序列不一定必须是连续的,即中间可以被其他字符分开,但它们的顺序必须正确的。
最长公共子序列不一定只有一个。
S1: h e l l oS2: l e o nS1和S2有一个最长公共子序列为 eo
源程序如下:

 #include<stdio.h>
#include<string.h>
#define maxn 1005
int n,m;//定义s1与s2的实际长度
int dp[maxn][maxn];//把当前子序列的长度存入
char s1[maxn],s2[maxn];//两个字符串
int main()
{int i,j;
    while(scanf("%s%s",s1,s2)==)
    {
        n=strlen(s1);//s1串长度
        m=strlen(s2);//s2串长度
        for( i=;i<=n;i++)//初始化dp
            for( j=;j<=m;j++)
                dp[i][j]=;
    for( i=;i<=n;i++)//dp数组从1,1开始利用
        for( j=;j<=m;j++)
        {
            if(s1[i-]==s2[j-])/*如果s1的第i-1个字符与s2的第j-1个字符相同,
   那么s1[i-1]与s[j-1]的最长子序列是s[i-2]与s[j-2]的最长子序列加一*/
                dp[i][j]=dp[i-][j-]+;
            else/*如果s1的第i-1个字符与s2的第j-1个字符不相同,
   那么s1[i-1]与s[j-1]的最长子序列是s[i-1]与s[j-2]的最长子序列
   或s[i-2]与s[j-1]的最长子序列之中最大的*/
                if(dp[i-][j]>=dp[i][j-])
                 dp[i][j]=dp[i-][j];
                 else
                 dp[i][j]=dp[i][j-];
        }//循环递归,最后的dp[n][m]一定是s1[n],s2[m]最大的子序列长度
        printf("%d\n",dp[n][m]); 
    }
    return ;
}

要点:

poj1458公共子序列 C语言的更多相关文章

  1. 最长公共子序列LCS(POJ1458)

    转载自:https://www.cnblogs.com/huashanqingzhu/p/7423745.html 题目链接:http://poj.org/problem?id=1458 题目大意:给 ...

  2. 最长公共子序列(POJ1458)

    题目链接:http://poj.org/problem?id=1458 题目大意:给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和 ...

  3. POJ-1458(LCS:最长公共子序列模板题)

    Common Subsequence POJ-1458 //最长公共子序列问题 #include<iostream> #include<algorithm> #include& ...

  4. poj1458 求最长公共子序列 经典DP

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45763   Accepted: 18 ...

  5. 最长公共子序列(POJ1458)

    给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致. Sample Input: abcfbc abfcabpr ...

  6. POJ-1458.CommonSubsequence.(DP:最长公共子序列裸题)

    本题大意:给出两个字符串,让你求出最长公共子序列的长度并输出. 本题思路:本题是经典的DP问题,由于是两个字符串,那么我们就用一个二维数组来进行区分,用dp[ i ][ j ]来表示在s1和s2中分别 ...

  7. C语言 · 最长公共子序列 · 最长字符序列

    算法提高篇有两个此类题目: 算法提高 最长字符序列   时间限制:1.0s   内存限制:256.0MB      最长字符序列 问题描述 设x(i), y(i), z(i)表示单个字符,则X={x( ...

  8. POJ-1458 Common Subsequence(线性动规,最长公共子序列问题)

    Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44464 Accepted: 18186 ...

  9. POJ1458 最长公共子序列

    描述: 给定两个字符串,求其最长公共子序列(不用连续), 输入: abc bcc programning content 输出: 2 2 解法: 动态规划. 定义dp[i][j]表示s1到i索引,以及 ...

随机推荐

  1. Reactive Extensions (Rx) 入门(2) —— 安装 Reactive Extensions

    译文:https://blog.csdn.net/fangxing80/article/details/7581937 原文:http://www.atmarkit.co.jp/fdotnet/int ...

  2. 【Wannafly挑战赛24E】旅行

    [Wannafly挑战赛24E]旅行 题面 牛客 题解 首先有一个非常显然的\(dp\):我们直接把\(s\rightarrow t\)的路径抠出来然后设\(f_{i,j}\)表示到第\(i\)个点, ...

  3. LOJ3058. 「HNOI2019」白兔之舞 [DP,MTT]

    LOJ 前置知识:任意长度NTT 普通NTT只能做\(2^k\)的循环卷积,尝试扩展成长度为\(n\)的循环卷积,保证模意义下\(\omega_n\)存在. 不管怎样还是要算点值.推式子: \[ \b ...

  4. Note_3.31

    2019/4/1 奇奇怪怪的笔记 整理了一些之前没有写过的东西,把它们拼在一起,并没有什么逻辑可言qwq FWT快速沃尔什变换 \[ FWT(A)=merge(FWT(A0),FWT(A0+A1)) ...

  5. js操作表格、table、

    js添加一行.删除一行 let str="<tr>" +"<td>"+a[1]+"</td>" +&qu ...

  6. vue-router踩坑日记Unknown custom element router-view

    今天笔者在研究vue-router的时候踩到了一个小坑,这个坑是这样的 笔者的具体代码如下:router.js import Home from '@/components/Home.vue'; im ...

  7. 基于redis5的session共享:【redis 5.x集群应用研究】

    基于springsession构建一个session共享的模块. 这里,基于redis的集群(Redis-5.0.3版本),为了解决整个物联网平台的各个子系统之间共享session需求,且方便各个子系 ...

  8. 使用bugly热更新时自定义升级弹窗的UI样式

    项目的热更新用的bugly,不过一直都只是使用他自带的升级弹窗. 不过UI小姐姐说弹窗太丑了,要自定义. bugly有提供自定义UI的官方文档:https://bugly.qq.com/docs/us ...

  9. Ubuntu16.04安装CDH

    官方参考文档: https://www.cloudera.com/documentation/enterprise/5-15-x/topics/configure_cm_repo.html 安装cdh ...

  10. bladex开发自己的服务不推送服务器的方法

    一:问题 使用代码生成器 生成的代码,运行后,需要推送至服务器才可以进行调试,每次推送,启动服务至少半个小时以上,相当浪费时间,如何可以让开发的服务不推送至服务器能调试呢? 二:尝试解决 直接开发机运 ...