mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-09-04 01:01:43 +00:00
feat: 添加P4267.cpp空模板文件并更新README文档
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/并更新内容
This commit is contained in:
parent
830b825fbf
commit
37f3373029
@ -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]
|
||||
> 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
|
@ -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<ll> result_stack;
|
||||
int curr_i = n;
|
||||
int curr_j = end_j;
|
||||
if (maxlen > 0) {
|
||||
std::stack<ll> 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;
|
||||
}
|
||||
|
3
src/8/28/P4267.cpp
Normal file
3
src/8/28/P4267.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
int main(){
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user