描述

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的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. hdu 1159:Common Subsequence(动态规划)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  4. HDU 1159 Common Subsequence (动态规划、最长公共子序列)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. 【POJ - 1458】Common Subsequence(动态规划)

    Common Subsequence Descriptions: A subsequence of a given sequence is the given sequence with some e ...

  6. POJ 1458 Common Subsequence (动态规划)

    题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...

  7. HDU 1159.Common Subsequence【动态规划DP】

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

  8. 算法:Common Subsequence(动态规划 Java 最长子序列)

    Description A subsequence of a given sequence is the given sequence with some elements (possible non ...

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

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

随机推荐

  1. android sdutio常用快捷键

    快捷键 说明 F2 定位到高亮错误或警告的位置 F4 若选中项目,打开 Project Struture F5 复制文件 Alt+F3 选中文本,逐个往下查找相同文本,并高亮显示 Alt+F1 可以将 ...

  2. 03 CheckBox 复选框

    五  CheckBox  复选框     >概念:可以从一个集合选项中选择一个或者多个选项     >属性:checked   选择状态     >使用:           > ...

  3. java设计模式---状态模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述状态(State)模式的: 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为 ...

  4. Intent的Component,Action和Category属性详解-android学习之旅(五十)

    Component属性 代码示例 public class MainActivity extends Activity{ @Override protected void onCreate(Bundl ...

  5. 自己动手写hibernate

    这篇文章 可作为北京尚学堂 hibernate的学习笔记 再学习hibernate之前 得有一点反射的基础知识 package com.bjsxt.hibernate; public class St ...

  6. Android源码浅析(三)——Android AOSP 5.1.1源码的同步sync和编译make,搭建Samba服务器进行更便捷的烧录刷机

    Android源码浅析(三)--Android AOSP 5.1.1源码的同步sync和编译make,搭建Samba服务器进行更便捷的烧录刷机 最近比较忙,而且又要维护自己的博客,视频和公众号,也就没 ...

  7. Python 编程常见问题

    Python 编程常见问题 经常使用Python编程,把经常遇到问题在这里记录一下,省得到网上查找,因此这篇文章会持续更新,需要的可以Mark一下.进入正题: 1.Python常用的文件头声明 #!/ ...

  8. linux 下启动java jar包 shell

    linux 下启动java jar包 shell #!/bin/sh JAVA_HOME=/usr/local/jdk1.6.0_34/bin/javaJAVA_OPTS="-Xmx256m ...

  9. PYTHON风格规范-Google 开源项目风格指南

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  10. 断言(Assertion)需要注意的一个地方

    因为断言只在debug构建中有效,所以它是中关重要的去避免运行任何代码或调用任何方法在断言条件中,而这些代码或方法会影响程序的状态. 否则程序的行为将在debug和release构建中变得不一致,这显 ...