From 37f3373029a626344d48e57a9a032f4d8b8d342f Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Thu, 28 Aug 2025 15:16:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0P4267.cpp=E7=A9=BA?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E6=96=87=E4=BB=B6=E5=B9=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?README=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor: 重命名变量以提高代码可读性 将max_len改为maxlen,predecessor_k改为pk,max_lcsis改为maxlen,end_j改为endj,result_stack改为res,curr_i改为curri,curr_j改为currj chore: 移动sort-matrix-by-diagonals.cpp文件位置 从src/8/27/移动到src/8/28/并更新内容 --- README.md | 7 ++- src/8/27/opj2000.cpp | 56 +++++++++---------- src/8/28/P4267.cpp | 3 + src/8/{27 => 28}/sort-matrix-by-diagonals.cpp | 0 4 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 src/8/28/P4267.cpp rename src/8/{27 => 28}/sort-matrix-by-diagonals.cpp (100%) diff --git a/README.md b/README.md index 731915a..b1d352e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,12 @@ >如果是递减序列就手写二分 ## 区间dp +### 步骤 1. 根据问题推出dp含义 2. 根据规则写出dp的状态转移公式 3. 处理边界问题 -> dp[i][j], dp[0][0], dp[i][0], dp[0][j], dp[i][i], dp[j][j] \ No newline at end of file +> dp[i][j], dp[0][0], dp[i][0], dp[0][j], dp[i][i], dp[j][j] +### +1. 编辑距离 i-1,j i,j-1 +2. 合并石子 1~k,k+1~i +3. 网捉蛇 1~k用j-1, k+1~i用1 \ No newline at end of file diff --git a/src/8/27/opj2000.cpp b/src/8/27/opj2000.cpp index 6eb37e7..e8db361 100644 --- a/src/8/27/opj2000.cpp +++ b/src/8/27/opj2000.cpp @@ -26,9 +26,9 @@ int main() { for (int i = 1; i <= n; ++i) { - int max_len = 0; + int maxlen = 0; - int predecessor_k = 0; + int pk = 0; for (int j = 1; j <= m; ++j) { @@ -36,60 +36,58 @@ int main() { path[i][j] = {i - 1, j}; if (a[i] == b[j]) { - if (max_len + 1 > dp[i][j]) { - dp[i][j] = max_len + 1; - path[i][j] = {i - 1, predecessor_k}; + if (maxlen + 1 > dp[i][j]) { + dp[i][j] = maxlen + 1; + path[i][j] = {i - 1, pk}; } } if (b[j] < a[i]) { - if (dp[i - 1][j] > max_len) { - max_len = dp[i - 1][j]; - predecessor_k = j; + if (dp[i - 1][j] > maxlen) { + maxlen = dp[i - 1][j]; + pk = j; } } } } - int max_lcsis = 0; - int end_j = 0; + int maxlen = 0; + int endj = 0; for (int j = 1; j <= m; ++j) { - if (dp[n][j] > max_lcsis) { - max_lcsis = dp[n][j]; - end_j = j; + if (dp[n][j] > maxlen) { + maxlen = dp[n][j]; + endj = j; } } - std::cout << max_lcsis << "\n"; + std::cout << maxlen << "\n"; - if (max_lcsis > 0) { - std::stack result_stack; - int curr_i = n; - int curr_j = end_j; + if (maxlen > 0) { + std::stack res; + int curri = n; + int currj = endj; - while (curr_i > 0 && curr_j > 0) { - pii prev = path[curr_i][curr_j]; + while (curri > 0 && currj > 0) { + pii prev = path[curri][currj]; - if (dp[curr_i][curr_j] > dp[prev.first][prev.second] && prev.second != curr_j) { - result_stack.push(a[curr_i]); + if (dp[curri][currj] > dp[prev.first][prev.second] && prev.second != currj) { + res.push(a[curri]); } - curr_i = prev.first; - curr_j = prev.second; + curri = prev.first; + currj = prev.second; } bool first = true; - while (!result_stack.empty()) { + while (!res.empty()) { if (!first) { std::cout << " "; } - std::cout << result_stack.top(); - result_stack.pop(); + std::cout << res.top(); + res.pop(); first = false; } std::cout << "\n"; } - - return 0; } diff --git a/src/8/28/P4267.cpp b/src/8/28/P4267.cpp new file mode 100644 index 0000000..294989d --- /dev/null +++ b/src/8/28/P4267.cpp @@ -0,0 +1,3 @@ +int main(){ + +} \ No newline at end of file diff --git a/src/8/27/sort-matrix-by-diagonals.cpp b/src/8/28/sort-matrix-by-diagonals.cpp similarity index 100% rename from src/8/27/sort-matrix-by-diagonals.cpp rename to src/8/28/sort-matrix-by-diagonals.cpp