当前位置:网站首页>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;
}
};
边栏推荐
- Redis persistence RDB
- Print linked list in reverse order
- 卸载手机自带APP的操作步骤
- 递归处理——子问题
- Introduction to three feasible schemes of grammatical generalization
- Operating steps for uninstalling the mobile app
- Redis publish subscription
- Talking about the practice of software defect management
- 【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
- Lemon class automatic learning after all
猜你喜欢

Widget is everything, widget introduction

Viewing the technology stack of distributed system from the crash report of station B

Print linked list in reverse order

Kingbasees SQL language reference manual of Jincang database (6. Expression)

金仓数据库 KingbaseES SQL 语言参考手册 (7. 条件表达式)

Unity2D 动画器无法 创建过渡

Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument

leetcode-aboutString

leetcode-Array

Blurring of unity pixel painting
随机推荐
Docking wechat payment (II) unified order API
逆序打印链表
Two auxiliary functions of integral Mall for business user operation
实习运维知识积累
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
Solutions to the failure of copy and paste shortcut keys
How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
[free and easy to use] holiday query interface
Recursive processing - subproblem
Byte interview question - judge whether a tree is a balanced binary tree
数据库sql语言实战
Can you make a JS to get the verification code?
2022年下半年系统集成项目管理工程师(软考中级)报名条件
[2023 Jerry technology approval test questions in advance] ~ questions and reference answers
Widget is everything, widget introduction
Full binary tree / true binary tree / complete binary tree~
ament_ Cmake generates the ros2 library and links it
VRRP principle and basic commands
L. Link with Level Editor I dp
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented