class Solution {
public:
bool isSubsequence(string s, string t) {
/*
dp[i][j] : 在长度为0~i-1的s的子串和长度为0~j-1的t的字串中选择,最长的公共子序列
dp[i][j] =
if s[i - 1] == t[j - 1] : dp[i - 1][j - 1] + 1
else : max(dp[i][j - 1], dp[i - 1][j])
*/
vector<vector<int>> dp(s.size()+1, vector<int>(t.size()+1,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] + 1;
else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
}
}
return dp[s.size()][t.size()] == s.size();
}
};
<Talk is cheap, show me the code 29> LeetCode.392 判断子序列
最新推荐文章于 2024-11-16 21:36:47 发布