动态规划 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 称为已 ...
随机推荐
- Android面试题总结
1.Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚拟 ...
- Ubuntu 15.10下的WebStorm-11.0.3完美破解
由于最新的JetBrains 发布了最新版本的IntelliJ IDEA的各个版本,而且更换了注册机的使用方式,这就导致了之前对WebStorm的破解方法不能在使用了.所以我们就必须另寻他法咯.如题, ...
- 多态原理探究-从C++编译器角度理解多态的实现原理
理论知识: 当类中声明虚函数时,编译器会在类中生成一个虚函数表. 虚函数表是一个存储类成员函数指针的数据结构. 虚函数表是由编译器自动生成与维护的. virtual成员函数会被编译器放入虚函数表中. ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- Dynamics CRM2013 ScLib::AccessCheckEx failed
今天在系统中做某一操作的时候报如下截图错误,把错误日志下载下来,根据AccessRights这:ReadAccess一提示确定是对某一实体没有读的权限. 那怎样知道是哪个实体呢,再看上面错误日志中给出 ...
- Tag功能介绍—我们为什么打Tag?
想必CSDN的新老用户在访问CSDN网站的博客.社区.下载等服务时,经常能够看到"请您添加标签"的提示.也许很多人对此抱有疑问:加标签有什么用?在这里我们为您集中解答一下疑惑. T ...
- Cocos2D旋转炮塔到指定角度(一)
原文地址:Rotating Turrets: How To Make A Simple iPhone Game with Cocos2D 2.X Part 2 翻译有节选和删除. 在你旋转炮塔之前,首 ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十七)
现在玩家选择机器人后,可以在屏幕上或手臂上点击来移动robot's arm了. 但是玩家选择一个机器人后没有视觉效果来表明哪个机器人被选中.玩家做了一个操作后没有视觉反馈会惹恼强迫症用户滴 ;) 这篇 ...
- ssh连接原理介绍( 无密码连接登录的原理)
SSH(Secure Shell)一种在不安全网络上提供安全远程登录及其它安全网络服务的协议.由客户端和服务端的软件组成的,有两个不兼容的版本分别是:1.x和2.x.(SSH 2.x的客户程序是不能 ...
- 基于Struts+Hibernate开发过程中遇到的错误
1.import javax.servlet.http.HttpServletRequest 导入包出错 导入包出错,通常是包未引入,HttpServletRequest包是浏览器通过http发出的 ...