Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 0392.判断子序列.md #2390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions problems/0392.判断子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

if (s[i - 1] == t[j - 1]),那么dp[i][j] = dp[i - 1][j - 1] + 1;,因为找到了一个相同的字符,相同子序列长度自然要在dp[i-1][j-1]的基础上加1(**如果不理解,在回看一下dp[i][j]的定义**)

if (s[i - 1] != t[j - 1]),此时相当于t要删除元素,t如果把当前元素t[j - 1]删除,那么dp[i][j] 的数值就是 看s[i - 1]与 t[j - 2]的比较结果了,即:dp[i][j] = dp[i][j - 1];
if (s[i - 1] != t[j - 1]),此时应该选择dp[i-1][j] 和 dp[i][j-1] 中较大的一个作为dp[i][j]的值,即:dp[i][j] = max(dp[i-1][j], dp[i][j-1])。如果只是把t作为要删除元素,让dp[i][j]=dp[i][j-1] 则会出现有的位置的值存在错误的情况,虽然依旧可以AC,但是逻辑上是存在错误的。

其实这里 大家可以发现和 [1143.最长公共子序列](https://programmercarl.com/1143.最长公共子序列.html) 的递推公式基本那就是一样的,区别就是 本题 如果删元素一定是字符串t,而 1143.最长公共子序列 是两个字符串都可以删元素。

Expand Down Expand Up @@ -123,7 +123,7 @@ public:
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] = dp[i][j - 1];
else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
if (dp[s.size()][t.size()] == s.size()) return true;
Expand Down