动态规划 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, 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.
输入
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.
输出
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.
样例输入
abcfbc abfcab
programming contest
abcd mnp
样例输出
4
2
0
一道非常简单的动态规划题,菜鸡小白正在研究之中
#include <iostream>
#include <algorithm>
#include <string>
#define N 1001
#include <cstring>
using namespace std;
int a[N][N];
int main()
{
int n, m, j, k, i;
string x, y;
while (cin >> x >> y)
{
n = x.length();
m = y.length();
for (i = ; i < n; i++)
{
for (j = ; j < m; j++)
{
a[i][j] = ;
}
}
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
if (x[i-] == y[j-])
a[i][j] = a[i - ][j - ] + ;
else if (a[i - ][j] > a[i][j - ])
a[i][j] = a[i - ][j];
else a[i][j] = a[i][j - ];
}
}
cout << a[n][m] << endl;
}
}
动态规划 Common Subsequence的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- hdu 1159:Common Subsequence(动态规划)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence 动态规划
2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...
- HDU 1159 Common Subsequence (动态规划、最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【POJ - 1458】Common Subsequence(动态规划)
Common Subsequence Descriptions: A subsequence of a given sequence is the given sequence with some e ...
- POJ 1458 Common Subsequence (动态规划)
题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...
- HDU 1159.Common Subsequence【动态规划DP】
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- 算法:Common Subsequence(动态规划 Java 最长子序列)
Description A subsequence of a given sequence is the given sequence with some elements (possible non ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
随机推荐
- (NO.00004)iOS实现打砖块游戏(十三):伸缩自如,我是如意金箍棒(下)!
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 准备缩短反弹棒素材 和上一篇类似,我们如法炮制一张缩短后反弹棒的 ...
- 套接字输入缓冲装置——InternalInputBuffer
互联网的世界很复杂,信息从一端传向另一端过程也相当复杂,中间可能通过若干个硬件,为了提高发送和接收效率,在发送端及接收端都将引入缓冲区,所以两端的套接字都拥有各自的缓冲区,当然这种缓冲区的引入也带来了 ...
- 基于java自身技术实现消息方式的系统间通信
这篇博客基本照搬了分布式java应用基础与实践一书的内容 java自带的远程调用分两种一种是rmi,一种是webservice 我们先看rmi(remote method invoke)# 使用rmi ...
- 方便使用FFMPEG的经验
FFMPEG是命令行工具,因此使用起来多少还是会有些不方便.在这记录两点方便使用FFMPEG的方法: 1.任何目录下都可以使用FFMPEG 问题描述:需要转码(播放)的时候,需要把ffmpeg.exe ...
- Libgdx 1.5.2发布
[1.5.2] - 修复问题 #2433 - 修复LWJGL在Mac OS X的本地载入问题 [1.5.1] - Gradle 升级到 2.2 - Android Gradle 工具升级到 1.0.0 ...
- 手把手带你画一个漂亮蜂窝view Android自定义view
上一篇做了一个水波纹view 不知道大家有没有动手试试呢点击打开链接 这个效果做起来好像没什么意义,如果不加监听回调 图片就能直接替代.写这篇博客的目的是锻炼一下思维能力,以更好的面多各种自定义vi ...
- 【翻译】Ext JS 5.0.1 中的新功能
原文:What's New in Ext JS 5.0.1 今天,我们很高兴的宣布Ext JS 5.0.1发布了!此维护版本基于Sencha社区的反馈做了一些改进.下面让我们来了解一下这些改变. 可访 ...
- OC语言(五)
三十七.SEL类型-方法的包装 发送消息其实就是发送SEL. 每个方法都有与之对应的SEL类型数据. 第一次调用方法,先把方法包装成为SEL数据,再根据SEL去找方法地址,最后根据方法地址调用相应的方 ...
- 苹果新的编程语言 Swift 语言进阶(八)--属性
属性是特定类.结构或枚举的相关值,属性根据作用域不同分为实例属性与类型属性,还可以根据是否存储分为存储属性和计算属性. 1.1 实例属性 为一个类.结构或枚举定义的属性默认属于实例属性,即该属性属于为 ...
- linux开发调试工具---GDB的使用
编译 gcc -g xxx.c -o xxxgdb gdb xxxgdb 进入gdb调试界面 查看代码 list ==> l 设置断点 可以以函数为断点 / 行数 break ...