题目链接
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. Python开发笔记:网络数据抓取

    网络数据获取(爬取)分为两部分: 1.抓取(抓取网页) · urlib内建模块,特别是urlib.request · Requests第三方库(中小型网络爬虫的开发) · Scrapy框架(大型网络爬 ...

  2. Python应用之-修改通讯录

    #-*- coding:utf-8 -*- import sqlite3 #打开本地数据库用于存储用户信息 conn = sqlite3.connect('mysql_person.db') #在该数 ...

  3. 实用的Python库

    一.Django 1.自动实现图片压缩: pip install easy-thumbnails / https://pypi.org/project/easy-thumbnails/2.实现定时任务 ...

  4. map_multimap

    #include<iostream> #include<string> #include<map> using namespace std; struct Self ...

  5. Git学习笔记--配置(二)

    由之前文章,总结得出Git的特点: 最优的存储能力: 非凡性能: 开源的: 管理成本低: 很容易做备份: 支持离线操作: 很容易定制工作流程: Git is a free and open sourc ...

  6. Java 锁(学习笔记)

    关于Java 锁的知识整理与回顾(个人笔记): 锁有哪些,分别用来干嘛? Java实现锁有两种方式,synchronized关键字和Lock (1)Lock(可判断锁状态) Lock是基于JDK层面实 ...

  7. (尚027)Vue_案例_交互添加

    TodoHeader.vue组件 写交互: 第一步:跟目标元素绑定监听 (1).按回车键确认@keyup.enter="add" (2). 注意:数据在哪个组件,更新数据的行为就应 ...

  8. python(三)——while语句

    while死循环 #!/usr/bin/env python #-*- coding:utf8 -*- import time while 1 == 1: print('Ok',time.time() ...

  9. pgloader 学习(四)一些简单操作例子

    上边已经说明了pgloader 的基本使用(篇理论),但是对于实际操作偏少,以下是一个简单的操作 不像官方文档那样,我为了方便,直接使用docker-compose 运行,同时这个环境,会在后边大部分 ...

  10. A revolutionary architecture for building a distributed graph

    转自:https://blog.apollographql.com/apollo-federation-f260cf525d21 What if you could access all of you ...