LeetCode:Distinct Subsequences

LeetCode: Distinct Subsequences(不同子序列的个数) 题目描述: Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of a string is a new string which is formed from the original string by deleting some (can b 阅读详情

Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example:
S = "rabbbit", T = "rabbit"

Return 3.

题意:给定两个字符串S和T,找出S中删除若干个字符后,和T相等的字符串的个数。
例如:S="rabbbit",T="rabbit",则“ra_bbit" "rab_bit" rabb_it,一共三个(其中‘_’表示删除的字符)

解题思路:用经典的dp来解,构建一个二维数组table[i][j](其中T的长度为行,S的长度为列),每一个元素表示在T[0...i]、S[0...j]中的解决方案的个数,然后按照以下规则进行递推:
1)i=0,则table[i][j]=1。因为i=0它表示T为空,此时无论S为多少,都存在S=""的情况(删除S中所有字符)。
2)i>0,若T[i] != S[j], 则没有新的解决方案,矩阵依旧和以前一样:table[i][j] = table[i][j-1]
           若T[i] == S[j],则有新的解决方案,其一,它包括table[i-1][j-1]方案的个数;其二,它还包括table[i][j-1]方案的个数(因为把S[j]删掉,它也应该具有和前者一样多的解决 方案);
因此,i>0时的解决方案一共有:table[i][j] = table[i-1][j-1] + table[i][j-1];

int numDistinct(string S, string T) 
{
	int m = T.length(), n = S.length();
	if (m > n)
		return 0;

	int** table = new int *[m + 1];
	for (int i = 0; i <= m; i++)
		table[i] = new int[n+1];

	for (int j = 0; j <= n; j++)
		table[0][j] = 1;

	for (int i = 1; i <= m; i++)
		table[i][0] = 0;

	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (T[i-1] != S[j-1])
				table[i][j] = table[i][j - 1];
			else
				table[i][j] = table[i][j - 1] + table[i - 1][j - 1];
		}
	}

	int res = table[m][n];
	for (int i = 0; i <= m; i++)
		delete[] table[i];

	delete[] table;
	
	return res;
}

axi时序图_(AXI)握手协议(pvld/prdy或者valid-ready)中ready打拍技巧 内容提要ready打拍的问题用FIFO的思路去解决用Buffer的思路去解决问题提出:ready时序如何优化?在valid/ready 握手协议中,valid 与 data的时序优化比较容易理解,(不熟悉valid/ready协议或者valid打拍方法的)大家可以参考上次推送(握手协议(pvld/prdy或者valid-ready或AXI)中Valid及data打拍技巧)。 但是有时候,关键路径是... 阅读详情

相关推荐

用大白话讲解Carplay(原创)

用大白话讲解Carplay: Carplay是美国苹果公司开发的一套服务,用户通过这套服务可以将iPhone与车载系统连接,然后在车载大屏上操作使用iPhone手机。个人理解,Carplay不能叫车载系统,不是操作系统,只能算一种服务。让你在车上的大屏上使用iPhone,这样用起来更方便,不需要在手机小屏幕上操作。基于这一点,有些人也说Carplay就是个投屏的功能,将手机屏幕投屏到了车机大屏上。可以简单这么理解,但是并不只是投屏这么简单,如果只是投屏的话,车子只是起到了一个显示器的功能,其实不止,手机和

cici2013的专栏 2万+

LeetCode: Distinct Subsequences

LeetCode: Distinct Subsequences LeetCode: Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a st...

weixin_33913377的博客 157

[Multisim][模电实验]简易函数信号发生器的设计与实现_北京邮电大学2019级信通院电子电路实验下

本文记述了笔者设计并实现以±12V供电电压输出1KHZ~10KHZ频率连续可调的方波、三角波和正弦波的过程。其中,方波输出电压峰峰值为12V(误差<20%),占空比可调、三角波输出电压峰峰值为8V(±20%),正弦波输出电压峰峰值≥1V。

lgc0208的博客 4万+

Leetcode:Distinct Subsequences

url:https://leetcode.com/problems/distinct-subsequences/description/描述:Given a string S and a string T, count the number of distinct subsequences of S which equals T.A subsequence of a string is a new

zhumingyuan111的博客 287

Ollama+FAISS+Embedding+Rerank+LLM 详细部署指南

下载安装Ollama 双击运行安装程序 安装完成后会自动启动服务 修改默认安装路径(避免C盘爆满) 或使用PowerShell: 验证安装 Linux系统部署: (二)准备模型 1. 拉取Embedding模型 2. 拉取LLM模型 注意:原文中提到的可能需要从自定义仓库拉取: 3. 下载Rerank模型 (三)安装依赖库 创建项目目录和虚拟环境: 二、主要功能实现 项目结构 (一)配置文件(config/config.yaml) (二)文本分块策略实现 创建 : (三)文档读取

AI技术改变世界 1575

matlab电偶极子源码,电偶极子的MATLAB场模拟

电偶极子周围电场电势分布的MATLAB三维模拟Matlab可以用于模拟四维场的效果,本文以电偶极子的电场和电势场为例展示MATLAB在该方面领域的应用。摘要电偶极子由一个正电荷和一个负电荷组成,其电场和空间电势分布与单电荷相比具有一些重要的特性。本文将给出电场和电势场的近似表达式。这两个量的精确表达式是复杂的,不太常用,因此 MATLAB 被采用来操作精确值和绘制三维分布的远区域, 近区域和一侧区...

weixin_35135480的博客 3150

[LeetCode] 027: Distinct Subsequences

[Problem] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some

1.02^365的成长裂变 3651

YOLOv8 改进:使用 MobileNetV3 轻量化架构改进特征提取网络

通过使用 MobileNetV3 作为 YOLOv8 的特征提取器,能够显著降低模型复杂度和运行时资源消耗,同时在一定程度上保持检测精度。这种方法特别适合需要高效计算的边缘设备和嵌入式系统。

走向CTO的路上... 1275

LeetCode题解:Distinct Subsequences

Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by

Magi Su的技术回收站 954

单反相机物镜设计[DG-10]

REAR(要打在像面,操作数要找像面对应位置15):以透镜单位表示的真实光线径向坐标,波长由Wave定义,参见“Hx、Hy、Px和Py”。最小玻璃厚度最小不能小于semi-dia的1/10,要根据layout看有没有过厚过薄,反复调节。PROD:两个操作数的乘积(操作#1 X操作#2)。3x4就是57.3x0.41=23.5。拦光一般在第一面和最后一面操作,减少边缘无用光线。光阑面与镜面贴近,要调距离,同时设置光阑像差。后截距是指从镜头后表面到焦点的距离。CONS:弧度转为角度。镜头相互嵌入,有错误。

weixin_72050316的博客 600

大规模新闻聚合爬虫实战:Python爬取数百万新闻数据的完整指南

本文详细介绍了如何构建一个大规模新闻聚合爬虫,结合Python异步爬虫技术,实现高效、稳定的新闻数据采集。增加NLP模块对新闻内容做自动分类、情感分析。引入机器学习模型检测新闻真实性。使用分布式爬虫框架进一步提升规模。增加多媒体内容抓取与处理。

2201_76125261的博客 1554

Leetcode: Distinct Subsequences

Question Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (ca

ayst123 的专栏 339

2 君正底层API接口的封装1

君正的IPC芯片主要有T31、T40、T41等型号,每个型号的API接口大部分是相同的,但也存在一些不一致和差异化的地方。为了屏蔽不同芯片型号之间的差异,我们需要基于一个公共基类进行封装。在封装之前,我们需要定义自己的错误码和数据结构,用于屏蔽不同君正芯片的实现细节。为了确保整个IPC库使用统一的错误码,我们在HP_MPPErrorCode.h文件中专门定义了以下错误码。错误码囊括了参数错误、内存错误、溢出错误、不支持、密码错误、非法数据等各种异常情况。

hope_wisdom的技术博客 1586

leetcode: distinct-subsequences

题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is form...

chenglan9265的博客 142

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode习题:Distinct Subsequences

Distinct Subsequences  Total Accepted: 2790 Total Submissions: 12196My Submissions Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a

APPLE的专栏 2305

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode: Distinct Subsequences 解题报告

Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by dele...

weixin_34008933的博客 201

jre9下载 jdk9

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

LeetCode: 115. Distinct Subsequences

LeetCode: 115. Distinct Subsequences 题目描述 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed f...

杨领well的专栏 587

LeetCode: Distinct Subsequences [115]

【题目】 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relati

Harry Huang 1066
上一篇: LeetCode : Partition List
下一篇: Windows下C/C++连接MySQL
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值