最长公共子序列(易理解)

动态规划 最长公共子序列 过程图解 1.基本概念       首先需要科普一下,最长公共子序列(longest common sequence)和最长公共子串(longest common substring)不是一回事儿。什么是子序列呢?即一个给定的序列的子序列,就是将给定序列中零个或多个元素去掉之后得到的结果。什么是子串呢?给定串中任意个连续的字符组成的子序列称为该串的子串。给一个图再解释一下:        如 阅读详情

最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:子串是串的一个连续的部分,子序列则是从不改变序列的顺序,而从序列中去掉任意的元素而获得新的序列;也就是说,子串中字符的位置必须是连续的,子序列则可以不必连续。

一: 作用

       最长公共子序列的问题常用于解决字符串的相似度,是一个非常实用的算法,作为码农,此算法是我们的必备基本功。

二:概念

     举个例子,cnblogs这个字符串中子序列有多少个呢?很显然有27个,比如其中的cb,cgs等等都是其子序列,我们可以看出

子序列不见得一定是连续的,连续的那是子串。

     我想大家已经了解了子序列的概念,那现在可以延伸到两个字符串了,那么大家能够看出:cnblogs和belong的公共子序列吗?

在你找出的公共子序列中,你能找出最长的公共子序列吗?

从图中我们看到了最长公共子序列为blog,仔细想想我们可以发现其实最长公共子序列的个数不是唯一的,可能会有两个以上,

但是长度一定是唯一的,比如这里的最长公共子序列的长度为4。

 

三:解决方案

<1> 枚举法

       这种方法是最简单,也是最容易想到的,当然时间复杂度也是龟速的,我们可以分析一下,刚才也说过了cnblogs的子序列

个数有27个 ,延伸一下:一个长度为N的字符串,其子序列有2N个,每个子序列要在第二个长度为N的字符串中去匹配,匹配一次

需要O(N)的时间,总共也就是O(N*2N),可以看出,时间复杂度为指数级,恐怖的令人窒息。

 

<2> 动态规划

      既然是经典的题目肯定是有优化空间的,并且解题方式是有固定流程的,这里我们采用的是矩阵实现,也就是二维数组。

第一步:先计算最长公共子序列的长度。

第二步:根据长度,然后通过回溯求出最长公共子序列。

现有两个序列X={x1,x2,x3,...xi},Y={y1,y2,y3,....,yi},

设一个C[i,j]: 保存Xi与Yj的LCS的长度。

递推方程为:

不知道大家看懂了没?动态规划的一个重要性质特点就是解决“子问题重叠”的场景,可以有效的避免重复计算,根据上面的

公式其实可以发现C[i,j]一直保存着当前(Xi,Yi)的最大子序列长度。

复制代码
 1 using System;
 2 namespace ConsoleApplication2
 3 {
 4     public class Program
 5     {
 6         static int[,] martix;
 7 
 8         static string str1 = "cnblogs";
 9         static string str2 = "belong";
10 
11         static void Main(string[] args)
12         {
13             martix = new int[str1.Length + 1, str2.Length + 1];
14 
15             LCS(str1, str2);
16 
17             //只要拿出矩阵最后一个位置的数字即可
18             Console.WriteLine("当前最大公共子序列的长度为:{0}", martix[str1.Length, str2.Length]);
19 
20             Console.Read();
21         }
22 
23         static void LCS(string str1, string str2)
24         {
25             //初始化边界,过滤掉0的情况
26             for (int i = 0; i <= str1.Length; i++)
27                 martix[i, 0] = 0;
28 
29             for (int j = 0; j <= str2.Length; j++)
30                 martix[0, j] = 0;
31 
32             //填充矩阵
33             for (int i = 1; i <= str1.Length; i++)
34             {
35                 for (int j = 1; j <= str2.Length; j++)
36                 {
37                     //相等的情况
38                     if (str1[i - 1] == str2[j - 1])
39                     {
40                         martix[i, j] = martix[i - 1, j - 1] + 1;
41                     }
42                     else
43                     {
44                         //比较“左边”和“上边“,根据其max来填充
45                         if (martix[i - 1, j] >= martix[i, j - 1])
46                             martix[i, j] = martix[i - 1, j];
47                         else
48                             martix[i, j] = martix[i, j - 1];
49                     }
50                 }
51             }
52         }
53     }
54 }
复制代码

图大家可以自己画一画,代码完全是根据上面的公式照搬过来的,长度的问题我们已经解决了,这次要解决输出最长子序列的问题,

我们采用一个标记函数Flag[i,j],当

①:C[i,j]=C[i-1,j-1]+1  时 标记Flag[i,j]="left_up";    (左上方箭头)

②:C[i-1,j]>=C[i,j-1]   时 标记Flag[i,j]="left";          (左箭头)

③: C[i-1,j]<C[i,j-1]     时 标记Flag[i,j]="up";            (上箭头)

 

例如:我输入两个序列X=acgbfhk,Y=cegefkh。

复制代码
 1 using System;
 2 
 3 namespace ConsoleApplication2
 4 {
 5     public class Program
 6     {
 7         static int[,] martix;
 8 
 9         static string[,] flag;
10 
11         static string str1 = "acgbfhk";
12 
13         static string str2 = "cegefkh";
14 
15         static void Main(string[] args)
16         {
17             martix = new int[str1.Length + 1, str2.Length + 1];
18 
19             flag = new string[str1.Length + 1, str2.Length + 1];
20 
21             LCS(str1, str2);
22 
23             //打印子序列
24             SubSequence(str1.Length, str2.Length);
25 
26             Console.Read();
27         }
28 
29         static void LCS(string str1, string str2)
30         {
31             //初始化边界,过滤掉0的情况
32             for (int i = 0; i <= str1.Length; i++)
33                 martix[i, 0] = 0;
34 
35             for (int j = 0; j <= str2.Length; j++)
36                 martix[0, j] = 0;
37 
38             //填充矩阵
39             for (int i = 1; i <= str1.Length; i++)
40             {
41                 for (int j = 1; j <= str2.Length; j++)
42                 {
43                     //相等的情况
44                     if (str1[i - 1] == str2[j - 1])
45                     {
46                         martix[i, j] = martix[i - 1, j - 1] + 1;
47                         flag[i, j] = "left_up";
48                     }
49                     else
50                     {
51                         //比较“左边”和“上边“,根据其max来填充
52                         if (martix[i - 1, j] >= martix[i, j - 1])
53                         {
54                             martix[i, j] = martix[i - 1, j];
55                             flag[i, j] = "left";
56                         }
57                         else
58                         {
59                             martix[i, j] = martix[i, j - 1];
60                             flag[i, j] = "up";
61                         }
62                     }
63                 }
64             }
65         }
66 
67         static void SubSequence(int i, int j)
68         {
69             if (i == 0 || j == 0)
70                 return;
71 
72             if (flag[i, j] == "left_up")
73             {
74                 Console.WriteLine("{0}: 当前坐标:({1},{2})", str2[j - 1], i - 1, j - 1);
75 
76                 //左前方
77                 SubSequence(i - 1, j - 1);
78             }
79             else
80             {
81                 if (flag[i, j] == "up")
82                 {
83                     SubSequence(i, j - 1);
84                 }
85                 else
86                 {
87                     SubSequence(i - 1, j);
88                 }
89             }
90         }
91     }
92 }
复制代码

由于直接绘图很麻烦,嘿嘿,我就用手机拍了张:

好,我们再输入两个字符串:

1         static string str1 = "abcbdab";
2 
3         static string str2 = "bdcaba";

通过上面的两张图,我们来分析下它的时间复杂度和空间复杂度。

时间复杂度:构建矩阵我们花费了O(MN)的时间,回溯时我们花费了O(M+N)的时间,两者相加最终我们花费了O(MN)的时间。

空间复杂度:构建矩阵我们花费了O(MN)的空间,标记函数也花费了O(MN)的空间,两者相加最终我们花费了O(MN)的空间。



最长公共子序列的长度的动态规划方程

    设有字符串a[0...n],b[0...m],下面就是递推公式。字符串a对应的是二维数组num的行,字符串b对应的是二维数组num的列。

 


最长公共子序列的长度的动态规划方程

    设有字符串a[0...n],b[0...m],下面就是递推公式。字符串a对应的是二维数组num的行,字符串b对应的是二维数组num的列。

 

详解最长公共子序列问题(三种方法) C++分别利用三种动态规划解决最长公共子序列问题 阅读详情

相关推荐

数据结构与算法之最长公共子序列

数据结构与算法之最长公共子序列

weixin_47225948的博客 1833

最长公共子序列

动态规划:最长公共子序列,代码及其填写dp表格详解

北渡的博客 1万+

算法:最长公共子序列

好久没做算法题了,现在发现自己的算法能力非常薄弱,所以特意练练,顺便做个笔记方便以后查看。 今天整理一下最长公共子序列,最长公共子序列的问题常用于解决字符串的相似度,是一个非常实用的算法,作为码农,此算法是我们的必备基本功。 一、最长公共子序列概念: 最长公共子序列,是指两个字符串可具有的长度最大的公共的子序列。听着好像有点绕口,举个例子吧: 上面标红的就是最长公共子序列,是不

gjnm820的博客 2764

最长公共子序列LCS(动态规划)—详解

一.基本概念 1、 子序列(subsequence): 一个特定序列的子序列就是将给定序列中零个或多个元素去掉后得到的结果(不改变元素间相对次序)。例如序列<A,B,C,B,D,A,B><A,B,C,B,D,A,B>的子序列有:<A,B><A,B>、<B,C,A><B,C,A>、<A,B,C,D,A><A,B...

weixin_44023658的博客 1万+

最长公共子序列(C++)

最长公共子序列(C++)

m0_59850318的博客 2143

保姆级教程:在RK3568上搞定PR2100K+GC2385双摄(从DTS配置到HAL层补丁)

本文详细介绍了在RK3568平台上调试PR2100K+GC2385双摄系统的完整流程,从DTS配置到HAL层补丁。涵盖硬件连接、媒体控制器拓扑验证、Camera3配置、HAL层特殊补丁及性能优化等关键步骤,帮助开发者快速解决双摄调试中的常见问题,提升嵌入式视觉系统开发效率。

weixin_28724661的博客 195

1143. 最长公共子序列

递归 画出递归状态树就很了然了,注意base是-1表示为空。 没啥毛病,但是超时了。。。 class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def helper(i, j): if i == -1 or j == -1: return 0 if text1[i] == text2[j]

annaya的博客 137

C# Log4Net不输出日志的解决方法

log4net是一个流行的 .NET 日志框架,用于记录应用程序的日志信息。如果你在使用log4net。

精通搬砖,资深技术砖家,在软件开发,人工智能,设备开发等领域都有研究。商业合作&交流学习可私信联系。 1263

最长公共子序列[转]

http://blog.youkuaiyun.com/yysdsyl/article/details/4226630 动态规划法 经常会遇到复杂问题不能简单地分解成几个子问题,而会分解出一系列的子问题。简单地采用把大问题分解成子问题,并综合子问题的解导出大问题的解的方法,问题求解耗时会按问题规模呈幂级数增加。 为了节约重复求相同子问题的时间,引入一个数组,不管它们是否对最终解有用,把所有

micx0124的专栏 315

告别繁琐同步:Zotero与Obsidian双向联动新方案(基于Better BibTeX与MarkDBConnect)

本文详细介绍了一套基于Better BibTeX、Bibnotes Formatter和MarkDBConnect插件的Zotero与Obsidian双向联动方案。该方案通过建立智能数据通道,实现文献信息的自动同步与笔记的增量更新,解决了手动同步的繁琐问题,并支持在两大工具间无缝跳转,极大提升了科研和知识管理的工作效率。

weixin_29213827的博客 642

DDR内存Prefetch机制详解:从DDR1到DDR4的技术演进与性能提升

本文深入解析DDR内存Prefetch机制从DDR1到DDR4的技术演进与性能提升。通过分析Prefetch技术的实现原理与Burst传输的协同机制,揭示DDR1的2n Prefetch、DDR2的4n Prefetch、DDR3的8n Prefetch以及DDR4的Bank Group架构创新如何显著提升内存带宽与系统性能。

weixin_30369041的博客 537

最长公共子序列问题 C++ (超详细)

最长公共子序列问题 C++ 问题描述 若给定序列X={x1,x2,…,xm},则另一序列Z={z1,z2,…,zk},是X的子序列是指存在一个严格递增下标序列{i1,i2,…,ik}使得对于所有j=1,2,…,k有:zj=xi。 例如,序列Z={B,C,D,B}是序列X={A,B,C,B,D,A,B}的子序列,相应的递增下标序列为{2,3,5,7}。 给定2个序列X和Y,当另一序列Z既是X的子序列又是Y的子序列时,称Z是序列X和Y的公共子序列。 给定2个序列X={x1,x2,…,xm}和Y={y1,y2,…

SYK000的博客 2万+

Verilog基本代码结构及常用语句always、begin...end解读

Verilog基本代码结构及常用语句always、begin...end解读 线性反馈以为寄存器(LFSR)

weixin_62380586的博客 1万+

最长公共子序列C++实现

最长公共子串,C++实现,附带思路解析

haust_允谦的博客 5550

Linux文件操作四剑客

① /^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日 yyyy-MM-dd / yy-MM-dd 格式。⑥“^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$” //正浮点数。

wuds_158的博客 1609

com.sap.aii.af.service.cpa.CPAObjectNotFoundException: Couldn‘t retrieve inbound binding

错误消息:错误截图:该错误通常表示在集成配置中,未找到与特定消息匹配的入站绑定(Inbound Binding)。具体而言,错误消息中的 P/S/A(Party/Service/Action)值未在配置中找到对应的入站绑定。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 373

最长公共子序列C++

最长公共子序列问题描述最长公共子序列问题分析代码 问题描述 先来了解一下字串和子序列,一个串的子串是指该串的一个连续的局部。如果不要求连续,则可称为它的子序列。 比如对串: “abcdefg” 而言,“ab”,“abd”,“bdef” 等都是它的子序列。 特别地,一个串本身,以及空串也是它的子序列。 最长公共子序列 给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致。 例如:abcfbc与abfcab的最长公共子序列是a

qq_41183949的博客 3549

FFMPEG音频开发: Linux下采集摄像头(使用V4L2框架)数据录制成MP4视频保存到本地

一、环境介绍 操作系统介绍:ubuntu 18.04 FFMPEG版本: 4.4.2 摄像头: USB摄像头、虚拟机挂载本机自带摄像头 二、FFMPEG与X264下载编译 X264下载地址: http://www.videolan.org/developers/x264.html FFMPEG下载地址:https://ffmpeg.org/download.html Yasm ...

8870

JAVA实现最长公共子序列

4、实验三最长公共子序列 实验内容 输入两个相同类型的序列,用动态规划方法计算他们的最长公共子序列的长度以及序列。 解题思路 1、先用一个二维数组存储最长公共子序列的长度,还要记录每个值的状态 2、根据记录值的状态,递归回溯求出最长公共子序列 3、递归方程是: 源代码 package c最长公共子序列; import java.util.Scanner; /** * @author Draco * @see 最长公共子序列(Longest common subsequence) * @v

不会飞的王 3015

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

3.3 最长公共子序列

我的理解:在字符串或序列中,子序列是指从原始序列中删除零个或多个元素后得到的序列,而且相对顺序保持不变。换句话说,子序列是从原始序列中选择出的元素,它们保持相对顺序,但可以跳过一些元素或者不选择任何元素。举个例子,考虑序列 "abcd",它的一些子序列包括:"a","b","c","d","ab","ac","ad","bc","bd","cd","abc","abd","acd","bcd","abcd"。注意,这些子序列可能不一定是连续的,可以跳过某些元素。

tang7mj的博客 3006

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

深刻理解最长公共子串、最长公共子序列(应该比较全面)

这里写自定义目录标题 理论知识: 推荐参考该博文:java实现字符串匹配问题之求两个字符串的最大公共子串 当然这篇也一样,看个人理解:求两个字符串的最长公共子串 图形理解: ...

3230

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

最长公共子序列算法 java,算法学习——java实现最长公共子序列,

算法学习——java实现最长公共子序列学习——java实现最长公共子序列的算法,实验目的:输入两个同类型的序列,用动态规划的方法计算它们最长的公共子序列的长度和序列。(推荐教程: Java视频教程思考:1.首先,使用二维数组存储最长公共子序列的长度,并记录每个值的状态2.根据记录值的状态,递归计算最长的公共子序列3.递归方程:代码实现:Package c最长公共子序列;导入Java . uti...

weixin_39594103的博客 3098

【算法理解笔记1】最长公共子序列Python

刚开始学习,第一次总结,希望对大家有帮助!

m0_50569708的博客 341

深入理解动态规划算法 - 最长公共子序列

通过欢迎点击「算法与编程之美」↑关注我们!本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列文章。前面三篇文章已经为大家介绍了利用动态规划算法解决问题...

算法与编程之美 755
下一篇: ACM进阶学习计划
amazingcode_lyl
博客等级 码龄11年 21粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值