Distinct Subsequences
https://leetcode.com/problems/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
.
定义f[i][j]表示在S[0,i]中,T[0,j]出现了几次。无论s[i]和t[j]是否相等,如果不匹配s[i],则f[i][j]=f[i-1][j];若s[i]==s[j],
f[i][j]=f[i-1][j]+[i-1][j-1]。
另外,当t=""时,只有一种匹配方式,f[i][0]=1;当s="",t!=""时,无论如何无法匹配,此时f[0][j]=0。
参考:http://www.cnblogs.com/yuzhangcmu/p/4196373.html
int numDistinct(string s, string t) {
int m=s.size();
int n=t.size(); vector<vector<int>> f(m+,vector<int>(n+,)); for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
if(i== && j==)
f[i][j]=;
else if(i==)
f[i][j]=;
else if(j==)
f[i][j]=;
else
f[i][j]=f[i-][j]+(s[i-]==t[j-]?f[i-][j-]:);
}
}
return f[m][n];
}
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 ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- html之select标签
循环select标签 <select name="group_id"> {% for row in group_list %} <option value={{r ...
- [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 【JavaWeb学习】文件的上传和下载
一.文件上传 1.1.概述 实现web开发中的文件上传功能,需完成如下二步操作: 在web页面中添加上传输入项 在servlet中读取上传文件的数据,并保存到本地硬盘中 如何在web页面中添加上传输入 ...
- C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有
前言:最近项目里面有一些对Excel操作的需求,博主想都没想,NPOI呗,简单.开源.免费,大家都喜欢!确实,对于一些简单的Excel导入.导出.合并单元格等,它都没啥太大的问题,但是这次的需求有两点 ...
- Hibernate+EhCache配置二级缓存
步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...
- ITIL十大流程
1.服务水平管理(Service Level Management):服务水平管理的目标是通过协调IT用户和提供者双方的观点,实现特定的.一致的.可测量的服务水平,以为客户节省成本.提高用户生产率. ...
- DedeCMS使用方法----如何将网站上传到服务器
我们如果在本地已经把网站做好了,上传到服务器上去的正确姿势是什么样的呢?简单的很~跟着我的步调来~ 方法一(推荐此方法): 1.把你本地所有的文件压缩,上传至服务器上的根目录,再解压. 2.把本地的数 ...
- PL/SQL异常获取
1.no_data SET SERVEROUTPUT ON DECLARE pename EMP.ENAME % TYPE ; BEGIN SELECT ename INTO pename FROM ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- Bzoj4008 [HNOI2015]亚瑟王
Time Limit: 20 Sec Memory Limit: 512 MBSec Special Judge Submit: 1009 Solved: 605[Submit][Status] ...