<LeetCode>115.Distinct Subsequences 求相同子序列数 Tag:DP, string
2017-05-15 11:59
316 查看
题目描述:
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,
Here is an example:
S =
Return
参考答案1:
利用递归的思路。假设当前输入串分别为 s_tmp, t_tmp, 目的是求s_tmp的不同子序列中t_tmp出现的个数。先利用for循环寻找s_tmp串中第一个与t_tmp[0]对应的字符,从而确定了输入递归函数的新的s_tmp,t_tmp。
不过这种方法在LeetCode上会TLE, 仅供参考。
class Solution {
public:
int numDistinct(string s, string t) {
if(s.size() < t.size()) return 0;
if(t.size() == 0) return 1;
int num = 0;//可能数
for(int i = 0;i < s.size();i++)//寻找初始对应点,有可能是多个
{
if(s[i] == t[0])//i开始对应
{
string s_tmp = s.substr(i+1);
string t_tmp = t.substr(1);
num += numDistinct(s_tmp, t_tmp);
}
}
return num;
}
};
参考答案2:
利用DP动态规划的思路。定义table[i][j]为s[0:i]的不同子序列中t[0:j]出现的个数,那么按照如下方式构建table表:
初始化:table[i][0] = 1,for i ∈ [0,s.size())
如果 s[i] != t[j]: table[i][j] = table[i - 1][j];
如果 s[i] == t[j]: 分s[i]与t[j]是否配对两种情况,则 table[i][j] = table[i-1][j] + table[i-1][j-1]
int numDistinct(string s, string t) {
if(s.size() < t.size()) return 0;
if(t.size() == 0) return 1;
int const M = s.size();
int const N = t.size();
vector<vector<int>> table(M + 1,vector<int>(N + 1,0));
for(int i = 0;i < M;i++)
{
table[i][0] = 1;
}
for(int i = 0;i < M ;i++)
{
for(int j = 0;j < N ;j++)
{
if(s[i] == t[j])
{
table[i+1][j+1] = table[i][j+1] + table[i][j];
}
else
{
table[i+1][j+1] = table[i][j+1];
}
}
}
return table[M]
;
}
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.
参考答案1:
利用递归的思路。假设当前输入串分别为 s_tmp, t_tmp, 目的是求s_tmp的不同子序列中t_tmp出现的个数。先利用for循环寻找s_tmp串中第一个与t_tmp[0]对应的字符,从而确定了输入递归函数的新的s_tmp,t_tmp。
不过这种方法在LeetCode上会TLE, 仅供参考。
class Solution {
public:
int numDistinct(string s, string t) {
if(s.size() < t.size()) return 0;
if(t.size() == 0) return 1;
int num = 0;//可能数
for(int i = 0;i < s.size();i++)//寻找初始对应点,有可能是多个
{
if(s[i] == t[0])//i开始对应
{
string s_tmp = s.substr(i+1);
string t_tmp = t.substr(1);
num += numDistinct(s_tmp, t_tmp);
}
}
return num;
}
};
参考答案2:
利用DP动态规划的思路。定义table[i][j]为s[0:i]的不同子序列中t[0:j]出现的个数,那么按照如下方式构建table表:
初始化:table[i][0] = 1,for i ∈ [0,s.size())
如果 s[i] != t[j]: table[i][j] = table[i - 1][j];
如果 s[i] == t[j]: 分s[i]与t[j]是否配对两种情况,则 table[i][j] = table[i-1][j] + table[i-1][j-1]
int numDistinct(string s, string t) {
if(s.size() < t.size()) return 0;
if(t.size() == 0) return 1;
int const M = s.size();
int const N = t.size();
vector<vector<int>> table(M + 1,vector<int>(N + 1,0));
for(int i = 0;i < M;i++)
{
table[i][0] = 1;
}
for(int i = 0;i < M ;i++)
{
for(int j = 0;j < N ;j++)
{
if(s[i] == t[j])
{
table[i+1][j+1] = table[i][j+1] + table[i][j];
}
else
{
table[i+1][j+1] = table[i][j+1];
}
}
}
return table[M]
;
}
相关文章推荐
- <LeetCode>125.Valid Palindrome 验证回文字符串 Tag:string
- <序列DP>codevs 2980 买帽子
- <序列DP>O(nlogn)的最长上升子序列
- <序列DP>codevs 4748 低价购买
- <leetcode>String to Integer (atoi)
- <九度 OJ>题目1094:String Matching
- <Leetcode>TWO SUM
- < C++ > string c_str
- <string> 与<string.h>、<cstring>的区别
- <uses-permission> tag appears after <application> tag
- <LeetCode><Easy>290 Word Pattern
- <Effective STL>笔记--vector和string
- 【转】<string> <string.h> <cstring>的区别
- <LeetCode><Easy>258Add Digits
- C++中各种<string,T>关联方式的速度对比
- <已解决>使用selector设置Button按下松开的样式以及 <item> tag requires a 'drawable' attribute or child tag defining a drawable 报错
- <LeetCode> Single Number
- <string.h>的学习
- <cf>Solitaire(DFS or DP)
- CF260--C--dp<最大不连续子序列和>