@
目录动态规划
● 392.判断子序列
● 115.不同的子序列
392.判断子序列
法1:动态规划
bool isSubsequence(string s, string t) {
//动态规划
vector<vector<int>>dp(s.size() + 1,vector<int>(t.size() + 1, 0));
for (int i = 1; i <= s.length(); ++i) {
for (int j = 1; j <= t.length(); ++j) {
if (s[i - 1] == t[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = dp[i][j - 1];
}
}
if (dp[s.size()][t.size()] == s.length())return true;
else return false;
}
//法2:双指针
bool isSubsequence(string s, string t) {
//双指针
int m =s.length(); int n =t.length();
int l = 0,r = 0;
while (l < m && r < n){
if (s[l] == t[r]){
l++;
}
r++;
}
return (l == m);
}
115.不同的子序列
115.不同的子序列
法1:动态规划
int numDistinct(string s, string t) {
vector<vector<unsigned int>>dp(s.size() + 1,vector<unsigned int>(t.size() + 1, 0));
for (int i = 0; i <= s.size(); ++i) {
dp[i][0] = 1;
}
for (int j = 1; j <= t.size(); ++j) {
dp[0][j] = 0;
}
for (int i = 1; i <= s.size(); ++i) {
for (int j = 1; j <= t.size(); ++j) {
if (s[i - 1] == t[j - 1])
dp[i][j] =dp[i -1][j - 1] +dp[i - 1][j];
else dp[i][j] = dp[i - 1][j];
}
}
return dp[s.length()][t.length()];
}
标签:15,string,392,55,115,int,序列,size,刷题
From: https://www.cnblogs.com/asupersource-tech/p/17441846.html