当前位置:网站首页>Leetcode:940. How many subsequences have different literal values
Leetcode:940. How many subsequences have different literal values
2022-07-26 06:06:00 【Oceanstar's study notes】
Title source
Title Description


class Solution {
public:
int distinctSubseqII(string s) {
}
};
title
Statistics must be in a~z The number of subsequences at the end

class Solution {
public:
int distinctSubseqII(string s) {
std::vector<long> dp(26, 0);
int mod = 1e9 + 7;
for(auto &ch : s){
dp[ch - 'a'] = accumulate(dp.begin(), dp.end(), 1l) % mod;
}
return accumulate(dp.begin(),dp.end(),0L)%mod;// Sum up
}
};
Dynamic programming + Hashtable
Try the model from left to right
If there are no repeated words in the sequence
every , Either choose , Or don't choose . So the length is N The number of subsequences of the sequence of , Namely 2 N 2^N 2N
If we use dynamic programming to solve it , Make d p [ i ] dp[i] dp[i] by s [ 1... i ] s[1...i] s[1...i] The number of subsequences of ( Contains an empty string ) that :
- The equation of state transfer is d p [ i + 1 ] = 2 ∗ d p [ i ] dp[i+1] = 2 * dp[i] dp[i+1]=2∗dp[i]
- among d p [ 0 ] = 1 dp[0] = 1 dp[0]=1, For empty strings , Of course, there is only one subsequence , It's an empty string .
What if there are repeated substrings in the sequence ?
- such as “abcbc” in “ab” It can be s[0] + s[1] It can also be s[0] + s[3].
- We use the idea of dynamic programming , This is the time s[i+1] Take it or not , All subsequences produced consist of two parts .
- Part of it is s[i+1] No , So this molecular sequence and s[i] The corresponding subsequence is the same .
- Another part s[i+1] take , Then there will be part of this molecular sequence and s[i] Subsequence coincidence of , We need to find out this part and deduct it .
- If s[i+1] Never appeared before , Then obviously there will be no overlapping parts .
- If s[i+1] There have been , The location assumption of the last occurrence is last, that dp[last] In fact, it is the overlapping part . Because their last one is the same . Let's subtract this part .
- therefore , The final state transition equation is as follows : d p [ i + 1 ] = d p [ i ] ∗ 2 − d p [ l a s t [ s [ i ] ] ] dp[i+1] = dp[i] * 2 - dp[last[s[i]]] dp[i+1]=dp[i]∗2−dp[last[s[i]]]
class Solution {
public:
int distinctSubseqII(string s) {
int n = s.size();
int MOD = 1000000007;
if(n == 0){
return 0;
}
std::vector<int> dp(n + 1);
std::vector<int> last(26, -1);
dp[0] = 0;
for (int i = 0; i < n; ++i) {
dp[i + 1] = dp[i] * 2 % MOD;
if(last[s[i] - 'a'] >= 0){
dp[i + 1] -= dp[last[s[i] - 'a']];
}
dp[i + 1] %= MOD;
last[s[i] - 'a'] = i;
}
// Finally, remove the empty string
return (dp[n] - 1 + MOD) % MOD;
}
};
边栏推荐
- Project topic selection reference
- Jincang database kingbasees SQL language reference manual (5. Operators)
- Redis publish subscription
- C language explanation series - comprehensive exercises, guessing numbers games
- Blurring of unity pixel painting
- Learn about spark project on nebulagraph
- Balanced binary tree (AVL)~
- 逆序打印链表
- EM and REM
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
猜你喜欢

Embedded sharing collection 15
![[paper notes] anti packet loss joint coding for network speech steganography](/img/ca/95476b6d4b5765f5fde82cbeda577e.png)
[paper notes] anti packet loss joint coding for network speech steganography

Amd zen4 game God u reached 208mb cache within this year, which is unprecedented

Optical quantum milestone: 3854 variable problems solved in 6 minutes

Using dynamic libraries in VS

Widget is everything, widget introduction
![[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM](/img/19/ca3a5710ead3c5b9a222a8ae4ecb37.png)
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM

Jdbc流式查询与游标查询

2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers

某公司给每个工位装监控:只为看员工写代码?
随机推荐
金仓数据库 KingbaseES SQL 语言参考手册 (11. SQL语句:ABORT 到 ALTER INDEX)
A company installs monitoring for each station: write code only to see employees?
满二叉树 / 真二叉树 / 完全二叉树 ~
【2023杰理科技提前批笔试题】~ 题目及参考答案
H. Take the elevator greedy
Embedded sharing collection 15
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
Database SQL language practice
Sequential search, half search, block search~
Practice operation and maintenance knowledge accumulation
L. Link with Level Editor I dp
flex布局
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
Using easyexcel to import tables to realize batch insertion of xlsx files ----- MySQL of Linux
Ros2 knowledge: DDS basic knowledge
leetcode-aboutString
Is the transaction in mysql45 isolated or not?
Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
Flex layout
程序员如何改善精神内耗?