动态规划 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 称为已 ...
随机推荐
- [django]项目打包构建
django项目的结构大体上都是类似,打包主要的功能就是把一些不需要部署的文件剔除,把需要部署的文件直接压缩打包. 这里还想集成一个配置文件模板生成配置文件的过程,或者写一个配置文件生成的工具,不用每 ...
- UE4类修饰符
官方文档链接: https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Reference/index.html ...
- Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)
主要介绍一下android的加速传感器的简单用法,模拟摇一摇 ,如果x,y,z三个方向的加速度超过了15,就会弹出Toast,当然你可以设置更复杂的策略,比如判断间隔 代码如下 public clas ...
- 一个简易版本的lua debugger实现
introduction 工欲善其事,必先利其器.lua作为一门动态语言,虽然我已经习惯了使用print来进行代码调试,但是还是有很多童鞋觉得一款好用的调试器能更好的进行lua代码编写.所以在以前接手 ...
- ssh连接原理介绍( 无密码连接登录的原理)
SSH(Secure Shell)一种在不安全网络上提供安全远程登录及其它安全网络服务的协议.由客户端和服务端的软件组成的,有两个不兼容的版本分别是:1.x和2.x.(SSH 2.x的客户程序是不能 ...
- json解析,异步下载(listview仅滑动时加载)Demo总结
异步加载的练习demo 主要涉及知识点: 1.解析json格式数据,主要包括图片,文本 2.使用AsynTask异步方式从网络下载图片 3.BaseAdapter的"优雅"使用 4 ...
- Leetcode_100_Same Tree
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42061529 Given two binary trees ...
- 采用JSP+JavaBean的方式进行简单的实现用户的网页登陆实例
我们都知道J2EE中的Model1开发模式,那么下面就让我们一起简单的进行一下回顾,其主要是体现了一个初步的分层的思想: jsp层,业务逻辑层,以及我们的数据库层,主要的作用分别为,jsp层负责与用户 ...
- CTBS问题百科
1.浏览CTBS网站时,Service Unavailable或应用程序池自动停止的现象 解决方法: 点击"开始"-"控制面板"-"管理工具" ...
- C语言生成32位和64位随机数算法
C语言生成32位和64位随机数算法 /** * randstd.h * * Standard definitions and types, Bob Jenkins * * 2015-01-19: re ...