当前位置:网站首页>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;
}
};
边栏推荐
- Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- VRRP principle and basic commands
- Calling mode and execution sequence of JS
- How to view the container name in pod
- [paper notes] anti packet loss joint coding for network speech steganography
- Properties of binary tree~
- Introduction of four redis cluster schemes + comparison of advantages and disadvantages
- Redis master-slave replication
- 基于消防GIS系统的智慧消防应用
猜你喜欢

满二叉树 / 真二叉树 / 完全二叉树 ~

Balanced binary tree (AVL)~

Introduction to three feasible schemes of grammatical generalization
![[the most complete and detailed] ten thousand words explanation: activiti workflow engine](/img/4c/2e43aef33c6ecd67d40730d78d29dc.png)
[the most complete and detailed] ten thousand words explanation: activiti workflow engine

VRRP protocol and experimental configuration

Mysql45 talks about transaction isolation: why can't I see it after you change it?

软件测试面试题全网独家没有之一的资深测试工程师面试题集锦

Print linked list in reverse order

Modifiers should be declared in the correct order

Redis persistence AOF
随机推荐
二叉树的前中后序遍历——本质(每个节点都是“根”节点)
Solutions to the failure of copy and paste shortcut keys
Youwei low code: Brick life cycle component life cycle
Introduction to three feasible schemes of grammatical generalization
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
[free and easy to use] holiday query interface
光量子里程碑:6分钟内解决3854个变量问题
[paper notes] anti packet loss joint coding for network speech steganography
JS的调用方式与执行顺序
vagrant下载速度慢的解决方法
Interview difficulties: difficulties in implementing distributed session, this is enough!
VRRP principle and basic commands
leetcode-aboutString
Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
Redis publish subscription
VRRP protocol and experimental configuration
[the most complete and detailed] ten thousand words explanation: activiti workflow engine
Matlab vector and matrix
ETCD数据库源码分析——Cluster membership changes日志
Lemon class automatic learning after all