题目链接
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: Accepted:

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


中文题目:

给出两个字符串,求出这样一个最长的公共子序列的长度——子序列的每个字符都能在两个原串中找到,且每个字符的先后顺序和原串中的先后顺序一致。

解题思路:

步骤1-找子问题:将原问题可以分解为求s1左边i个字符的子串和s2左边j个字符子串的最长公共子序列。

步骤2-确定状态:MaxLen(i,j)表示上述最长公共子序列的长度,即为本题的状态。

步骤3-确定状态转移方程:

  • MaxLen(n,0)=0, MaxLen(0,m)=0 (n=0,1,2...len1, m=1,2...len2)
  • if(s1[i-1]==s2[j-1]) MaxLen(i,j) = MaxLen(i-1,j-1)+1;
  • else MaxLen(i,j) = Max(MaxLen(i,j-1), ManLen(i-1,j));

重点在于状态转移方程的书写,这一题讲义PPT中画的图很好,言简意赅,我一开始想的是计算s2中以xk为终点的字串在s1中的公共子序列,但是发现自己对题意的理解有误,子串中的各字母是可以隔开的,因此逐个字符比较是最好的。既然是逐个字符相比较,那么自然也要考虑s1的位置。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; char s1[];
char s2[];
int maxLen[][]; int main()
{
while (cin >> s1 >> s2)
{
int length1 = strlen(s1);
int length2 = strlen(s2);
for (int i = ; i <= length1; i++)
maxLen[i][] = ;
for (int j = ; j <= length2; j++)
maxLen[][j] = ;
for (int i = ; i <= length1; i++)
{
for (int j = ; j <= length2; j++)
{
if (s1[i - ] == s2[j - ])
maxLen[i][j] = maxLen[i - ][j - ] + ;
else
maxLen[i][j] = max(maxLen[i - ][j], maxLen[i][j - ]);
}
}
cout << maxLen[length1][length2] << endl;
}
return ;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N = ;
char s1[N], s2[N];
int l1, l2;
int dp[N][N]; int DP()
{
memset(dp, , sizeof(dp));
for (int i = ; i <= l1; i++)
{
for (int j = ; j <= l2; j++)
{
if (s1[i-] == s2[j-])dp[i][j] = dp[i - ][j - ] + ;
else dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
}
}
return dp[l1][l2];
} int main()
{
while (scanf("%s%s", s1, s2) != EOF)
{
l1 = strlen(s1);
l2 = strlen(s2);
printf("%d\n", DP());
}
//system("pause");
return ;
}

二刷

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

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

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

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

    题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...

  3. POJ 1458 Common Subsequence 最长公共子序列 LCS

    LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> ...

  4. PKU 1458 Common Subsequence(最长公共子序列,dp,简单)

    题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> ...

  5. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  6. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  7. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  8. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

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

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

  10. LCS修改版(Longest Common Subsequence 最长公共子序列)

    题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉 ...

随机推荐

  1. 为什么需要 Redis 哨兵?

    在说哨兵之前,我们先说下主从复制,Redis 的主从复制模式,一旦主节点出现故障无法提供服务,需要人工介入手工将从节点调整为主节点,同时应用端还需要修改新的主节点地址,这种故障转移的方式对于很多应用场 ...

  2. MySQL 日期时间相关函数

    第一部分:时间差函数  timestampdiff.datediff.timediff 一.时间差函数:timestampdiff 语法:timestampdiff(interval, datetim ...

  3. C++中指针形参问题

    1.C++指针做形参,会有很多陷阱,很多时候也许并不如我们想的那样.比如我们想通过一个函数改变指针的值: #include<</SPAN>iostream> using nam ...

  4. scylladb docker-compose 用户密码认证配置

    scylladb 对于用户的认证配置还是比较简单的,以下是一个docker-compose 配置的说明 环境准备 docker-compose 文件 version: "3" se ...

  5. [RN] React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页头部 效果

    React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页效果 效果如下: 一.安装依赖 npm install react-native- ...

  6. C语言博客作业—2019-指针

    0.展示PTA总分 1.本章学习总结 1.1学习内容总结 指针做循环变量:即将指针作为循环变量,在指针移动到某一个位置的时候,达到了循环结束的条件,循环结束. for (p = a; p <= ...

  7. GoCN每日新闻(2019-10-14)

    GoCN每日新闻(2019-10-14) 1. 基于 Go 开源项目 MIMIO 的对象存储方案在探探的实践 https://mp.weixin.qq.com/s/YIKB_qAqqy6ydtFT_a ...

  8. 004 vue组件

    一:创建组件 1.第一种创建方式 主要有Vue.extend,Vue.component. 注释掉的代码是一步一步的推断,后面的代码是简化的代码. <!DOCTYPE html> < ...

  9. 企业架构 Red Hat Drools KIE Project 三大核心产品

    美团放弃Drools自研规则引擎: https://blog.csdn.net/qq_18603599/article/details/80767912 Drools rule engine虽然好,但 ...

  10. [转]白话HTTP短连接中的Session和Token

    我经常想象并怀念三十年前那原始而美好的互联网旧时光, 工作很轻松, 生活很悠闲. 上班的时候偶尔有些HTTP的请求发到我这里, 我简单的看一下, 取出相对应的html文档,图片,发回去就可以了, 然后 ...