diff --git a/content/chapter-02-stack-queue/00-overview.md b/content/chapter-02-stack-queue/00-overview.md index 35733844..b0dafe2e 100644 --- a/content/chapter-02-stack-queue/00-overview.md +++ b/content/chapter-02-stack-queue/00-overview.md @@ -40,9 +40,11 @@ status: "draft" 1. 先学习[栈](./01-stack.md),建立“后进先出 + 表尾操作”的模型,再完成[Lab 02-T-01:栈选择题精练](../../labs/chapter-02/theory/T-02-01-stack-quiz/README.md)检查基础概念。 2. 再学习[队列](./02-queue.md),重点理解循环队列判空判满的边界约定,再完成[Lab 02-T-02:队列选择题精练](../../labs/chapter-02/theory/T-02-02-queue-quiz/README.md)检查公式应用与实现取舍。 3. 完成基础实现练习:先做栈序列与最小栈,再做队列窗口、循环结构和用栈实现队列。 -4. 接着学习[应用](./03-applications.md),完成其中的逆波兰表达式求值例题,并用 Lab 02-E-07 与 02-E-08 练习单调队列、单调栈与边界计算。 -5. 完成[Lab 02-T-03:栈与队列综合理论题](../../labs/chapter-02/theory/T-02-03-stack-queue-comprehensive/README.md),用 20 道选择题和 5 道理论大题检查序列约束、结构设计与证明。 -6. 最后依次完成可撤销浏览器、超市收银模拟与停车场管理三个综合 Lab。 +1. 先学习[栈](./01-stack.md),建立“后进先出 + 表尾操作”的模型,再完成[Lab 02-T-01:栈选择题精练](../../labs/chapter-02/theory/T-02-01-stack-quiz/README.md)检查基础概念。 +2. 再学习[队列](./02-queue.md),重点理解循环队列判空判满的边界约定,再完成[Lab 02-T-02:队列选择题精练](../../labs/chapter-02/theory/T-02-02-queue-quiz/README.md)检查公式应用与实现取舍。 +3. 完成基础实现练习:先做栈序列与最小栈,再做队列窗口、循环结构和用栈实现队列。 +4. 接着学习[应用](./03-applications.md),完成逆波兰表达式求值、中缀转后缀、单调栈与逐层扩散练习。 +5. 最后依次完成可撤销浏览器、超市收银模拟与停车场管理三个综合 Lab。 ## 配套 Labs @@ -50,16 +52,25 @@ status: "draft" | --- | --- | --- | | 栈的概念、边界与复杂度 | [Lab 02-T-01:栈选择题精练](../../labs/chapter-02/theory/T-02-01-stack-quiz/README.md) | 独立判断出栈序列、空满条件、括号匹配与基本操作复杂度 | | 队列的语义、边界与实现取舍 | [Lab 02-T-02:队列选择题精练](../../labs/chapter-02/theory/T-02-02-queue-quiz/README.md) | 推导循环下标与长度,辨析链队列边界、复杂度和工程选型 | -| 栈与队列综合理论 | [Lab 02-T-03:栈与队列综合理论题](../../labs/chapter-02/theory/T-02-03-stack-queue-comprehensive/README.md) | 20 道选择题与 5 道大题,完成约束分析、空间复用设计和序列计数证明 | -| 栈的模拟与接口实现 | [Lab 02-E-01:验证栈序列](../../labs/chapter-02/exercise/E-02-01-validate-stack-sequences/README.md)、[Lab 02-E-02:最小栈](../../labs/chapter-02/exercise/E-02-02-min-stack/README.md) | 从基础模拟推进到常数时间辅助状态维护 | -| 队列的窗口与接口实现 | [Lab 02-E-03:最近请求](../../labs/chapter-02/exercise/E-02-03-recent-counter/README.md)、[Lab 02-E-04:循环队列](../../labs/chapter-02/exercise/E-02-04-circular-queue/README.md)、[Lab 02-E-05:用栈实现队列](../../labs/chapter-02/exercise/E-02-05-queue-using-stacks/README.md)、[Lab 02-E-06:循环双端队列](../../labs/chapter-02/exercise/E-02-06-circular-deque/README.md) | 掌握滑动窗口、环绕下标、摊还分析和两端操作 | -| 单调结构进阶 | [Lab 02-E-07:滑动窗口最大值](../../labs/chapter-02/exercise/E-02-07-sliding-window-maximum/README.md)、[Lab 02-E-08:柱状图最大矩形](../../labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/README.md) | 维护候选单调性,处理窗口过期和左右边界 | +| 实践主题 | 对应 Lab | 验收重点 | +| --- | --- | --- | +| 栈的概念、边界与复杂度 | [Lab 02-T-01:栈选择题精练](../../labs/chapter-02/theory/T-02-01-stack-quiz/README.md) | 独立判断出栈序列、空满条件、括号匹配与基本操作复杂度 | +| 队列的语义、边界与实现取舍 | [Lab 02-T-02:队列选择题精练](../../labs/chapter-02/theory/T-02-02-queue-quiz/README.md) | 推导循环下标与长度,辨析链队列边界、复杂度和工程选型 | +| 栈的模拟与接口实现 | [02-03 验证栈序列](../../labs/chapter-02/exercise/E-02-01-validate-stack-sequences/README.md)、[02-04 最小栈](../../labs/chapter-02/exercise/E-02-02-min-stack/README.md) | 从基础模拟推进到常数时间辅助状态维护 | +| 队列的窗口与接口实现 | [02-05 最近请求](../../labs/chapter-02/exercise/E-02-03-recent-counter/README.md)、[02-06 循环队列](../../labs/chapter-02/exercise/E-02-04-circular-queue/README.md)、[02-07 用栈实现队列](../../labs/chapter-02/exercise/E-02-05-queue-using-stacks/README.md)、[02-08 循环双端队列](../../labs/chapter-02/exercise/E-02-06-circular-deque/README.md) | 掌握滑动窗口、环绕下标、摊还分析和两端操作 | +| 单调结构进阶 | [02-09 滑动窗口最大值](../../labs/chapter-02/exercise/E-02-07-sliding-window-maximum/README.md)、[02-10 柱状图最大矩形](../../labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/README.md) | 维护候选单调性,处理窗口过期和左右边界 | +| 表达式与匹配 | [Lab 02-E-09:括号匹配](../../labs/chapter-02/exercise/E-02-09-bracket-matching/README.md)、[Lab 02-E-10:逆波兰表达式求值](../../labs/chapter-02/exercise/E-02-10-rpn-evaluation/README.md)、[Lab 02-E-11:中缀表达式转后缀](../../labs/chapter-02/exercise/E-02-11-infix-to-postfix/README.md) | 检验嵌套匹配、操作数顺序、优先级和括号边界 | +| 单调栈与逐层扩散 | [Lab 02-E-12:下一个更大元素](../../labs/chapter-02/exercise/E-02-12-next-greater-element/README.md)、[Lab 02-E-13:下一个更小元素](../../labs/chapter-02/exercise/E-02-13-next-smaller-element/README.md)、[Lab 02-E-14:农夫抓牛](../../labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/README.md)、[Lab 02-E-20:股票价格跨度](../../labs/chapter-02/exercise/E-02-20-stock-span/README.md) | 维护单调候选,并用 FIFO 保证按层最短路径 | +| 队列与栈应用 | [Lab 02-E-15:Josephus 出列顺序](../../labs/chapter-02/exercise/E-02-15-josephus-order/README.md)、[Lab 02-E-16:队列生成二进制数](../../labs/chapter-02/exercise/E-02-16-binary-number-queue/README.md)、[Lab 02-E-17:双端队列回文检查](../../labs/chapter-02/exercise/E-02-17-palindrome-deque/README.md)、[Lab 02-E-18:反转队列前 K 个元素](../../labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/README.md)、[Lab 02-E-19:仅用辅助栈排序](../../labs/chapter-02/exercise/E-02-19-stack-sorting/README.md) | 练习循环调度、FIFO 生成、双端访问和辅助结构协作 | +| 栈、导航历史与 Undo/Redo | [Lab 02-P-01:可撤销浏览器](../../labs/chapter-02/project/P-02-01-undoable-browser/README.md) | 用完整页面状态协调后退、前进和页面级命令历史 | +| FIFO、多队列与离散时间 | [Lab 02-P-02:超市收银模拟](../../labs/chapter-02/project/P-02-02-supermarket-checkout/README.md) | 统一时间口径,验证等待、逗留、忙碌率和队列峰值 | +| 栈与队列联动 | [Lab 02-P-03:停车场管理](../../labs/chapter-02/project/P-02-03-parking-lot-management/README.md) | 完成倒车、便道补位、中间删除与统计 | | 栈、导航历史与 Undo/Redo | [Lab 02-P-01:可撤销浏览器](../../labs/chapter-02/project/P-02-01-undoable-browser/README.md) | 用完整页面状态协调后退、前进和页面级命令历史 | | FIFO、多队列与离散时间 | [Lab 02-P-02:超市收银模拟](../../labs/chapter-02/project/P-02-02-supermarket-checkout/README.md) | 统一时间口径,验证等待、逗留、忙碌率和队列峰值 | | 栈与队列联动 | [Lab 02-P-03:停车场管理](../../labs/chapter-02/project/P-02-03-parking-lot-management/README.md) | 完成倒车、便道补位、中间删除与统计 | ::: info Lab 编号说明 -Lab 02-T-01 与 Lab 02-T-02 是交互式概念自测,Lab 02-T-03 是包含理论大题的综合训练;Lab 02-E-01 至 Lab 02-E-08 是带起始代码、参考实现和 100 分自动测试的 Program Lab;Lab 02-P-01 至 Lab 02-P-03 是综合 Project 规格,目前仍需学习者按任务说明自行创建实现。侧栏按 Theory、Exercise、Project 分类展示。 +Lab 02-01 与 Lab 02-02 是交互式概念自测;20 道 Exercise 均带起始代码、参考实现和 100 分自动测试;3 道 Project 用于综合建模。侧栏按 Theory、Exercise、Project 分类展示。 ::: ## 学习建议 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/021-blockwise-valid.in b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/021-blockwise-valid.in deleted file mode 100644 index 6090cef5..00000000 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/021-blockwise-valid.in +++ /dev/null @@ -1,3 +0,0 @@ -7 -1 2 3 4 5 6 7 -3 2 1 5 4 7 6 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/022-invalid-late-top.in b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/022-invalid-late-top.in deleted file mode 100644 index db4a0407..00000000 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/022-invalid-late-top.in +++ /dev/null @@ -1,3 +0,0 @@ -5 -1 2 3 4 5 -1 2 5 3 4 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/022-invalid-late-top.out b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/022-invalid-late-top.out deleted file mode 100644 index 17cdefd7..00000000 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/022-invalid-late-top.out +++ /dev/null @@ -1 +0,0 @@ -NO 3 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/023-negative-valid.in b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/023-negative-valid.in deleted file mode 100644 index dde817c3..00000000 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/023-negative-valid.in +++ /dev/null @@ -1,3 +0,0 @@ -3 -0 -1 -2 --1 0 -2 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/024-three-blocks.in b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/024-three-blocks.in deleted file mode 100644 index e1e2b94b..00000000 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/024-three-blocks.in +++ /dev/null @@ -1,3 +0,0 @@ -9 -1 2 3 4 5 6 7 8 9 -3 2 1 6 5 4 9 8 7 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/025-invalid-final-block.in b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/025-invalid-final-block.in deleted file mode 100644 index 0ec1d5c3..00000000 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/025-invalid-final-block.in +++ /dev/null @@ -1,3 +0,0 @@ -8 -1 2 3 4 5 6 7 8 -2 1 5 4 8 3 7 6 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/025-invalid-final-block.out b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/025-invalid-final-block.out deleted file mode 100644 index 605e2317..00000000 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/025-invalid-final-block.out +++ /dev/null @@ -1 +0,0 @@ -NO 5 diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/cases.json b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/cases.json index dc6fe6fc..f9d42709 100644 --- a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/cases.json @@ -3,7 +3,7 @@ "id": "001-valid-sample", "input": "tests/001-valid-sample.in", "expected": "tests/001-valid-sample.out", - "points": 10, + "points": 8, "tags": [ "sample" ], @@ -15,7 +15,7 @@ "id": "002-invalid-middle", "input": "tests/002-invalid-middle.in", "expected": "tests/002-invalid-middle.out", - "points": 20, + "points": 8, "tags": [ "normal" ] @@ -24,7 +24,7 @@ "id": "003-invalid-after-first", "input": "tests/003-invalid-after-first.in", "expected": "tests/003-invalid-after-first.out", - "points": 15, + "points": 8, "tags": [ "regression" ] @@ -33,7 +33,7 @@ "id": "004-single", "input": "tests/004-single.in", "expected": "tests/004-single.out", - "points": 15, + "points": 8, "tags": [ "boundary", "numeric-boundary" @@ -43,7 +43,7 @@ "id": "005-empty", "input": "tests/005-empty.in", "expected": "tests/005-empty.out", - "points": 10, + "points": 8, "tags": [ "boundary" ] @@ -52,7 +52,7 @@ "id": "006-all-delayed", "input": "tests/006-all-delayed.in", "expected": "tests/006-all-delayed.out", - "points": 10, + "points": 4, "tags": [ "normal" ] @@ -61,28 +61,127 @@ "id": "007-scale", "input": "tests/007-scale.in", "expected": "tests/007-scale.out", - "points": 2, + "points": 4, "tags": [ "scale", "complexity" ] }, - {"id":"008-interleaved-valid","input":"tests/008-interleaved-valid.in","expected":"tests/008-interleaved-valid.out","points":1,"tags":["normal"]}, - {"id":"009-invalid-late","input":"tests/009-invalid-late.in","expected":"tests/009-invalid-late.out","points":1,"tags":["regression"]}, - {"id":"010-immediate-valid","input":"tests/010-immediate-valid.in","expected":"tests/010-immediate-valid.out","points":1,"tags":["normal"]}, - {"id":"011-negative-reverse","input":"tests/011-negative-reverse.in","expected":"tests/011-negative-reverse.out","points":1,"tags":["normal"]}, - {"id":"012-small-invalid","input":"tests/012-small-invalid.in","expected":"tests/012-small-invalid.out","points":1,"tags":["boundary"]}, - {"id":"013-delayed-valid","input":"tests/013-delayed-valid.in","expected":"tests/013-delayed-valid.out","points":1,"tags":["normal"]}, - {"id":"014-int64-values","input":"tests/014-int64-values.in","expected":"tests/014-int64-values.out","points":1,"tags":["boundary"]}, - {"id":"015-alternating-valid","input":"tests/015-alternating-valid.in","expected":"tests/015-alternating-valid.out","points":1,"tags":["normal"]}, - {"id":"016-two-valid-blocks","input":"tests/016-two-valid-blocks.in","expected":"tests/016-two-valid-blocks.out","points":1,"tags":["normal"]}, - {"id":"017-invalid-after-prefix","input":"tests/017-invalid-after-prefix.in","expected":"tests/017-invalid-after-prefix.out","points":1,"tags":["regression"]}, - {"id":"018-alternating-valid","input":"tests/018-alternating-valid.in","expected":"tests/018-alternating-valid.out","points":1,"tags":["normal"]}, - {"id":"019-extreme-values","input":"tests/019-extreme-values.in","expected":"tests/019-extreme-values.out","points":1,"tags":["boundary"]}, - {"id":"020-invalid-stack-top","input":"tests/020-invalid-stack-top.in","expected":"tests/020-invalid-stack-top.out","points":1,"tags":["regression"]}, - {"id":"021-blockwise-valid","input":"tests/021-blockwise-valid.in","expected":"tests/021-blockwise-valid.out","points":1,"tags":["normal"]}, - {"id":"022-invalid-late-top","input":"tests/022-invalid-late-top.in","expected":"tests/022-invalid-late-top.out","points":1,"tags":["regression"]}, - {"id":"023-negative-valid","input":"tests/023-negative-valid.in","expected":"tests/023-negative-valid.out","points":1,"tags":["normal"]}, - {"id":"024-three-blocks","input":"tests/024-three-blocks.in","expected":"tests/024-three-blocks.out","points":1,"tags":["normal"]}, - {"id":"025-invalid-final-block","input":"tests/025-invalid-final-block.in","expected":"tests/025-invalid-final-block.out","points":1,"tags":["regression"]} + { + "id": "008-interleaved-valid", + "input": "tests/008-interleaved-valid.in", + "expected": "tests/008-interleaved-valid.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-invalid-late", + "input": "tests/009-invalid-late.in", + "expected": "tests/009-invalid-late.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "010-immediate-valid", + "input": "tests/010-immediate-valid.in", + "expected": "tests/010-immediate-valid.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-negative-reverse", + "input": "tests/011-negative-reverse.in", + "expected": "tests/011-negative-reverse.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-small-invalid", + "input": "tests/012-small-invalid.in", + "expected": "tests/012-small-invalid.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "013-delayed-valid", + "input": "tests/013-delayed-valid.in", + "expected": "tests/013-delayed-valid.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-int64-values", + "input": "tests/014-int64-values.in", + "expected": "tests/014-int64-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "015-alternating-valid", + "input": "tests/015-alternating-valid.in", + "expected": "tests/015-alternating-valid.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-two-valid-blocks", + "input": "tests/016-two-valid-blocks.in", + "expected": "tests/016-two-valid-blocks.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "017-invalid-after-prefix", + "input": "tests/017-invalid-after-prefix.in", + "expected": "tests/017-invalid-after-prefix.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-alternating-valid", + "input": "tests/018-alternating-valid.in", + "expected": "tests/018-alternating-valid.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "019-extreme-values", + "input": "tests/019-extreme-values.in", + "expected": "tests/019-extreme-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "020-invalid-stack-top", + "input": "tests/020-invalid-stack-top.in", + "expected": "tests/020-invalid-stack-top.out", + "points": 4, + "tags": [ + "regression" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/021-duplicate-churn.in b/labs/chapter-02/exercise/E-02-02-min-stack/tests/021-duplicate-churn.in deleted file mode 100644 index 2fb6e63e..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/021-duplicate-churn.in +++ /dev/null @@ -1,17 +0,0 @@ -16 -PUSH 4 -PUSH 4 -PUSH 2 -PUSH 2 -MIN -POP -MIN -POP -MIN -PUSH 1 -MIN -POP -MIN -POP -POP -EMPTY diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/021-duplicate-churn.out b/labs/chapter-02/exercise/E-02-02-min-stack/tests/021-duplicate-churn.out deleted file mode 100644 index 1a5da05e..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/021-duplicate-churn.out +++ /dev/null @@ -1,11 +0,0 @@ -2 -2 -2 -2 -4 -1 -1 -4 -4 -4 -YES diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/022-transfer-like-sequence.in b/labs/chapter-02/exercise/E-02-02-min-stack/tests/022-transfer-like-sequence.in deleted file mode 100644 index 6f60bead..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/022-transfer-like-sequence.in +++ /dev/null @@ -1,19 +0,0 @@ -18 -PUSH 10 -PUSH 20 -MIN -POP -PUSH 5 -PUSH 6 -TOP -MIN -POP -MIN -POP -MIN -SIZE -EMPTY -POP -TOP -SIZE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/022-transfer-like-sequence.out b/labs/chapter-02/exercise/E-02-02-min-stack/tests/022-transfer-like-sequence.out deleted file mode 100644 index db8e676b..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/022-transfer-like-sequence.out +++ /dev/null @@ -1,14 +0,0 @@ -10 -20 -6 -5 -6 -5 -5 -10 -1 -NO -10 -EMPTY -0 -YES diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/023-size-after-failures.in b/labs/chapter-02/exercise/E-02-02-min-stack/tests/023-size-after-failures.in deleted file mode 100644 index d4569bd1..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/023-size-after-failures.in +++ /dev/null @@ -1,13 +0,0 @@ -12 -POP -TOP -MIN -SIZE -EMPTY -PUSH 11 -SIZE -POP -SIZE -POP -EMPTY -MIN diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/023-size-after-failures.out b/labs/chapter-02/exercise/E-02-02-min-stack/tests/023-size-after-failures.out deleted file mode 100644 index ec5d1e92..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/023-size-after-failures.out +++ /dev/null @@ -1,11 +0,0 @@ -EMPTY -EMPTY -EMPTY -0 -YES -1 -11 -0 -EMPTY -YES -EMPTY diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/024-large-positive.in b/labs/chapter-02/exercise/E-02-02-min-stack/tests/024-large-positive.in deleted file mode 100644 index b4700a82..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/024-large-positive.in +++ /dev/null @@ -1,12 +0,0 @@ -11 -PUSH 1000000000000 -PUSH 999999999999 -MIN -PUSH 1000000000001 -TOP -POP -MIN -POP -MIN -SIZE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/024-large-positive.out b/labs/chapter-02/exercise/E-02-02-min-stack/tests/024-large-positive.out deleted file mode 100644 index 0dec0796..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/024-large-positive.out +++ /dev/null @@ -1,8 +0,0 @@ -999999999999 -1000000000001 -1000000000001 -999999999999 -999999999999 -1000000000000 -1 -NO diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/025-long-minimum-chain.in b/labs/chapter-02/exercise/E-02-02-min-stack/tests/025-long-minimum-chain.in deleted file mode 100644 index 3c688b7b..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/025-long-minimum-chain.in +++ /dev/null @@ -1,21 +0,0 @@ -20 -PUSH 9 -PUSH 7 -PUSH 5 -PUSH 3 -PUSH 1 -MIN -POP -MIN -POP -MIN -POP -MIN -POP -MIN -POP -EMPTY -SIZE -TOP -POP -MIN diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/025-long-minimum-chain.out b/labs/chapter-02/exercise/E-02-02-min-stack/tests/025-long-minimum-chain.out deleted file mode 100644 index df115dbd..00000000 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/025-long-minimum-chain.out +++ /dev/null @@ -1,15 +0,0 @@ -1 -1 -3 -3 -5 -5 -7 -7 -9 -9 -YES -0 -EMPTY -EMPTY -EMPTY diff --git a/labs/chapter-02/exercise/E-02-02-min-stack/tests/cases.json b/labs/chapter-02/exercise/E-02-02-min-stack/tests/cases.json index 75e9c10c..0400dfb8 100644 --- a/labs/chapter-02/exercise/E-02-02-min-stack/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-02-min-stack/tests/cases.json @@ -3,7 +3,7 @@ "id": "001-duplicate-minimum", "input": "tests/001-duplicate-minimum.in", "expected": "tests/001-duplicate-minimum.out", - "points": 5, + "points": 8, "tags": [ "sample" ], @@ -15,7 +15,7 @@ "id": "002-empty-contract", "input": "tests/002-empty-contract.in", "expected": "tests/002-empty-contract.out", - "points": 20, + "points": 8, "tags": [ "boundary", "error" @@ -25,7 +25,7 @@ "id": "003-negative-duplicates", "input": "tests/003-negative-duplicates.in", "expected": "tests/003-negative-duplicates.out", - "points": 15, + "points": 8, "tags": [ "normal", "numeric-boundary" @@ -35,7 +35,7 @@ "id": "004-single", "input": "tests/004-single.in", "expected": "tests/004-single.out", - "points": 15, + "points": 8, "tags": [ "boundary" ] @@ -44,7 +44,7 @@ "id": "005-restore-chain", "input": "tests/005-restore-chain.in", "expected": "tests/005-restore-chain.out", - "points": 15, + "points": 8, "tags": [ "normal" ] @@ -53,7 +53,7 @@ "id": "006-mixed-values", "input": "tests/006-mixed-values.in", "expected": "tests/006-mixed-values.out", - "points": 10, + "points": 4, "tags": [ "regression" ] @@ -62,28 +62,129 @@ "id": "007-scale", "input": "tests/007-scale.in", "expected": "tests/007-scale.out", - "points": 2, + "points": 4, "tags": [ "scale", "complexity" ] }, - {"id":"008-descending-restore","input":"tests/008-descending-restore.in","expected":"tests/008-descending-restore.out","points":1,"tags":["normal"]}, - {"id":"009-read-only-queries","input":"tests/009-read-only-queries.in","expected":"tests/009-read-only-queries.out","points":1,"tags":["regression"]}, - {"id":"010-empty-interleave","input":"tests/010-empty-interleave.in","expected":"tests/010-empty-interleave.out","points":1,"tags":["boundary","error"]}, - {"id":"011-int64-values","input":"tests/011-int64-values.in","expected":"tests/011-int64-values.out","points":1,"tags":["boundary"]}, - {"id":"012-zero-positive","input":"tests/012-zero-positive.in","expected":"tests/012-zero-positive.out","points":1,"tags":["normal"]}, - {"id":"013-minimum-churn","input":"tests/013-minimum-churn.in","expected":"tests/013-minimum-churn.out","points":1,"tags":["regression"]}, - {"id":"014-size-tracking","input":"tests/014-size-tracking.in","expected":"tests/014-size-tracking.out","points":1,"tags":["normal"]}, - {"id":"015-single-cycle","input":"tests/015-single-cycle.in","expected":"tests/015-single-cycle.out","points":1,"tags":["boundary"]}, - {"id":"016-pop-and-min","input":"tests/016-pop-and-min.in","expected":"tests/016-pop-and-min.out","points":1,"tags":["normal"]}, - {"id":"017-extreme-values","input":"tests/017-extreme-values.in","expected":"tests/017-extreme-values.out","points":1,"tags":["boundary"]}, - {"id":"018-minimum-restoration","input":"tests/018-minimum-restoration.in","expected":"tests/018-minimum-restoration.out","points":1,"tags":["regression"]}, - {"id":"019-zero-cycle","input":"tests/019-zero-cycle.in","expected":"tests/019-zero-cycle.out","points":1,"tags":["boundary"]}, - {"id":"020-empty-and-negative","input":"tests/020-empty-and-negative.in","expected":"tests/020-empty-and-negative.out","points":1,"tags":["boundary","error"]}, - {"id":"021-duplicate-churn","input":"tests/021-duplicate-churn.in","expected":"tests/021-duplicate-churn.out","points":1,"tags":["regression"]}, - {"id":"022-transfer-like-sequence","input":"tests/022-transfer-like-sequence.in","expected":"tests/022-transfer-like-sequence.out","points":1,"tags":["normal"]}, - {"id":"023-size-after-failures","input":"tests/023-size-after-failures.in","expected":"tests/023-size-after-failures.out","points":1,"tags":["error"]}, - {"id":"024-large-positive","input":"tests/024-large-positive.in","expected":"tests/024-large-positive.out","points":1,"tags":["boundary"]}, - {"id":"025-long-minimum-chain","input":"tests/025-long-minimum-chain.in","expected":"tests/025-long-minimum-chain.out","points":1,"tags":["normal"]} + { + "id": "008-descending-restore", + "input": "tests/008-descending-restore.in", + "expected": "tests/008-descending-restore.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-read-only-queries", + "input": "tests/009-read-only-queries.in", + "expected": "tests/009-read-only-queries.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "010-empty-interleave", + "input": "tests/010-empty-interleave.in", + "expected": "tests/010-empty-interleave.out", + "points": 4, + "tags": [ + "boundary", + "error" + ] + }, + { + "id": "011-int64-values", + "input": "tests/011-int64-values.in", + "expected": "tests/011-int64-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "012-zero-positive", + "input": "tests/012-zero-positive.in", + "expected": "tests/012-zero-positive.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-minimum-churn", + "input": "tests/013-minimum-churn.in", + "expected": "tests/013-minimum-churn.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "014-size-tracking", + "input": "tests/014-size-tracking.in", + "expected": "tests/014-size-tracking.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-single-cycle", + "input": "tests/015-single-cycle.in", + "expected": "tests/015-single-cycle.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "016-pop-and-min", + "input": "tests/016-pop-and-min.in", + "expected": "tests/016-pop-and-min.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "017-extreme-values", + "input": "tests/017-extreme-values.in", + "expected": "tests/017-extreme-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "018-minimum-restoration", + "input": "tests/018-minimum-restoration.in", + "expected": "tests/018-minimum-restoration.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-zero-cycle", + "input": "tests/019-zero-cycle.in", + "expected": "tests/019-zero-cycle.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "020-empty-and-negative", + "input": "tests/020-empty-and-negative.in", + "expected": "tests/020-empty-and-negative.out", + "points": 4, + "tags": [ + "boundary", + "error" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/021-regular-spacing.in b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/021-regular-spacing.in deleted file mode 100644 index e666ebf2..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/021-regular-spacing.in +++ /dev/null @@ -1,2 +0,0 @@ -7 -7 1007 2007 3007 4007 5007 6007 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/021-regular-spacing.out b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/021-regular-spacing.out deleted file mode 100644 index c9c9d53b..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/021-regular-spacing.out +++ /dev/null @@ -1 +0,0 @@ -1 2 3 4 4 4 4 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/022-long-gap.in b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/022-long-gap.in deleted file mode 100644 index 0fd3c4e8..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/022-long-gap.in +++ /dev/null @@ -1,2 +0,0 @@ -6 -1 2 10000 10001 13000 13001 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/022-long-gap.out b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/022-long-gap.out deleted file mode 100644 index e66def97..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/022-long-gap.out +++ /dev/null @@ -1 +0,0 @@ -1 2 1 2 3 3 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/023-large-timestamps.in b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/023-large-timestamps.in deleted file mode 100644 index b4ba44ab..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/023-large-timestamps.in +++ /dev/null @@ -1,2 +0,0 @@ -3 -123456789012345 123456789015345 123456789015346 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/024-boundary-reentry.in b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/024-boundary-reentry.in deleted file mode 100644 index 4ba36b74..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/024-boundary-reentry.in +++ /dev/null @@ -1,2 +0,0 @@ -6 -500 3500 3501 6500 6501 9501 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/024-boundary-reentry.out b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/024-boundary-reentry.out deleted file mode 100644 index cc37ad67..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/024-boundary-reentry.out +++ /dev/null @@ -1 +0,0 @@ -1 2 2 3 3 2 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/025-stepped-expiry.in b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/025-stepped-expiry.in deleted file mode 100644 index 68661de4..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/025-stepped-expiry.in +++ /dev/null @@ -1,2 +0,0 @@ -5 -0 2999 5998 8997 11996 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/025-stepped-expiry.out b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/025-stepped-expiry.out deleted file mode 100644 index 1af3cd67..00000000 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/025-stepped-expiry.out +++ /dev/null @@ -1 +0,0 @@ -1 2 2 2 2 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/cases.json b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/cases.json index 2f697374..80d3f3b0 100644 --- a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-03-recent-counter/tests/cases.json @@ -3,7 +3,7 @@ "id": "001-sample", "input": "tests/001-sample.in", "expected": "tests/001-sample.out", - "points": 5, + "points": 8, "tags": [ "sample" ], @@ -15,7 +15,7 @@ "id": "002-inclusive-boundary", "input": "tests/002-inclusive-boundary.in", "expected": "tests/002-inclusive-boundary.out", - "points": 15, + "points": 8, "tags": [ "boundary" ] @@ -24,7 +24,7 @@ "id": "003-sparse", "input": "tests/003-sparse.in", "expected": "tests/003-sparse.out", - "points": 20, + "points": 8, "tags": [ "normal" ] @@ -33,7 +33,7 @@ "id": "004-dense", "input": "tests/004-dense.in", "expected": "tests/004-dense.out", - "points": 20, + "points": 8, "tags": [ "normal" ] @@ -42,7 +42,7 @@ "id": "005-large-time", "input": "tests/005-large-time.in", "expected": "tests/005-large-time.out", - "points": 20, + "points": 8, "tags": [ "boundary" ] @@ -51,29 +51,137 @@ "id": "006-scale", "input": "tests/006-scale.in", "expected": "tests/006-scale.out", - "points": 1, + "points": 4, "tags": [ "scale", "complexity" ] }, - {"id":"007-single","input":"tests/007-single.in","expected":"tests/007-single.out","points":1,"tags":["boundary"]}, - {"id":"008-all-within","input":"tests/008-all-within.in","expected":"tests/008-all-within.out","points":1,"tags":["normal"]}, - {"id":"009-boundary-repeated","input":"tests/009-boundary-repeated.in","expected":"tests/009-boundary-repeated.out","points":1,"tags":["boundary","regression"]}, - {"id":"010-large-gaps","input":"tests/010-large-gaps.in","expected":"tests/010-large-gaps.out","points":1,"tags":["normal"]}, - {"id":"011-burst-and-gap","input":"tests/011-burst-and-gap.in","expected":"tests/011-burst-and-gap.out","points":1,"tags":["normal"]}, - {"id":"012-near-zero","input":"tests/012-near-zero.in","expected":"tests/012-near-zero.out","points":1,"tags":["boundary"]}, - {"id":"013-max-timestamp","input":"tests/013-max-timestamp.in","expected":"tests/013-max-timestamp.out","points":1,"tags":["boundary"]}, - {"id":"014-steady-spacing","input":"tests/014-steady-spacing.in","expected":"tests/014-steady-spacing.out","points":1,"tags":["normal"]}, - {"id":"015-window-expiry","input":"tests/015-window-expiry.in","expected":"tests/015-window-expiry.out","points":1,"tags":["regression"]}, - {"id":"016-exact-boundaries","input":"tests/016-exact-boundaries.in","expected":"tests/016-exact-boundaries.out","points":1,"tags":["boundary"]}, - {"id":"017-two-bursts","input":"tests/017-two-bursts.in","expected":"tests/017-two-bursts.out","points":1,"tags":["normal"]}, - {"id":"018-near-zero-window","input":"tests/018-near-zero-window.in","expected":"tests/018-near-zero-window.out","points":1,"tags":["boundary"]}, - {"id":"019-repeated-expiry","input":"tests/019-repeated-expiry.in","expected":"tests/019-repeated-expiry.out","points":1,"tags":["regression"]}, - {"id":"020-max-time-range","input":"tests/020-max-time-range.in","expected":"tests/020-max-time-range.out","points":1,"tags":["boundary"]}, - {"id":"021-regular-spacing","input":"tests/021-regular-spacing.in","expected":"tests/021-regular-spacing.out","points":1,"tags":["normal"]}, - {"id":"022-long-gap","input":"tests/022-long-gap.in","expected":"tests/022-long-gap.out","points":1,"tags":["normal"]}, - {"id":"023-large-timestamps","input":"tests/023-large-timestamps.in","expected":"tests/023-large-timestamps.out","points":1,"tags":["boundary"]}, - {"id":"024-boundary-reentry","input":"tests/024-boundary-reentry.in","expected":"tests/024-boundary-reentry.out","points":1,"tags":["regression"]}, - {"id":"025-stepped-expiry","input":"tests/025-stepped-expiry.in","expected":"tests/025-stepped-expiry.out","points":1,"tags":["normal"]} + { + "id": "007-single", + "input": "tests/007-single.in", + "expected": "tests/007-single.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "008-all-within", + "input": "tests/008-all-within.in", + "expected": "tests/008-all-within.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-boundary-repeated", + "input": "tests/009-boundary-repeated.in", + "expected": "tests/009-boundary-repeated.out", + "points": 4, + "tags": [ + "boundary", + "regression" + ] + }, + { + "id": "010-large-gaps", + "input": "tests/010-large-gaps.in", + "expected": "tests/010-large-gaps.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-burst-and-gap", + "input": "tests/011-burst-and-gap.in", + "expected": "tests/011-burst-and-gap.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-near-zero", + "input": "tests/012-near-zero.in", + "expected": "tests/012-near-zero.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "013-max-timestamp", + "input": "tests/013-max-timestamp.in", + "expected": "tests/013-max-timestamp.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "014-steady-spacing", + "input": "tests/014-steady-spacing.in", + "expected": "tests/014-steady-spacing.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-window-expiry", + "input": "tests/015-window-expiry.in", + "expected": "tests/015-window-expiry.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "016-exact-boundaries", + "input": "tests/016-exact-boundaries.in", + "expected": "tests/016-exact-boundaries.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "017-two-bursts", + "input": "tests/017-two-bursts.in", + "expected": "tests/017-two-bursts.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "018-near-zero-window", + "input": "tests/018-near-zero-window.in", + "expected": "tests/018-near-zero-window.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "019-repeated-expiry", + "input": "tests/019-repeated-expiry.in", + "expected": "tests/019-repeated-expiry.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-max-time-range", + "input": "tests/020-max-time-range.in", + "expected": "tests/020-max-time-range.out", + "points": 4, + "tags": [ + "boundary" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/021-failed-enqueues.in b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/021-failed-enqueues.in deleted file mode 100644 index ac65c86a..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/021-failed-enqueues.in +++ /dev/null @@ -1,17 +0,0 @@ -2 -15 -ENQUEUE 1 -ENQUEUE 2 -ENQUEUE 3 -FULL -FRONT -REAR -SIZE -DEQUEUE -ENQUEUE 4 -ENQUEUE 5 -FULL -REAR -DEQUEUE -DEQUEUE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/021-failed-enqueues.out b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/021-failed-enqueues.out deleted file mode 100644 index 9d44b90a..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/021-failed-enqueues.out +++ /dev/null @@ -1,15 +0,0 @@ -TRUE -TRUE -FALSE -TRUE -1 -2 -2 -1 -TRUE -FALSE -TRUE -4 -2 -4 -TRUE diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/022-negative-cycle.in b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/022-negative-cycle.in deleted file mode 100644 index 0d1ec927..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/022-negative-cycle.in +++ /dev/null @@ -1,14 +0,0 @@ -3 -12 -ENQUEUE -1 -ENQUEUE -2 -DEQUEUE -ENQUEUE -3 -FRONT -REAR -DEQUEUE -DEQUEUE -ENQUEUE -4 -FRONT -SIZE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/022-negative-cycle.out b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/022-negative-cycle.out deleted file mode 100644 index f2c1c8a9..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/022-negative-cycle.out +++ /dev/null @@ -1,12 +0,0 @@ -TRUE -TRUE --1 -TRUE --2 --3 --2 --3 -TRUE --4 -1 -FALSE diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/023-empty-status.in b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/023-empty-status.in deleted file mode 100644 index 4cc4c06f..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/023-empty-status.in +++ /dev/null @@ -1,13 +0,0 @@ -1 -11 -EMPTY -FULL -FRONT -REAR -DEQUEUE -SIZE -ENQUEUE 0 -FULL -DEQUEUE -EMPTY -FULL diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/023-empty-status.out b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/023-empty-status.out deleted file mode 100644 index 94baab6b..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/023-empty-status.out +++ /dev/null @@ -1,11 +0,0 @@ -TRUE -FALSE -EMPTY -EMPTY -EMPTY -0 -TRUE -TRUE -0 -TRUE -FALSE diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/024-reuse-after-wrap.in b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/024-reuse-after-wrap.in deleted file mode 100644 index 96f277d5..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/024-reuse-after-wrap.in +++ /dev/null @@ -1,14 +0,0 @@ -2 -12 -ENQUEUE 1 -DEQUEUE -ENQUEUE 2 -DEQUEUE -ENQUEUE 3 -DEQUEUE -ENQUEUE 4 -FRONT -REAR -SIZE -DEQUEUE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/024-reuse-after-wrap.out b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/024-reuse-after-wrap.out deleted file mode 100644 index 590854c0..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/024-reuse-after-wrap.out +++ /dev/null @@ -1,12 +0,0 @@ -TRUE -1 -TRUE -2 -TRUE -3 -TRUE -4 -4 -1 -4 -TRUE diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/025-large-values.in b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/025-large-values.in deleted file mode 100644 index 169730dd..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/025-large-values.in +++ /dev/null @@ -1,14 +0,0 @@ -3 -12 -ENQUEUE 1000000000000 -ENQUEUE -1000000000000 -FRONT -REAR -DEQUEUE -ENQUEUE 0 -REAR -SIZE -DEQUEUE -DEQUEUE -EMPTY -FULL diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/025-large-values.out b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/025-large-values.out deleted file mode 100644 index 2d79346d..00000000 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/025-large-values.out +++ /dev/null @@ -1,12 +0,0 @@ -TRUE -TRUE -1000000000000 --1000000000000 -1000000000000 -TRUE -0 -2 --1000000000000 -0 -TRUE -FALSE diff --git a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/cases.json b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/cases.json index 6a25ef14..ddc588f7 100644 --- a/labs/chapter-02/exercise/E-02-04-circular-queue/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-04-circular-queue/tests/cases.json @@ -3,7 +3,7 @@ "id": "001-sample", "input": "tests/001-sample.in", "expected": "tests/001-sample.out", - "points": 10, + "points": 8, "tags": [ "sample" ], @@ -15,7 +15,7 @@ "id": "002-wraparound", "input": "tests/002-wraparound.in", "expected": "tests/002-wraparound.out", - "points": 20, + "points": 8, "tags": [ "boundary" ] @@ -24,7 +24,7 @@ "id": "003-capacity-one", "input": "tests/003-capacity-one.in", "expected": "tests/003-capacity-one.out", - "points": 20, + "points": 8, "tags": [ "boundary" ] @@ -33,7 +33,7 @@ "id": "004-empty-stability", "input": "tests/004-empty-stability.in", "expected": "tests/004-empty-stability.out", - "points": 15, + "points": 8, "tags": [ "error" ] @@ -42,7 +42,7 @@ "id": "005-negative-duplicates", "input": "tests/005-negative-duplicates.in", "expected": "tests/005-negative-duplicates.out", - "points": 15, + "points": 8, "tags": [ "normal", "numeric-boundary" @@ -52,30 +52,139 @@ "id": "006-scale-wraparound", "input": "tests/006-scale-wraparound.in", "expected": "tests/006-scale-wraparound.out", - "points": 1, + "points": 4, "tags": [ "scale", "complexity", "wraparound" ] }, - {"id":"007-fill-and-reject","input":"tests/007-fill-and-reject.in","expected":"tests/007-fill-and-reject.out","points":1,"tags":["boundary","error"]}, - {"id":"008-multi-wrap","input":"tests/008-multi-wrap.in","expected":"tests/008-multi-wrap.out","points":1,"tags":["wraparound","regression"]}, - {"id":"009-capacity-two","input":"tests/009-capacity-two.in","expected":"tests/009-capacity-two.out","points":1,"tags":["boundary"]}, - {"id":"010-int64-values","input":"tests/010-int64-values.in","expected":"tests/010-int64-values.out","points":1,"tags":["boundary"]}, - {"id":"011-empty-repeated","input":"tests/011-empty-repeated.in","expected":"tests/011-empty-repeated.out","points":1,"tags":["error"]}, - {"id":"012-peek-stability","input":"tests/012-peek-stability.in","expected":"tests/012-peek-stability.out","points":1,"tags":["regression"]}, - {"id":"013-alternating","input":"tests/013-alternating.in","expected":"tests/013-alternating.out","points":1,"tags":["normal"]}, - {"id":"014-drain-refill","input":"tests/014-drain-refill.in","expected":"tests/014-drain-refill.out","points":1,"tags":["normal"]}, - {"id":"015-status-cycle","input":"tests/015-status-cycle.in","expected":"tests/015-status-cycle.out","points":1,"tags":["normal"]}, - {"id":"016-extreme-values","input":"tests/016-extreme-values.in","expected":"tests/016-extreme-values.out","points":1,"tags":["boundary"]}, - {"id":"017-double-wrap","input":"tests/017-double-wrap.in","expected":"tests/017-double-wrap.out","points":1,"tags":["regression"]}, - {"id":"018-repeated-peeks","input":"tests/018-repeated-peeks.in","expected":"tests/018-repeated-peeks.out","points":1,"tags":["normal"]}, - {"id":"019-capacity-three","input":"tests/019-capacity-three.in","expected":"tests/019-capacity-three.out","points":1,"tags":["boundary"]}, - {"id":"020-drain-and-fill","input":"tests/020-drain-and-fill.in","expected":"tests/020-drain-and-fill.out","points":1,"tags":["normal"]}, - {"id":"021-failed-enqueues","input":"tests/021-failed-enqueues.in","expected":"tests/021-failed-enqueues.out","points":1,"tags":["error"]}, - {"id":"022-negative-cycle","input":"tests/022-negative-cycle.in","expected":"tests/022-negative-cycle.out","points":1,"tags":["normal"]}, - {"id":"023-empty-status","input":"tests/023-empty-status.in","expected":"tests/023-empty-status.out","points":1,"tags":["boundary"]}, - {"id":"024-reuse-after-wrap","input":"tests/024-reuse-after-wrap.in","expected":"tests/024-reuse-after-wrap.out","points":1,"tags":["regression"]}, - {"id":"025-large-values","input":"tests/025-large-values.in","expected":"tests/025-large-values.out","points":1,"tags":["boundary"]} + { + "id": "007-fill-and-reject", + "input": "tests/007-fill-and-reject.in", + "expected": "tests/007-fill-and-reject.out", + "points": 4, + "tags": [ + "boundary", + "error" + ] + }, + { + "id": "008-multi-wrap", + "input": "tests/008-multi-wrap.in", + "expected": "tests/008-multi-wrap.out", + "points": 4, + "tags": [ + "wraparound", + "regression" + ] + }, + { + "id": "009-capacity-two", + "input": "tests/009-capacity-two.in", + "expected": "tests/009-capacity-two.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "010-int64-values", + "input": "tests/010-int64-values.in", + "expected": "tests/010-int64-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "011-empty-repeated", + "input": "tests/011-empty-repeated.in", + "expected": "tests/011-empty-repeated.out", + "points": 4, + "tags": [ + "error" + ] + }, + { + "id": "012-peek-stability", + "input": "tests/012-peek-stability.in", + "expected": "tests/012-peek-stability.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "013-alternating", + "input": "tests/013-alternating.in", + "expected": "tests/013-alternating.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-drain-refill", + "input": "tests/014-drain-refill.in", + "expected": "tests/014-drain-refill.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-status-cycle", + "input": "tests/015-status-cycle.in", + "expected": "tests/015-status-cycle.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-extreme-values", + "input": "tests/016-extreme-values.in", + "expected": "tests/016-extreme-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "017-double-wrap", + "input": "tests/017-double-wrap.in", + "expected": "tests/017-double-wrap.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-repeated-peeks", + "input": "tests/018-repeated-peeks.in", + "expected": "tests/018-repeated-peeks.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "019-capacity-three", + "input": "tests/019-capacity-three.in", + "expected": "tests/019-capacity-three.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "020-drain-and-fill", + "input": "tests/020-drain-and-fill.in", + "expected": "tests/020-drain-and-fill.out", + "points": 4, + "tags": [ + "normal" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/021-size-tracking.in b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/021-size-tracking.in deleted file mode 100644 index 53f7a82f..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/021-size-tracking.in +++ /dev/null @@ -1,13 +0,0 @@ -12 -EMPTY -SIZE -ENQUEUE 7 -SIZE -ENQUEUE 8 -SIZE -DEQUEUE -SIZE -DEQUEUE -SIZE -EMPTY -FRONT diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/021-size-tracking.out b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/021-size-tracking.out deleted file mode 100644 index 5a414517..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/021-size-tracking.out +++ /dev/null @@ -1,10 +0,0 @@ -YES -0 -1 -2 -7 -1 -8 -0 -YES -EMPTY diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/022-interleaved-transfers.in b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/022-interleaved-transfers.in deleted file mode 100644 index 85acf200..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/022-interleaved-transfers.in +++ /dev/null @@ -1,14 +0,0 @@ -13 -ENQUEUE 1 -ENQUEUE 2 -DEQUEUE -ENQUEUE 3 -ENQUEUE 4 -FRONT -DEQUEUE -DEQUEUE -ENQUEUE 5 -FRONT -DEQUEUE -DEQUEUE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/022-interleaved-transfers.out b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/022-interleaved-transfers.out deleted file mode 100644 index 8cf83a24..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/022-interleaved-transfers.out +++ /dev/null @@ -1,8 +0,0 @@ -1 -2 -2 -3 -4 -4 -5 -YES diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/023-singleton-repeated.in b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/023-singleton-repeated.in deleted file mode 100644 index 74f032a8..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/023-singleton-repeated.in +++ /dev/null @@ -1,10 +0,0 @@ -9 -ENQUEUE 9 -FRONT -DEQUEUE -FRONT -ENQUEUE 10 -SIZE -DEQUEUE -EMPTY -DEQUEUE diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/023-singleton-repeated.out b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/023-singleton-repeated.out deleted file mode 100644 index 5c6bc0b6..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/023-singleton-repeated.out +++ /dev/null @@ -1,7 +0,0 @@ -9 -9 -EMPTY -1 -10 -YES -EMPTY diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/024-large-values.in b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/024-large-values.in deleted file mode 100644 index 1e33681b..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/024-large-values.in +++ /dev/null @@ -1,12 +0,0 @@ -11 -ENQUEUE 1000000000000 -ENQUEUE -1000000000000 -FRONT -DEQUEUE -FRONT -DEQUEUE -EMPTY -SIZE -ENQUEUE 0 -DEQUEUE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/024-large-values.out b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/024-large-values.out deleted file mode 100644 index aeff9c3c..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/024-large-values.out +++ /dev/null @@ -1,8 +0,0 @@ -1000000000000 -1000000000000 --1000000000000 --1000000000000 -YES -0 -0 -YES diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/025-front-stability.in b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/025-front-stability.in deleted file mode 100644 index 57138716..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/025-front-stability.in +++ /dev/null @@ -1,12 +0,0 @@ -11 -ENQUEUE 4 -ENQUEUE 5 -FRONT -FRONT -DEQUEUE -FRONT -DEQUEUE -FRONT -EMPTY -SIZE -DEQUEUE diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/025-front-stability.out b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/025-front-stability.out deleted file mode 100644 index ff160232..00000000 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/025-front-stability.out +++ /dev/null @@ -1,9 +0,0 @@ -4 -4 -4 -5 -5 -EMPTY -YES -0 -EMPTY diff --git a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/cases.json b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/cases.json index 3b86635e..f2c6e85e 100644 --- a/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-05-queue-using-stacks/tests/cases.json @@ -3,7 +3,7 @@ "id": "001-lazy-transfer", "input": "tests/001-lazy-transfer.in", "expected": "tests/001-lazy-transfer.out", - "points": 10, + "points": 8, "tags": [ "sample" ], @@ -15,7 +15,7 @@ "id": "002-empty", "input": "tests/002-empty.in", "expected": "tests/002-empty.out", - "points": 20, + "points": 8, "tags": [ "boundary", "error" @@ -25,7 +25,7 @@ "id": "003-multiple-transfers", "input": "tests/003-multiple-transfers.in", "expected": "tests/003-multiple-transfers.out", - "points": 20, + "points": 8, "tags": [ "regression" ] @@ -34,7 +34,7 @@ "id": "004-duplicates", "input": "tests/004-duplicates.in", "expected": "tests/004-duplicates.out", - "points": 15, + "points": 8, "tags": [ "normal" ] @@ -43,7 +43,7 @@ "id": "005-alternating", "input": "tests/005-alternating.in", "expected": "tests/005-alternating.out", - "points": 15, + "points": 8, "tags": [ "normal", "numeric-boundary" @@ -53,30 +53,139 @@ "id": "006-scale-transfer", "input": "tests/006-scale-transfer.in", "expected": "tests/006-scale-transfer.out", - "points": 1, + "points": 4, "tags": [ "scale", "complexity", "amortized" ] }, - {"id":"007-single","input":"tests/007-single.in","expected":"tests/007-single.out","points":1,"tags":["boundary"]}, - {"id":"008-front-repeat","input":"tests/008-front-repeat.in","expected":"tests/008-front-repeat.out","points":1,"tags":["regression"]}, - {"id":"009-drain-refill","input":"tests/009-drain-refill.in","expected":"tests/009-drain-refill.out","points":1,"tags":["normal"]}, - {"id":"010-empty-interleave","input":"tests/010-empty-interleave.in","expected":"tests/010-empty-interleave.out","points":1,"tags":["boundary","error"]}, - {"id":"011-negative-values","input":"tests/011-negative-values.in","expected":"tests/011-negative-values.out","points":1,"tags":["normal"]}, - {"id":"012-transfer-chain","input":"tests/012-transfer-chain.in","expected":"tests/012-transfer-chain.out","points":1,"tags":["regression"]}, - {"id":"013-size-empty","input":"tests/013-size-empty.in","expected":"tests/013-size-empty.out","points":1,"tags":["normal"]}, - {"id":"014-int64-values","input":"tests/014-int64-values.in","expected":"tests/014-int64-values.out","points":1,"tags":["boundary"]}, - {"id":"015-alternating","input":"tests/015-alternating.in","expected":"tests/015-alternating.out","points":1,"tags":["normal"]}, - {"id":"016-extreme-values","input":"tests/016-extreme-values.in","expected":"tests/016-extreme-values.out","points":1,"tags":["boundary"]}, - {"id":"017-front-before-transfer","input":"tests/017-front-before-transfer.in","expected":"tests/017-front-before-transfer.out","points":1,"tags":["normal"]}, - {"id":"018-multiple-drains","input":"tests/018-multiple-drains.in","expected":"tests/018-multiple-drains.out","points":1,"tags":["regression"]}, - {"id":"019-negative-cycle","input":"tests/019-negative-cycle.in","expected":"tests/019-negative-cycle.out","points":1,"tags":["normal"]}, - {"id":"020-empty-recovery","input":"tests/020-empty-recovery.in","expected":"tests/020-empty-recovery.out","points":1,"tags":["boundary","error"]}, - {"id":"021-size-tracking","input":"tests/021-size-tracking.in","expected":"tests/021-size-tracking.out","points":1,"tags":["normal"]}, - {"id":"022-interleaved-transfers","input":"tests/022-interleaved-transfers.in","expected":"tests/022-interleaved-transfers.out","points":1,"tags":["regression"]}, - {"id":"023-singleton-repeated","input":"tests/023-singleton-repeated.in","expected":"tests/023-singleton-repeated.out","points":1,"tags":["boundary"]}, - {"id":"024-large-values","input":"tests/024-large-values.in","expected":"tests/024-large-values.out","points":1,"tags":["boundary"]}, - {"id":"025-front-stability","input":"tests/025-front-stability.in","expected":"tests/025-front-stability.out","points":1,"tags":["regression"]} + { + "id": "007-single", + "input": "tests/007-single.in", + "expected": "tests/007-single.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "008-front-repeat", + "input": "tests/008-front-repeat.in", + "expected": "tests/008-front-repeat.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "009-drain-refill", + "input": "tests/009-drain-refill.in", + "expected": "tests/009-drain-refill.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-empty-interleave", + "input": "tests/010-empty-interleave.in", + "expected": "tests/010-empty-interleave.out", + "points": 4, + "tags": [ + "boundary", + "error" + ] + }, + { + "id": "011-negative-values", + "input": "tests/011-negative-values.in", + "expected": "tests/011-negative-values.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-transfer-chain", + "input": "tests/012-transfer-chain.in", + "expected": "tests/012-transfer-chain.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "013-size-empty", + "input": "tests/013-size-empty.in", + "expected": "tests/013-size-empty.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-int64-values", + "input": "tests/014-int64-values.in", + "expected": "tests/014-int64-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "015-alternating", + "input": "tests/015-alternating.in", + "expected": "tests/015-alternating.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-extreme-values", + "input": "tests/016-extreme-values.in", + "expected": "tests/016-extreme-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "017-front-before-transfer", + "input": "tests/017-front-before-transfer.in", + "expected": "tests/017-front-before-transfer.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "018-multiple-drains", + "input": "tests/018-multiple-drains.in", + "expected": "tests/018-multiple-drains.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-negative-cycle", + "input": "tests/019-negative-cycle.in", + "expected": "tests/019-negative-cycle.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "020-empty-recovery", + "input": "tests/020-empty-recovery.in", + "expected": "tests/020-empty-recovery.out", + "points": 4, + "tags": [ + "boundary", + "error" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/021-failed-inserts.in b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/021-failed-inserts.in deleted file mode 100644 index 59fcfaba..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/021-failed-inserts.in +++ /dev/null @@ -1,17 +0,0 @@ -2 -15 -INSERT_LAST 1 -INSERT_LAST 2 -INSERT_FRONT 3 -FULL -FRONT -REAR -SIZE -DELETE_FRONT -INSERT_FRONT 4 -INSERT_LAST 5 -FULL -REAR -DELETE_LAST -DELETE_FRONT -EMPTY diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/021-failed-inserts.out b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/021-failed-inserts.out deleted file mode 100644 index 8b3a7905..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/021-failed-inserts.out +++ /dev/null @@ -1,15 +0,0 @@ -TRUE -TRUE -FALSE -TRUE -1 -2 -2 -1 -TRUE -FALSE -TRUE -2 -2 -4 -TRUE diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/022-negative-cycle.in b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/022-negative-cycle.in deleted file mode 100644 index eaad5d16..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/022-negative-cycle.in +++ /dev/null @@ -1,14 +0,0 @@ -3 -12 -INSERT_LAST -1 -INSERT_FRONT -2 -DELETE_LAST -INSERT_LAST -3 -FRONT -REAR -DELETE_FRONT -DELETE_LAST -INSERT_FRONT -4 -FRONT -SIZE -EMPTY diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/022-negative-cycle.out b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/022-negative-cycle.out deleted file mode 100644 index f2c1c8a9..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/022-negative-cycle.out +++ /dev/null @@ -1,12 +0,0 @@ -TRUE -TRUE --1 -TRUE --2 --3 --2 --3 -TRUE --4 -1 -FALSE diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/023-empty-status.in b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/023-empty-status.in deleted file mode 100644 index b6a443b6..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/023-empty-status.in +++ /dev/null @@ -1,14 +0,0 @@ -1 -12 -EMPTY -FULL -FRONT -REAR -DELETE_FRONT -DELETE_LAST -SIZE -INSERT_LAST 0 -FULL -DELETE_LAST -EMPTY -FULL diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/023-empty-status.out b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/023-empty-status.out deleted file mode 100644 index 90fb9f83..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/023-empty-status.out +++ /dev/null @@ -1,12 +0,0 @@ -TRUE -FALSE -EMPTY -EMPTY -EMPTY -EMPTY -0 -TRUE -TRUE -0 -TRUE -FALSE diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/024-reuse-after-wrap.in b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/024-reuse-after-wrap.in deleted file mode 100644 index 9b1774a8..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/024-reuse-after-wrap.in +++ /dev/null @@ -1,14 +0,0 @@ -2 -12 -INSERT_LAST 1 -DELETE_FRONT -INSERT_FRONT 2 -DELETE_LAST -INSERT_LAST 3 -DELETE_FRONT -INSERT_FRONT 4 -FRONT -REAR -SIZE -DELETE_LAST -EMPTY diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/024-reuse-after-wrap.out b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/024-reuse-after-wrap.out deleted file mode 100644 index 590854c0..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/024-reuse-after-wrap.out +++ /dev/null @@ -1,12 +0,0 @@ -TRUE -1 -TRUE -2 -TRUE -3 -TRUE -4 -4 -1 -4 -TRUE diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/025-large-values.in b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/025-large-values.in deleted file mode 100644 index 43968d00..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/025-large-values.in +++ /dev/null @@ -1,14 +0,0 @@ -3 -12 -INSERT_FRONT 1000000000000 -INSERT_LAST -1000000000000 -FRONT -REAR -DELETE_FRONT -INSERT_LAST 0 -REAR -SIZE -DELETE_LAST -DELETE_FRONT -EMPTY -FULL diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/025-large-values.out b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/025-large-values.out deleted file mode 100644 index 6b8219a0..00000000 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/025-large-values.out +++ /dev/null @@ -1,12 +0,0 @@ -TRUE -TRUE -1000000000000 --1000000000000 -1000000000000 -TRUE -0 -2 -0 --1000000000000 -TRUE -FALSE diff --git a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/cases.json b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/cases.json index bd1217db..80dd22d3 100644 --- a/labs/chapter-02/exercise/E-02-06-circular-deque/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-06-circular-deque/tests/cases.json @@ -3,7 +3,7 @@ "id": "001-sample", "input": "tests/001-sample.in", "expected": "tests/001-sample.out", - "points": 10, + "points": 8, "tags": [ "sample" ], @@ -15,7 +15,7 @@ "id": "002-bidirectional-wrap", "input": "tests/002-bidirectional-wrap.in", "expected": "tests/002-bidirectional-wrap.out", - "points": 20, + "points": 8, "tags": [ "boundary" ] @@ -24,7 +24,7 @@ "id": "003-capacity-one", "input": "tests/003-capacity-one.in", "expected": "tests/003-capacity-one.out", - "points": 20, + "points": 8, "tags": [ "boundary" ] @@ -33,7 +33,7 @@ "id": "004-empty-and-full-stability", "input": "tests/004-empty-and-full-stability.in", "expected": "tests/004-empty-and-full-stability.out", - "points": 15, + "points": 8, "tags": [ "error" ] @@ -42,7 +42,7 @@ "id": "005-alternating", "input": "tests/005-alternating.in", "expected": "tests/005-alternating.out", - "points": 15, + "points": 8, "tags": [ "normal", "numeric-boundary" @@ -52,30 +52,138 @@ "id": "006-scale-wraparound", "input": "tests/006-scale-wraparound.in", "expected": "tests/006-scale-wraparound.out", - "points": 1, + "points": 4, "tags": [ "scale", "complexity", "wraparound" ] }, - {"id":"007-fill-and-reject","input":"tests/007-fill-and-reject.in","expected":"tests/007-fill-and-reject.out","points":1,"tags":["boundary","error"]}, - {"id":"008-front-only-wrap","input":"tests/008-front-only-wrap.in","expected":"tests/008-front-only-wrap.out","points":1,"tags":["wraparound"]}, - {"id":"009-last-only-wrap","input":"tests/009-last-only-wrap.in","expected":"tests/009-last-only-wrap.out","points":1,"tags":["wraparound"]}, - {"id":"010-capacity-two","input":"tests/010-capacity-two.in","expected":"tests/010-capacity-two.out","points":1,"tags":["boundary"]}, - {"id":"011-int64-values","input":"tests/011-int64-values.in","expected":"tests/011-int64-values.out","points":1,"tags":["boundary"]}, - {"id":"012-empty-repeated","input":"tests/012-empty-repeated.in","expected":"tests/012-empty-repeated.out","points":1,"tags":["error"]}, - {"id":"013-peek-stability","input":"tests/013-peek-stability.in","expected":"tests/013-peek-stability.out","points":1,"tags":["regression"]}, - {"id":"014-drain-refill","input":"tests/014-drain-refill.in","expected":"tests/014-drain-refill.out","points":1,"tags":["normal"]}, - {"id":"015-alternating","input":"tests/015-alternating.in","expected":"tests/015-alternating.out","points":1,"tags":["normal"]}, - {"id":"016-extreme-values","input":"tests/016-extreme-values.in","expected":"tests/016-extreme-values.out","points":1,"tags":["boundary"]}, - {"id":"017-double-wrap","input":"tests/017-double-wrap.in","expected":"tests/017-double-wrap.out","points":1,"tags":["regression"]}, - {"id":"018-front-last-mix","input":"tests/018-front-last-mix.in","expected":"tests/018-front-last-mix.out","points":1,"tags":["normal"]}, - {"id":"019-capacity-three","input":"tests/019-capacity-three.in","expected":"tests/019-capacity-three.out","points":1,"tags":["boundary"]}, - {"id":"020-drain-and-fill","input":"tests/020-drain-and-fill.in","expected":"tests/020-drain-and-fill.out","points":1,"tags":["normal"]}, - {"id":"021-failed-inserts","input":"tests/021-failed-inserts.in","expected":"tests/021-failed-inserts.out","points":1,"tags":["error"]}, - {"id":"022-negative-cycle","input":"tests/022-negative-cycle.in","expected":"tests/022-negative-cycle.out","points":1,"tags":["normal"]}, - {"id":"023-empty-status","input":"tests/023-empty-status.in","expected":"tests/023-empty-status.out","points":1,"tags":["boundary"]}, - {"id":"024-reuse-after-wrap","input":"tests/024-reuse-after-wrap.in","expected":"tests/024-reuse-after-wrap.out","points":1,"tags":["regression"]}, - {"id":"025-large-values","input":"tests/025-large-values.in","expected":"tests/025-large-values.out","points":1,"tags":["boundary"]} + { + "id": "007-fill-and-reject", + "input": "tests/007-fill-and-reject.in", + "expected": "tests/007-fill-and-reject.out", + "points": 4, + "tags": [ + "boundary", + "error" + ] + }, + { + "id": "008-front-only-wrap", + "input": "tests/008-front-only-wrap.in", + "expected": "tests/008-front-only-wrap.out", + "points": 4, + "tags": [ + "wraparound" + ] + }, + { + "id": "009-last-only-wrap", + "input": "tests/009-last-only-wrap.in", + "expected": "tests/009-last-only-wrap.out", + "points": 4, + "tags": [ + "wraparound" + ] + }, + { + "id": "010-capacity-two", + "input": "tests/010-capacity-two.in", + "expected": "tests/010-capacity-two.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "011-int64-values", + "input": "tests/011-int64-values.in", + "expected": "tests/011-int64-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "012-empty-repeated", + "input": "tests/012-empty-repeated.in", + "expected": "tests/012-empty-repeated.out", + "points": 4, + "tags": [ + "error" + ] + }, + { + "id": "013-peek-stability", + "input": "tests/013-peek-stability.in", + "expected": "tests/013-peek-stability.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "014-drain-refill", + "input": "tests/014-drain-refill.in", + "expected": "tests/014-drain-refill.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-alternating", + "input": "tests/015-alternating.in", + "expected": "tests/015-alternating.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-extreme-values", + "input": "tests/016-extreme-values.in", + "expected": "tests/016-extreme-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "017-double-wrap", + "input": "tests/017-double-wrap.in", + "expected": "tests/017-double-wrap.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-front-last-mix", + "input": "tests/018-front-last-mix.in", + "expected": "tests/018-front-last-mix.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "019-capacity-three", + "input": "tests/019-capacity-three.in", + "expected": "tests/019-capacity-three.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "020-drain-and-fill", + "input": "tests/020-drain-and-fill.in", + "expected": "tests/020-drain-and-fill.out", + "points": 4, + "tags": [ + "normal" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/021-single-element.out b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/021-single-element.out deleted file mode 100644 index d81cc071..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/021-single-element.out +++ /dev/null @@ -1 +0,0 @@ -42 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/022-wide-mixed.in b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/022-wide-mixed.in deleted file mode 100644 index 8f246f1a..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/022-wide-mixed.in +++ /dev/null @@ -1,3 +0,0 @@ -9 -5 -8 -3 5 1 9 -2 6 0 4 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/022-wide-mixed.out b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/022-wide-mixed.out deleted file mode 100644 index e4dbcfd0..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/022-wide-mixed.out +++ /dev/null @@ -1 +0,0 @@ -9 9 9 9 9 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/023-repeated-maxima.in b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/023-repeated-maxima.in deleted file mode 100644 index 6acb1739..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/023-repeated-maxima.in +++ /dev/null @@ -1,3 +0,0 @@ -8 -4 -2 2 1 2 2 0 2 2 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/023-repeated-maxima.out b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/023-repeated-maxima.out deleted file mode 100644 index fd4deaa8..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/023-repeated-maxima.out +++ /dev/null @@ -1 +0,0 @@ -2 2 2 2 2 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/024-large-range.in b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/024-large-range.in deleted file mode 100644 index b95927f7..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/024-large-range.in +++ /dev/null @@ -1,3 +0,0 @@ -5 -2 --1000000000 1000000000 -999999999 999999999 0 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/024-large-range.out b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/024-large-range.out deleted file mode 100644 index 38c1b79f..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/024-large-range.out +++ /dev/null @@ -1 +0,0 @@ -1000000000 1000000000 999999999 999999999 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/025-decreasing-window.in b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/025-decreasing-window.in deleted file mode 100644 index be00a407..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/025-decreasing-window.in +++ /dev/null @@ -1,3 +0,0 @@ -9 -5 -9 8 7 6 5 4 3 2 1 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/025-decreasing-window.out b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/025-decreasing-window.out deleted file mode 100644 index 721f9fd5..00000000 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/025-decreasing-window.out +++ /dev/null @@ -1 +0,0 @@ -9 8 7 6 5 diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/cases.json b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/cases.json index 4637b8a5..50cdcaa4 100644 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/cases.json @@ -3,8 +3,10 @@ "id": "001-sample", "input": "tests/001-sample.in", "expected": "tests/001-sample.out", - "points": 5, - "tags": ["sample"], + "points": 8, + "tags": [ + "sample" + ], "compare": { "mode": "exact" } @@ -13,66 +15,172 @@ "id": "002-window-one", "input": "tests/002-window-one.in", "expected": "tests/002-window-one.out", - "points": 15, - "tags": ["boundary"] + "points": 8, + "tags": [ + "boundary" + ] }, { "id": "003-window-all", "input": "tests/003-window-all.in", "expected": "tests/003-window-all.out", - "points": 15, - "tags": ["boundary"] + "points": 8, + "tags": [ + "boundary" + ] }, { "id": "004-increasing", "input": "tests/004-increasing.in", "expected": "tests/004-increasing.out", - "points": 10, - "tags": ["normal"] + "points": 8, + "tags": [ + "normal" + ] }, { "id": "005-decreasing", "input": "tests/005-decreasing.in", "expected": "tests/005-decreasing.out", - "points": 15, - "tags": ["normal"] + "points": 8, + "tags": [ + "normal" + ] }, { "id": "006-duplicate-maxima", "input": "tests/006-duplicate-maxima.in", "expected": "tests/006-duplicate-maxima.out", - "points": 10, - "tags": ["regression"] + "points": 4, + "tags": [ + "regression" + ] }, { "id": "007-negative-values", "input": "tests/007-negative-values.in", "expected": "tests/007-negative-values.out", - "points": 10, - "tags": ["boundary"] + "points": 4, + "tags": [ + "boundary" + ] }, { "id": "008-scale", "input": "tests/008-scale.in", "expected": "tests/008-scale.out", - "points": 3, - "tags": ["scale", "complexity"] - }, - {"id":"009-window-two","input":"tests/009-window-two.in","expected":"tests/009-window-two.out","points":1,"tags":["normal"]}, - {"id":"010-all-equal","input":"tests/010-all-equal.in","expected":"tests/010-all-equal.out","points":1,"tags":["regression"]}, - {"id":"011-large-values","input":"tests/011-large-values.in","expected":"tests/011-large-values.out","points":1,"tags":["boundary"]}, - {"id":"012-negative-decreasing","input":"tests/012-negative-decreasing.in","expected":"tests/012-negative-decreasing.out","points":1,"tags":["normal"]}, - {"id":"013-window-near-all","input":"tests/013-window-near-all.in","expected":"tests/013-window-near-all.out","points":1,"tags":["boundary"]}, - {"id":"014-plateaus","input":"tests/014-plateaus.in","expected":"tests/014-plateaus.out","points":1,"tags":["regression"]}, - {"id":"015-mixed-signs","input":"tests/015-mixed-signs.in","expected":"tests/015-mixed-signs.out","points":1,"tags":["normal"]}, - {"id":"016-window-two","input":"tests/016-window-two.in","expected":"tests/016-window-two.out","points":1,"tags":["normal"]}, - {"id":"017-window-near-all","input":"tests/017-window-near-all.in","expected":"tests/017-window-near-all.out","points":1,"tags":["boundary"]}, - {"id":"018-extreme-values","input":"tests/018-extreme-values.in","expected":"tests/018-extreme-values.out","points":1,"tags":["boundary"]}, - {"id":"019-alternating-peaks","input":"tests/019-alternating-peaks.in","expected":"tests/019-alternating-peaks.out","points":1,"tags":["regression"]}, - {"id":"020-negative-plateaus","input":"tests/020-negative-plateaus.in","expected":"tests/020-negative-plateaus.out","points":1,"tags":["normal"]}, - {"id":"021-single-element","input":"tests/021-single-element.in","expected":"tests/021-single-element.out","points":1,"tags":["boundary"]}, - {"id":"022-wide-mixed","input":"tests/022-wide-mixed.in","expected":"tests/022-wide-mixed.out","points":1,"tags":["normal"]}, - {"id":"023-repeated-maxima","input":"tests/023-repeated-maxima.in","expected":"tests/023-repeated-maxima.out","points":1,"tags":["regression"]}, - {"id":"024-large-range","input":"tests/024-large-range.in","expected":"tests/024-large-range.out","points":1,"tags":["boundary"]}, - {"id":"025-decreasing-window","input":"tests/025-decreasing-window.in","expected":"tests/025-decreasing-window.out","points":1,"tags":["normal"]} + "points": 4, + "tags": [ + "scale", + "complexity" + ] + }, + { + "id": "009-window-two", + "input": "tests/009-window-two.in", + "expected": "tests/009-window-two.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-all-equal", + "input": "tests/010-all-equal.in", + "expected": "tests/010-all-equal.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "011-large-values", + "input": "tests/011-large-values.in", + "expected": "tests/011-large-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "012-negative-decreasing", + "input": "tests/012-negative-decreasing.in", + "expected": "tests/012-negative-decreasing.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-window-near-all", + "input": "tests/013-window-near-all.in", + "expected": "tests/013-window-near-all.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "014-plateaus", + "input": "tests/014-plateaus.in", + "expected": "tests/014-plateaus.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "015-mixed-signs", + "input": "tests/015-mixed-signs.in", + "expected": "tests/015-mixed-signs.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-window-two", + "input": "tests/016-window-two.in", + "expected": "tests/016-window-two.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "017-window-near-all", + "input": "tests/017-window-near-all.in", + "expected": "tests/017-window-near-all.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "018-extreme-values", + "input": "tests/018-extreme-values.in", + "expected": "tests/018-extreme-values.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "019-alternating-peaks", + "input": "tests/019-alternating-peaks.in", + "expected": "tests/019-alternating-peaks.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-negative-plateaus", + "input": "tests/020-negative-plateaus.in", + "expected": "tests/020-negative-plateaus.out", + "points": 4, + "tags": [ + "normal" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/021-large-flat.in b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/021-large-flat.in deleted file mode 100644 index 8416d6a4..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/021-large-flat.in +++ /dev/null @@ -1,2 +0,0 @@ -5 -1000000000 1000000000 1000000000 1000000000 1000000000 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/021-large-flat.out b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/021-large-flat.out deleted file mode 100644 index d007f6b2..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/021-large-flat.out +++ /dev/null @@ -1 +0,0 @@ -5000000000 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/022-alternating-heights.in b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/022-alternating-heights.in deleted file mode 100644 index d4cb2953..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/022-alternating-heights.in +++ /dev/null @@ -1,2 +0,0 @@ -7 -1 3 1 3 1 3 1 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/023-double-plateau.in b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/023-double-plateau.in deleted file mode 100644 index 15af0dc4..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/023-double-plateau.in +++ /dev/null @@ -1,2 +0,0 @@ -8 -4 4 1 4 4 2 4 4 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/024-zero-edges.in b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/024-zero-edges.in deleted file mode 100644 index cb250182..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/024-zero-edges.in +++ /dev/null @@ -1,2 +0,0 @@ -8 -0 2 2 2 0 5 5 0 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/024-zero-edges.out b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/024-zero-edges.out deleted file mode 100644 index f599e28b..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/024-zero-edges.out +++ /dev/null @@ -1 +0,0 @@ -10 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/025-large-mixed.in b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/025-large-mixed.in deleted file mode 100644 index 7d986b2a..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/025-large-mixed.in +++ /dev/null @@ -1,2 +0,0 @@ -6 -1000000000 1 1000000000 1 1000000000 1000000000 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/025-large-mixed.out b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/025-large-mixed.out deleted file mode 100644 index 04b2646e..00000000 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/025-large-mixed.out +++ /dev/null @@ -1 +0,0 @@ -2000000000 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/cases.json b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/cases.json index 307549f7..fdbb3d9f 100644 --- a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/cases.json +++ b/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/cases.json @@ -3,8 +3,10 @@ "id": "001-sample", "input": "tests/001-sample.in", "expected": "tests/001-sample.out", - "points": 5, - "tags": ["sample"], + "points": 8, + "tags": [ + "sample" + ], "compare": { "mode": "exact" } @@ -13,72 +15,172 @@ "id": "002-single-bar", "input": "tests/002-single-bar.in", "expected": "tests/002-single-bar.out", - "points": 10, - "tags": ["boundary"] + "points": 8, + "tags": [ + "boundary" + ] }, { "id": "003-increasing", "input": "tests/003-increasing.in", "expected": "tests/003-increasing.out", - "points": 15, - "tags": ["normal"] + "points": 8, + "tags": [ + "normal" + ] }, { "id": "004-decreasing", "input": "tests/004-decreasing.in", "expected": "tests/004-decreasing.out", - "points": 15, - "tags": ["normal"] + "points": 8, + "tags": [ + "normal" + ] }, { "id": "005-equal-heights", "input": "tests/005-equal-heights.in", "expected": "tests/005-equal-heights.out", - "points": 10, - "tags": ["regression"] + "points": 8, + "tags": [ + "regression" + ] }, { "id": "006-zero-gaps", "input": "tests/006-zero-gaps.in", "expected": "tests/006-zero-gaps.out", - "points": 10, - "tags": ["boundary"] + "points": 4, + "tags": [ + "boundary" + ] }, { "id": "007-valley", "input": "tests/007-valley.in", "expected": "tests/007-valley.out", - "points": 10, - "tags": ["regression"] + "points": 4, + "tags": [ + "regression" + ] }, { "id": "008-large-area", "input": "tests/008-large-area.in", "expected": "tests/008-large-area.out", - "points": 5, - "tags": ["overflow"] + "points": 4, + "tags": [ + "overflow" + ] }, { "id": "009-scale", "input": "tests/009-scale.in", "expected": "tests/009-scale.out", "points": 4, - "tags": ["scale", "complexity"] - }, - {"id":"010-two-bars","input":"tests/010-two-bars.in","expected":"tests/010-two-bars.out","points":1,"tags":["boundary"]}, - {"id":"011-zero-only","input":"tests/011-zero-only.in","expected":"tests/011-zero-only.out","points":1,"tags":["boundary"]}, - {"id":"012-tall-middle","input":"tests/012-tall-middle.in","expected":"tests/012-tall-middle.out","points":1,"tags":["normal"]}, - {"id":"013-plateau-with-dips","input":"tests/013-plateau-with-dips.in","expected":"tests/013-plateau-with-dips.out","points":1,"tags":["regression"]}, - {"id":"014-mixed-small","input":"tests/014-mixed-small.in","expected":"tests/014-mixed-small.out","points":1,"tags":["normal"]}, - {"id":"015-large-height","input":"tests/015-large-height.in","expected":"tests/015-large-height.out","points":1,"tags":["overflow"]}, - {"id":"016-two-levels","input":"tests/016-two-levels.in","expected":"tests/016-two-levels.out","points":1,"tags":["normal"]}, - {"id":"017-zero-split","input":"tests/017-zero-split.in","expected":"tests/017-zero-split.out","points":1,"tags":["boundary"]}, - {"id":"018-tall-edges","input":"tests/018-tall-edges.in","expected":"tests/018-tall-edges.out","points":1,"tags":["normal"]}, - {"id":"019-nested-valleys","input":"tests/019-nested-valleys.in","expected":"tests/019-nested-valleys.out","points":1,"tags":["regression"]}, - {"id":"020-single-zero","input":"tests/020-single-zero.in","expected":"tests/020-single-zero.out","points":1,"tags":["boundary"]}, - {"id":"021-large-flat","input":"tests/021-large-flat.in","expected":"tests/021-large-flat.out","points":1,"tags":["overflow"]}, - {"id":"022-alternating-heights","input":"tests/022-alternating-heights.in","expected":"tests/022-alternating-heights.out","points":1,"tags":["normal"]}, - {"id":"023-double-plateau","input":"tests/023-double-plateau.in","expected":"tests/023-double-plateau.out","points":1,"tags":["regression"]}, - {"id":"024-zero-edges","input":"tests/024-zero-edges.in","expected":"tests/024-zero-edges.out","points":1,"tags":["boundary"]}, - {"id":"025-large-mixed","input":"tests/025-large-mixed.in","expected":"tests/025-large-mixed.out","points":1,"tags":["overflow"]} + "tags": [ + "scale", + "complexity" + ] + }, + { + "id": "010-two-bars", + "input": "tests/010-two-bars.in", + "expected": "tests/010-two-bars.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "011-zero-only", + "input": "tests/011-zero-only.in", + "expected": "tests/011-zero-only.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "012-tall-middle", + "input": "tests/012-tall-middle.in", + "expected": "tests/012-tall-middle.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-plateau-with-dips", + "input": "tests/013-plateau-with-dips.in", + "expected": "tests/013-plateau-with-dips.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "014-mixed-small", + "input": "tests/014-mixed-small.in", + "expected": "tests/014-mixed-small.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-large-height", + "input": "tests/015-large-height.in", + "expected": "tests/015-large-height.out", + "points": 4, + "tags": [ + "overflow" + ] + }, + { + "id": "016-two-levels", + "input": "tests/016-two-levels.in", + "expected": "tests/016-two-levels.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "017-zero-split", + "input": "tests/017-zero-split.in", + "expected": "tests/017-zero-split.out", + "points": 4, + "tags": [ + "boundary" + ] + }, + { + "id": "018-tall-edges", + "input": "tests/018-tall-edges.in", + "expected": "tests/018-tall-edges.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "019-nested-valleys", + "input": "tests/019-nested-valleys.in", + "expected": "tests/019-nested-valleys.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-single-zero", + "input": "tests/020-single-zero.in", + "expected": "tests/020-single-zero.out", + "points": 4, + "tags": [ + "boundary" + ] + } ] diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/Makefile b/labs/chapter-02/exercise/E-02-09-bracket-matching/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/README.md b/labs/chapter-02/exercise/E-02-09-bracket-matching/README.md new file mode 100644 index 00000000..c5591274 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/README.md @@ -0,0 +1,75 @@ +--- +title: "Lab 02-E-09:括号匹配" +description: "用栈验证多种括号的嵌套关系。" +order: 20 +chapter: 2 +labId: "02E09" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-09:括号匹配 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用栈验证多种括号的嵌套关系。 + +## 输入格式 + +字符串 `s`,仅含 ASCII 非空白字符,可含括号与普通字符;空字符串不作为输入。 + +## 输出格式 + +括号是否完全匹配:`YES` 或 `NO`。 + +## 数据范围与限制 + +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-09-bracket-matching +pnpm lab:run -- labs/chapter-02/exercise/E-02-09-bracket-matching +pnpm lab:score -- labs/chapter-02/exercise/E-02-09-bracket-matching +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/lab.json b/labs/chapter-02/exercise/E-02-09-bracket-matching/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/solution/main.cpp b/labs/chapter-02/exercise/E-02-09-bracket-matching/solution/main.cpp new file mode 100644 index 00000000..f8193fbc --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/solution/main.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +bool matches(char left, char right) { + return (left == '(' && right == ')') || (left == '[' && right == ']') || + (left == '{' && right == '}'); +} + +int main() { + std::string text; + if (!(std::cin >> text)) return 0; + std::stack pending; + for (char ch : text) { + if (ch == '(' || ch == '[' || ch == '{') pending.push(ch); + else if (ch == ')' || ch == ']' || ch == '}') { + if (pending.empty() || !matches(pending.top(), ch)) { + std::cout << "NO\n"; + return 0; + } + pending.pop(); + } + } + std::cout << (pending.empty() ? "YES" : "NO") << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/student/main.cpp b/labs/chapter-02/exercise/E-02-09-bracket-matching/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/001-sample.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/001-sample.in new file mode 100644 index 00000000..6a452c18 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/001-sample.in @@ -0,0 +1 @@ +() diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/021-blockwise-valid.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/001-sample.out similarity index 100% rename from labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/021-blockwise-valid.out rename to labs/chapter-02/exercise/E-02-09-bracket-matching/tests/001-sample.out diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/002-boundary.in new file mode 100644 index 00000000..9d32875d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/002-boundary.in @@ -0,0 +1 @@ +([]{}) diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/023-negative-valid.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/002-boundary.out similarity index 100% rename from labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/023-negative-valid.out rename to labs/chapter-02/exercise/E-02-09-bracket-matching/tests/002-boundary.out diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/003-boundary.in new file mode 100644 index 00000000..75efa8c7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/003-boundary.in @@ -0,0 +1 @@ +([)] diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/003-boundary.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/003-boundary.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/004-boundary.in new file mode 100644 index 00000000..2d06f376 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/004-boundary.in @@ -0,0 +1 @@ +( diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/004-boundary.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/004-boundary.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/005-boundary.in new file mode 100644 index 00000000..bea8cd17 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/005-boundary.in @@ -0,0 +1 @@ +) diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/005-boundary.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/005-boundary.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/006-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/006-normal.in new file mode 100644 index 00000000..8baef1b4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/006-normal.in @@ -0,0 +1 @@ +abc diff --git a/labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/024-three-blocks.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/006-normal.out similarity index 100% rename from labs/chapter-02/exercise/E-02-01-validate-stack-sequences/tests/024-three-blocks.out rename to labs/chapter-02/exercise/E-02-09-bracket-matching/tests/006-normal.out diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/007-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/007-normal.in new file mode 100644 index 00000000..c68423d3 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/007-normal.in @@ -0,0 +1 @@ +a(b[c]{d}) diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/007-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/007-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/007-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/008-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/008-normal.in new file mode 100644 index 00000000..6b534fff --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/008-normal.in @@ -0,0 +1 @@ +{[()]} diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/008-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/008-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/008-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/009-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/009-normal.in new file mode 100644 index 00000000..1e9bae17 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/009-normal.in @@ -0,0 +1 @@ +{[(])} diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/009-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/009-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/009-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/010-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/010-normal.in new file mode 100644 index 00000000..8485180c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/010-normal.in @@ -0,0 +1 @@ +(((()))) diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/010-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/010-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/010-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/011-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/011-normal.in new file mode 100644 index 00000000..833f4ee6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/011-normal.in @@ -0,0 +1 @@ +[[]]{} diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/011-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/011-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/011-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/012-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/012-normal.in new file mode 100644 index 00000000..8289d3dd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/012-normal.in @@ -0,0 +1 @@ +([{}])x diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/012-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/012-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/012-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/013-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/013-normal.in new file mode 100644 index 00000000..74d7e510 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/013-normal.in @@ -0,0 +1 @@ +((()) diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/013-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/013-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/013-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/014-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/014-normal.in new file mode 100644 index 00000000..9f93cf02 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/014-normal.in @@ -0,0 +1 @@ +())( diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/014-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/014-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/014-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/015-normal.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/015-normal.in new file mode 100644 index 00000000..98232c64 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/015-normal.in @@ -0,0 +1 @@ +{ diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/015-normal.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/015-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/015-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/016-regression.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/016-regression.in new file mode 100644 index 00000000..5c34318c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/016-regression.in @@ -0,0 +1 @@ +} diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/016-regression.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/016-regression.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/016-regression.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/017-regression.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/017-regression.in new file mode 100644 index 00000000..e4db21bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/017-regression.in @@ -0,0 +1 @@ +a[b(c)d]e diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/017-regression.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/017-regression.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/017-regression.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/018-regression.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/018-regression.in new file mode 100644 index 00000000..3edf676a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/018-regression.in @@ -0,0 +1 @@ +a[b(c}d]e diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/018-regression.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/018-regression.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/018-regression.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/019-regression.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/019-regression.in new file mode 100644 index 00000000..10b3a6be --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/019-regression.in @@ -0,0 +1 @@ +(([]){}) diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/019-regression.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/019-regression.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/019-regression.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/020-regression.in b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/020-regression.in new file mode 100644 index 00000000..b312bcbe --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/020-regression.in @@ -0,0 +1 @@ +([{}])[] diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/020-regression.out b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/020-regression.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/020-regression.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/cases.json b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-09-bracket-matching/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/Makefile b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/README.md b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/README.md new file mode 100644 index 00000000..5e37f80b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/README.md @@ -0,0 +1,77 @@ +--- +title: "Lab 02-E-10:逆波兰表达式求值" +description: "用操作数栈处理二元运算与非法表达式。" +order: 21 +chapter: 2 +labId: "02E10" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-10:逆波兰表达式求值 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用操作数栈处理二元运算与非法表达式。 + +## 输入格式 + +第一行 token 数 `n`;第二行包含恰好 `n` 个、以空白分隔的整数或 `+ - * /` token;当 `n=0` 时第二行为空。 + +## 输出格式 + +合法表达式输出整数结果,否则输出 `ERROR`。 + +## 数据范围与限制 + +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- `0 <= n <= 200000`;每个操作数和每次运算结果都必须落在 64 位有符号整数范围内,保证不会发生溢出。 +- 除数为 `0`、操作数不足或最终栈中不是恰好一个结果时,输出 `ERROR`。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-10-rpn-evaluation +pnpm lab:run -- labs/chapter-02/exercise/E-02-10-rpn-evaluation +pnpm lab:score -- labs/chapter-02/exercise/E-02-10-rpn-evaluation +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/lab.json b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/solution/main.cpp b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/solution/main.cpp new file mode 100644 index 00000000..8944f1ee --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/solution/main.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +bool integerToken(const std::string& token, long long& value) { + try { + std::size_t used = 0; + value = std::stoll(token, &used); + return used == token.size(); + } catch (...) { return false; } +} + +int main() { + std::size_t n = 0; + if (!(std::cin >> n)) return 0; + std::vector values; + bool valid = true; + for (std::size_t i = 0; i < n; ++i) { + std::string token; + std::cin >> token; + if (!valid) continue; + long long value = 0; + if (integerToken(token, value)) { values.push_back(value); continue; } + if ((token != "+" && token != "-" && token != "*" && token != "/") || values.size() < 2) { valid = false; continue; } + const long long right = values.back(); values.pop_back(); + const long long left = values.back(); values.pop_back(); + if (token == "+") values.push_back(left + right); + else if (token == "-") values.push_back(left - right); + else if (token == "*") values.push_back(left * right); + else if (right == 0) valid = false; + else values.push_back(left / right); + } + if (!valid || values.size() != 1) std::cout << "ERROR\n"; + else std::cout << values.back() << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/student/main.cpp b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/001-sample.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/001-sample.in new file mode 100644 index 00000000..4ccde532 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/001-sample.in @@ -0,0 +1,2 @@ +5 +2 1 + 3 * diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/001-sample.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/001-sample.out new file mode 100644 index 00000000..ec635144 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/001-sample.out @@ -0,0 +1 @@ +9 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/002-boundary.in new file mode 100644 index 00000000..df85a17b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/002-boundary.in @@ -0,0 +1,2 @@ +5 +4 13 5 / + diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/002-boundary.out new file mode 100644 index 00000000..1e8b3149 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/002-boundary.out @@ -0,0 +1 @@ +6 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/003-boundary.in new file mode 100644 index 00000000..7ba59a03 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/003-boundary.in @@ -0,0 +1,2 @@ +3 +8 3 - diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/003-boundary.out new file mode 100644 index 00000000..7ed6ff82 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/003-boundary.out @@ -0,0 +1 @@ +5 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/004-boundary.in new file mode 100644 index 00000000..ab3c946d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/004-boundary.in @@ -0,0 +1,2 @@ +3 +-3 -2 * diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/004-boundary.out new file mode 100644 index 00000000..1e8b3149 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/004-boundary.out @@ -0,0 +1 @@ +6 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/005-boundary.in new file mode 100644 index 00000000..79ac02e8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/005-boundary.in @@ -0,0 +1,2 @@ +2 +1 + diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/005-boundary.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/005-boundary.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/006-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/006-normal.in new file mode 100644 index 00000000..09a90ac1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/006-normal.in @@ -0,0 +1,2 @@ +3 +4 0 / diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/006-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/006-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/006-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/007-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/007-normal.in new file mode 100644 index 00000000..a789e877 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/007-normal.in @@ -0,0 +1,2 @@ +2 +1 2 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/007-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/007-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/007-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/008-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/008-normal.in new file mode 100644 index 00000000..0ab8d200 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/008-normal.in @@ -0,0 +1,2 @@ +1 ++ diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/008-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/008-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/008-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/009-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/009-normal.in new file mode 100644 index 00000000..90ec29d6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/009-normal.in @@ -0,0 +1,2 @@ +13 +10 6 9 3 + -11 * / * 17 + 5 + diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/009-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/009-normal.out new file mode 100644 index 00000000..2bd5a0a9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/009-normal.out @@ -0,0 +1 @@ +22 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/010-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/010-normal.in new file mode 100644 index 00000000..ce6ef18a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/010-normal.in @@ -0,0 +1,2 @@ +15 +15 7 1 1 + - / 3 * 2 1 1 + + - diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/010-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/010-normal.out new file mode 100644 index 00000000..7ed6ff82 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/010-normal.out @@ -0,0 +1 @@ +5 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/011-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/011-normal.in new file mode 100644 index 00000000..ba96e434 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/011-normal.in @@ -0,0 +1,2 @@ +9 +5 1 2 + 4 * + 3 - diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/011-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/011-normal.out new file mode 100644 index 00000000..8351c193 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/011-normal.out @@ -0,0 +1 @@ +14 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/012-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/012-normal.in new file mode 100644 index 00000000..02da3b3c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/012-normal.in @@ -0,0 +1,2 @@ +3 +7 2 / diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/012-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/012-normal.out new file mode 100644 index 00000000..00750edc --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/012-normal.out @@ -0,0 +1 @@ +3 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/013-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/013-normal.in new file mode 100644 index 00000000..ff525412 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/013-normal.in @@ -0,0 +1,2 @@ +3 +-7 2 / diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/013-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/013-normal.out new file mode 100644 index 00000000..a83d1d52 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/013-normal.out @@ -0,0 +1 @@ +-3 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/014-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/014-normal.in new file mode 100644 index 00000000..ae14ceff --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/014-normal.in @@ -0,0 +1,2 @@ +5 +100 20 / 3 + diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/014-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/014-normal.out new file mode 100644 index 00000000..45a4fb75 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/014-normal.out @@ -0,0 +1 @@ +8 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/015-normal.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/015-normal.in new file mode 100644 index 00000000..6e137daa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/015-normal.in @@ -0,0 +1,2 @@ +5 +9 9 * 2 / diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/015-normal.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/015-normal.out new file mode 100644 index 00000000..425151f3 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/015-normal.out @@ -0,0 +1 @@ +40 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/016-regression.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/016-regression.in new file mode 100644 index 00000000..0513ca5c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/016-regression.in @@ -0,0 +1,2 @@ +4 +1 2 + + diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/016-regression.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/016-regression.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/016-regression.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/017-regression.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/017-regression.in new file mode 100644 index 00000000..b763bfdf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/017-regression.in @@ -0,0 +1,2 @@ +4 +1 2 ? + diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/017-regression.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/017-regression.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/017-regression.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/018-regression.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/018-regression.in new file mode 100644 index 00000000..8da61788 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/018-regression.in @@ -0,0 +1,2 @@ +7 +6 2 / 3 - 4 * diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/018-regression.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/018-regression.out new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/018-regression.out @@ -0,0 +1 @@ +0 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/019-regression.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/019-regression.in new file mode 100644 index 00000000..da2ceaff --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/019-regression.in @@ -0,0 +1,2 @@ +7 +20 3 / 2 * 1 + diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/019-regression.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/019-regression.out new file mode 100644 index 00000000..b1bd38b6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/019-regression.out @@ -0,0 +1 @@ +13 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/020-regression.in b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/020-regression.in new file mode 100644 index 00000000..77b5ce66 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/020-regression.in @@ -0,0 +1,2 @@ +5 +0 5 - 2 * diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/020-regression.out b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/020-regression.out new file mode 100644 index 00000000..29167035 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/020-regression.out @@ -0,0 +1 @@ +-10 diff --git a/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/cases.json b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-10-rpn-evaluation/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/Makefile b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/README.md b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/README.md new file mode 100644 index 00000000..ad9144bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/README.md @@ -0,0 +1,75 @@ +--- +title: "Lab 02-E-11:中缀表达式转后缀" +description: "用调度场算法处理优先级、左结合和括号。" +order: 22 +chapter: 2 +labId: "02E11" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-11:中缀表达式转后缀 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用调度场算法处理优先级、左结合和括号。 + +## 输入格式 + +一行无空格表达式;操作数为单字符 ASCII 字母或数字,运算符仅允许 `+ - * /`,并允许使用圆括号 `(`、`)`。不要求处理一元运算符。 + +## 输出格式 + +合法时输出后缀表达式,非法时输出 `ERROR`。 + +## 数据范围与限制 + +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-11-infix-to-postfix +pnpm lab:run -- labs/chapter-02/exercise/E-02-11-infix-to-postfix +pnpm lab:score -- labs/chapter-02/exercise/E-02-11-infix-to-postfix +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/lab.json b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/solution/main.cpp b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/solution/main.cpp new file mode 100644 index 00000000..4a6bed56 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/solution/main.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +bool op(char ch) { return ch == '+' || ch == '-' || ch == '*' || ch == '/'; } +int priority(char ch) { return (ch == '*' || ch == '/') ? 2 : 1; } + +int main() { + std::string expression; + if (!(std::cin >> expression)) return 0; + std::stack operators; + std::string output; + bool expectOperand = true; + bool valid = true; + for (char ch : expression) { + if (std::isalnum(static_cast(ch))) { + if (!expectOperand) { valid = false; break; } + output += ch; expectOperand = false; + } else if (ch == '(') { + if (!expectOperand) { valid = false; break; } + operators.push(ch); + } else if (ch == ')') { + if (expectOperand) { valid = false; break; } + while (!operators.empty() && operators.top() != '(') { output += operators.top(); operators.pop(); } + if (operators.empty()) { valid = false; break; } + operators.pop(); + } else if (op(ch)) { + if (expectOperand) { valid = false; break; } + while (!operators.empty() && operators.top() != '(' && priority(operators.top()) >= priority(ch)) { output += operators.top(); operators.pop(); } + operators.push(ch); expectOperand = true; + } else { valid = false; break; } + } + if (expectOperand) valid = false; + while (valid && !operators.empty()) { + if (operators.top() == '(') { valid = false; break; } + output += operators.top(); operators.pop(); + } + std::cout << (valid ? output : "ERROR") << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/student/main.cpp b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/001-sample.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/001-sample.in new file mode 100644 index 00000000..9b7d5e5b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/001-sample.in @@ -0,0 +1 @@ +a+b*c diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/001-sample.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/001-sample.out new file mode 100644 index 00000000..fdeb9c47 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/001-sample.out @@ -0,0 +1 @@ +abc*+ diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/002-boundary.in new file mode 100644 index 00000000..8b00c1ee --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/002-boundary.in @@ -0,0 +1 @@ +(a+b)*c diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/002-boundary.out new file mode 100644 index 00000000..e48193bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/002-boundary.out @@ -0,0 +1 @@ +ab+c* diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/003-boundary.in new file mode 100644 index 00000000..3b26186d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/003-boundary.in @@ -0,0 +1 @@ +a*(b+c) diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/003-boundary.out new file mode 100644 index 00000000..29742127 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/003-boundary.out @@ -0,0 +1 @@ +abc+* diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/004-boundary.in new file mode 100644 index 00000000..b0d94ba4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/004-boundary.in @@ -0,0 +1 @@ +a+b*c-d/e diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/004-boundary.out new file mode 100644 index 00000000..f298e01d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/004-boundary.out @@ -0,0 +1 @@ +abc*+de/- diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/005-boundary.in new file mode 100644 index 00000000..78981922 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/005-boundary.in @@ -0,0 +1 @@ +a diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/005-boundary.out new file mode 100644 index 00000000..78981922 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/005-boundary.out @@ -0,0 +1 @@ +a diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/006-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/006-normal.in new file mode 100644 index 00000000..b03e7409 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/006-normal.in @@ -0,0 +1 @@ +(a) diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/006-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/006-normal.out new file mode 100644 index 00000000..78981922 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/006-normal.out @@ -0,0 +1 @@ +a diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/007-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/007-normal.in new file mode 100644 index 00000000..94a54f3d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/007-normal.in @@ -0,0 +1 @@ +a+b diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/007-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/007-normal.out new file mode 100644 index 00000000..d9148b29 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/007-normal.out @@ -0,0 +1 @@ +ab+ diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/008-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/008-normal.in new file mode 100644 index 00000000..ec4d9e62 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/008-normal.in @@ -0,0 +1 @@ +a-b-c diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/008-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/008-normal.out new file mode 100644 index 00000000..ac77c501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/008-normal.out @@ -0,0 +1 @@ +ab-c- diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/009-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/009-normal.in new file mode 100644 index 00000000..c76aadf4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/009-normal.in @@ -0,0 +1 @@ +a/(b-c+d)*(e-a)*c diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/009-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/009-normal.out new file mode 100644 index 00000000..5d2ac2e2 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/009-normal.out @@ -0,0 +1 @@ +abc-d+/ea-*c* diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/010-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/010-normal.in new file mode 100644 index 00000000..703cd228 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/010-normal.in @@ -0,0 +1 @@ +a+ diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/010-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/010-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/010-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/011-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/011-normal.in new file mode 100644 index 00000000..daea40b7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/011-normal.in @@ -0,0 +1 @@ ++a diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/011-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/011-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/011-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/012-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/012-normal.in new file mode 100644 index 00000000..baa17a6b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/012-normal.in @@ -0,0 +1 @@ +a(b+c) diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/012-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/012-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/012-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/013-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/013-normal.in new file mode 100644 index 00000000..21522a5f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/013-normal.in @@ -0,0 +1 @@ +a++b diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/013-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/013-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/013-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/014-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/014-normal.in new file mode 100644 index 00000000..0bc905d6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/014-normal.in @@ -0,0 +1 @@ +(a+b diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/014-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/014-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/014-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/015-normal.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/015-normal.in new file mode 100644 index 00000000..c60d2573 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/015-normal.in @@ -0,0 +1 @@ +a+b) diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/015-normal.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/015-normal.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/015-normal.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/016-regression.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/016-regression.in new file mode 100644 index 00000000..6a452c18 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/016-regression.in @@ -0,0 +1 @@ +() diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/016-regression.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/016-regression.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/016-regression.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/017-regression.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/017-regression.in new file mode 100644 index 00000000..2ba1679b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/017-regression.in @@ -0,0 +1 @@ +a+() diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/017-regression.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/017-regression.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/017-regression.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/018-regression.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/018-regression.in new file mode 100644 index 00000000..997f180e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/018-regression.in @@ -0,0 +1 @@ +a$b diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/018-regression.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/018-regression.out new file mode 100644 index 00000000..5df7507e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/018-regression.out @@ -0,0 +1 @@ +ERROR diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/019-regression.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/019-regression.in new file mode 100644 index 00000000..3a46b1b7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/019-regression.in @@ -0,0 +1 @@ +a*b/c diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/019-regression.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/019-regression.out new file mode 100644 index 00000000..d8f74feb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/019-regression.out @@ -0,0 +1 @@ +ab*c/ diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/020-regression.in b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/020-regression.in new file mode 100644 index 00000000..de880a86 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/020-regression.in @@ -0,0 +1 @@ +a*(b/(c-d)) diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/020-regression.out b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/020-regression.out new file mode 100644 index 00000000..a8d9b565 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/020-regression.out @@ -0,0 +1 @@ +abcd-/* diff --git a/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/cases.json b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-11-infix-to-postfix/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/Makefile b/labs/chapter-02/exercise/E-02-12-next-greater-element/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/README.md b/labs/chapter-02/exercise/E-02-12-next-greater-element/README.md new file mode 100644 index 00000000..b84422f8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/README.md @@ -0,0 +1,76 @@ +--- +title: "Lab 02-E-12:下一个更大元素" +description: "维护单调递减候选栈,求右侧第一个严格更大值。" +order: 23 +chapter: 2 +labId: "02E12" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-12:下一个更大元素 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +维护单调递减候选栈,求右侧第一个严格更大值。 + +## 输入格式 + +第一行 `n`;第二行 `n` 个整数。 + +## 输出格式 + +输出每个位置右侧第一个严格更大值;不存在输出 `-1`。各答案以单个空格分隔,末尾输出换行。 + +## 数据范围与限制 + +- `0 <= n <= 200000`;当 `n=0` 时第二行为空,输出空行。 +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-12-next-greater-element +pnpm lab:run -- labs/chapter-02/exercise/E-02-12-next-greater-element +pnpm lab:score -- labs/chapter-02/exercise/E-02-12-next-greater-element +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/lab.json b/labs/chapter-02/exercise/E-02-12-next-greater-element/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/solution/main.cpp b/labs/chapter-02/exercise/E-02-12-next-greater-element/solution/main.cpp new file mode 100644 index 00000000..554020db --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/solution/main.cpp @@ -0,0 +1,15 @@ +#include +#include +#include + +int main() { + std::size_t n = 0; if (!(std::cin >> n)) return 0; + std::vector a(n), answer(n, -1); std::vector pending; + for (auto& x : a) std::cin >> x; + for (std::size_t i = 0; i < n; ++i) { + while (!pending.empty() && a[pending.back()] < a[i]) { answer[pending.back()] = a[i]; pending.pop_back(); } + pending.push_back(i); + } + for (std::size_t i = 0; i < n; ++i) std::cout << (i ? " " : "") << answer[i]; + std::cout << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/student/main.cpp b/labs/chapter-02/exercise/E-02-12-next-greater-element/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/021-single-element.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/001-sample.in similarity index 57% rename from labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/021-single-element.in rename to labs/chapter-02/exercise/E-02-12-next-greater-element/tests/001-sample.in index dd5eb5d5..6ed281c7 100644 --- a/labs/chapter-02/exercise/E-02-07-sliding-window-maximum/tests/021-single-element.in +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/001-sample.in @@ -1,3 +1,2 @@ 1 1 -42 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/001-sample.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/001-sample.out new file mode 100644 index 00000000..3a2e3f49 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/001-sample.out @@ -0,0 +1 @@ +-1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/002-boundary.in new file mode 100644 index 00000000..65cd4f58 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/002-boundary.in @@ -0,0 +1,2 @@ +2 +2 1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/002-boundary.out new file mode 100644 index 00000000..9b56b57b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/002-boundary.out @@ -0,0 +1 @@ +-1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/003-boundary.in new file mode 100644 index 00000000..a789e877 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/003-boundary.in @@ -0,0 +1,2 @@ +2 +1 2 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/003-boundary.out new file mode 100644 index 00000000..97940c7d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/003-boundary.out @@ -0,0 +1 @@ +2 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/004-boundary.in new file mode 100644 index 00000000..2d777856 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/004-boundary.in @@ -0,0 +1,2 @@ +2 +2 2 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/004-boundary.out new file mode 100644 index 00000000..9b56b57b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/004-boundary.out @@ -0,0 +1 @@ +-1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/005-boundary.in new file mode 100644 index 00000000..8b98c7f1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/005-boundary.in @@ -0,0 +1,2 @@ +3 +2 1 2 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/005-boundary.out new file mode 100644 index 00000000..9608cd14 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/005-boundary.out @@ -0,0 +1 @@ +-1 2 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/006-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/006-normal.in new file mode 100644 index 00000000..cca7fe34 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/006-normal.in @@ -0,0 +1,2 @@ +5 +5 1 4 2 6 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/006-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/006-normal.out new file mode 100644 index 00000000..51a6185d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/006-normal.out @@ -0,0 +1 @@ +6 4 6 6 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/007-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/007-normal.in new file mode 100644 index 00000000..8db8ab6d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/007-normal.in @@ -0,0 +1,2 @@ +6 +6 5 4 3 2 1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/007-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/007-normal.out new file mode 100644 index 00000000..bc5e4943 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/007-normal.out @@ -0,0 +1 @@ +-1 -1 -1 -1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/008-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/008-normal.in new file mode 100644 index 00000000..5ee886e1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/008-normal.in @@ -0,0 +1,2 @@ +6 +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/008-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/008-normal.out new file mode 100644 index 00000000..157e6f88 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/008-normal.out @@ -0,0 +1 @@ +2 3 4 5 6 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/009-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/009-normal.in new file mode 100644 index 00000000..5aab6eaa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/009-normal.in @@ -0,0 +1,2 @@ +4 +-1 -3 -2 -4 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/009-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/009-normal.out new file mode 100644 index 00000000..481daef0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/009-normal.out @@ -0,0 +1 @@ +-1 -2 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/010-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/010-normal.in new file mode 100644 index 00000000..3f62807b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/010-normal.in @@ -0,0 +1,2 @@ +4 +0 0 0 0 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/010-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/010-normal.out new file mode 100644 index 00000000..97610e3c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/010-normal.out @@ -0,0 +1 @@ +-1 -1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/011-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/011-normal.in new file mode 100644 index 00000000..481b7578 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/011-normal.in @@ -0,0 +1,2 @@ +5 +7 1 7 1 7 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/011-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/011-normal.out new file mode 100644 index 00000000..f144f379 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/011-normal.out @@ -0,0 +1 @@ +-1 7 -1 7 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/012-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/012-normal.in new file mode 100644 index 00000000..824a30e0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/012-normal.in @@ -0,0 +1,2 @@ +6 +3 8 2 9 1 10 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/012-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/012-normal.out new file mode 100644 index 00000000..f4a43a8d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/012-normal.out @@ -0,0 +1 @@ +8 9 9 10 10 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/013-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/013-normal.in new file mode 100644 index 00000000..ece8fbdb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/013-normal.in @@ -0,0 +1,2 @@ +5 +10 9 8 7 6 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/013-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/013-normal.out new file mode 100644 index 00000000..5302f772 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/013-normal.out @@ -0,0 +1 @@ +-1 -1 -1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/014-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/014-normal.in new file mode 100644 index 00000000..8215b61b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/014-normal.in @@ -0,0 +1,2 @@ +5 +4 1 2 3 0 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/014-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/014-normal.out new file mode 100644 index 00000000..411b70e1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/014-normal.out @@ -0,0 +1 @@ +-1 2 3 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/015-normal.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/015-normal.in new file mode 100644 index 00000000..73c8c8e6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/015-normal.in @@ -0,0 +1,2 @@ +5 +100 -100 50 -50 0 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/015-normal.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/015-normal.out new file mode 100644 index 00000000..59cfa639 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/015-normal.out @@ -0,0 +1 @@ +-1 50 -1 0 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/016-regression.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/016-regression.in new file mode 100644 index 00000000..430eb31e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/016-regression.in @@ -0,0 +1,2 @@ +7 +9 1 8 2 7 3 6 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/016-regression.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/016-regression.out new file mode 100644 index 00000000..d41d1593 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/016-regression.out @@ -0,0 +1 @@ +-1 8 -1 7 -1 6 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/017-regression.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/017-regression.in new file mode 100644 index 00000000..49184691 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/017-regression.in @@ -0,0 +1,2 @@ +6 +1 3 2 4 3 5 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/017-regression.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/017-regression.out new file mode 100644 index 00000000..1be57f3f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/017-regression.out @@ -0,0 +1 @@ +3 4 4 5 5 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/018-regression.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/018-regression.in new file mode 100644 index 00000000..efa0c2e6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/018-regression.in @@ -0,0 +1,2 @@ +6 +5 5 4 4 3 3 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/018-regression.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/018-regression.out new file mode 100644 index 00000000..bc5e4943 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/018-regression.out @@ -0,0 +1 @@ +-1 -1 -1 -1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/019-regression.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/019-regression.in new file mode 100644 index 00000000..cfc22005 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/019-regression.in @@ -0,0 +1,2 @@ +6 +2 9 1 8 0 7 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/019-regression.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/019-regression.out new file mode 100644 index 00000000..b273e37c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/019-regression.out @@ -0,0 +1 @@ +9 -1 8 -1 7 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/020-regression.in b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/020-regression.in new file mode 100644 index 00000000..08c96f86 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/020-regression.in @@ -0,0 +1,2 @@ +7 +11 4 12 3 13 2 14 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/020-regression.out b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/020-regression.out new file mode 100644 index 00000000..731f06c2 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/020-regression.out @@ -0,0 +1 @@ +12 12 13 13 14 14 -1 diff --git a/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/cases.json b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-12-next-greater-element/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/Makefile b/labs/chapter-02/exercise/E-02-13-next-smaller-element/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/README.md b/labs/chapter-02/exercise/E-02-13-next-smaller-element/README.md new file mode 100644 index 00000000..9230bc0f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/README.md @@ -0,0 +1,76 @@ +--- +title: "Lab 02-E-13:下一个更小元素" +description: "维护单调递增候选栈,求右侧第一个严格更小值。" +order: 24 +chapter: 2 +labId: "02E13" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-13:下一个更小元素 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +维护单调递增候选栈,求右侧第一个严格更小值。 + +## 输入格式 + +第一行 `n`;第二行 `n` 个整数。 + +## 输出格式 + +输出每个位置右侧第一个严格更小值;不存在输出 `-1`。各答案以单个空格分隔,末尾输出换行。 + +## 数据范围与限制 + +- `0 <= n <= 200000`;当 `n=0` 时第二行为空,输出空行。 +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-13-next-smaller-element +pnpm lab:run -- labs/chapter-02/exercise/E-02-13-next-smaller-element +pnpm lab:score -- labs/chapter-02/exercise/E-02-13-next-smaller-element +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/lab.json b/labs/chapter-02/exercise/E-02-13-next-smaller-element/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/solution/main.cpp b/labs/chapter-02/exercise/E-02-13-next-smaller-element/solution/main.cpp new file mode 100644 index 00000000..baa995c8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/solution/main.cpp @@ -0,0 +1,15 @@ +#include +#include +#include + +int main() { + std::size_t n = 0; if (!(std::cin >> n)) return 0; + std::vector a(n), answer(n, -1); std::vector pending; + for (auto& x : a) std::cin >> x; + for (std::size_t i = 0; i < n; ++i) { + while (!pending.empty() && a[pending.back()] > a[i]) { answer[pending.back()] = a[i]; pending.pop_back(); } + pending.push_back(i); + } + for (std::size_t i = 0; i < n; ++i) std::cout << (i ? " " : "") << answer[i]; + std::cout << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/student/main.cpp b/labs/chapter-02/exercise/E-02-13-next-smaller-element/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/001-sample.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/001-sample.in new file mode 100644 index 00000000..6ed281c7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/001-sample.in @@ -0,0 +1,2 @@ +1 +1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/001-sample.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/001-sample.out new file mode 100644 index 00000000..3a2e3f49 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/001-sample.out @@ -0,0 +1 @@ +-1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/002-boundary.in new file mode 100644 index 00000000..65cd4f58 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/002-boundary.in @@ -0,0 +1,2 @@ +2 +2 1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/002-boundary.out new file mode 100644 index 00000000..7428044d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/002-boundary.out @@ -0,0 +1 @@ +1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/003-boundary.in new file mode 100644 index 00000000..a789e877 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/003-boundary.in @@ -0,0 +1,2 @@ +2 +1 2 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/003-boundary.out new file mode 100644 index 00000000..9b56b57b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/003-boundary.out @@ -0,0 +1 @@ +-1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/004-boundary.in new file mode 100644 index 00000000..2d777856 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/004-boundary.in @@ -0,0 +1,2 @@ +2 +2 2 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/004-boundary.out new file mode 100644 index 00000000..9b56b57b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/004-boundary.out @@ -0,0 +1 @@ +-1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/005-boundary.in new file mode 100644 index 00000000..8b98c7f1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/005-boundary.in @@ -0,0 +1,2 @@ +3 +2 1 2 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/005-boundary.out new file mode 100644 index 00000000..562aba03 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/005-boundary.out @@ -0,0 +1 @@ +1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/006-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/006-normal.in new file mode 100644 index 00000000..cca7fe34 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/006-normal.in @@ -0,0 +1,2 @@ +5 +5 1 4 2 6 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/006-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/006-normal.out new file mode 100644 index 00000000..1b1dc1a1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/006-normal.out @@ -0,0 +1 @@ +1 -1 2 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/007-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/007-normal.in new file mode 100644 index 00000000..8db8ab6d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/007-normal.in @@ -0,0 +1,2 @@ +6 +6 5 4 3 2 1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/007-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/007-normal.out new file mode 100644 index 00000000..277d2513 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/007-normal.out @@ -0,0 +1 @@ +5 4 3 2 1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/008-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/008-normal.in new file mode 100644 index 00000000..5ee886e1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/008-normal.in @@ -0,0 +1,2 @@ +6 +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/008-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/008-normal.out new file mode 100644 index 00000000..bc5e4943 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/008-normal.out @@ -0,0 +1 @@ +-1 -1 -1 -1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/009-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/009-normal.in new file mode 100644 index 00000000..5aab6eaa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/009-normal.in @@ -0,0 +1,2 @@ +4 +-1 -3 -2 -4 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/009-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/009-normal.out new file mode 100644 index 00000000..faa0d1ff --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/009-normal.out @@ -0,0 +1 @@ +-3 -4 -4 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/010-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/010-normal.in new file mode 100644 index 00000000..3f62807b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/010-normal.in @@ -0,0 +1,2 @@ +4 +0 0 0 0 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/010-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/010-normal.out new file mode 100644 index 00000000..97610e3c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/010-normal.out @@ -0,0 +1 @@ +-1 -1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/011-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/011-normal.in new file mode 100644 index 00000000..481b7578 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/011-normal.in @@ -0,0 +1,2 @@ +5 +7 1 7 1 7 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/011-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/011-normal.out new file mode 100644 index 00000000..31c7519c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/011-normal.out @@ -0,0 +1 @@ +1 -1 1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/012-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/012-normal.in new file mode 100644 index 00000000..824a30e0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/012-normal.in @@ -0,0 +1,2 @@ +6 +3 8 2 9 1 10 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/012-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/012-normal.out new file mode 100644 index 00000000..02d4b561 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/012-normal.out @@ -0,0 +1 @@ +2 2 1 1 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/013-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/013-normal.in new file mode 100644 index 00000000..ece8fbdb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/013-normal.in @@ -0,0 +1,2 @@ +5 +10 9 8 7 6 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/013-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/013-normal.out new file mode 100644 index 00000000..465b62fa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/013-normal.out @@ -0,0 +1 @@ +9 8 7 6 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/014-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/014-normal.in new file mode 100644 index 00000000..8215b61b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/014-normal.in @@ -0,0 +1,2 @@ +5 +4 1 2 3 0 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/014-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/014-normal.out new file mode 100644 index 00000000..249b0f7e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/014-normal.out @@ -0,0 +1 @@ +1 0 0 0 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/015-normal.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/015-normal.in new file mode 100644 index 00000000..73c8c8e6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/015-normal.in @@ -0,0 +1,2 @@ +5 +100 -100 50 -50 0 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/015-normal.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/015-normal.out new file mode 100644 index 00000000..063e6e34 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/015-normal.out @@ -0,0 +1 @@ +-100 -1 -50 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/016-regression.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/016-regression.in new file mode 100644 index 00000000..430eb31e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/016-regression.in @@ -0,0 +1,2 @@ +7 +9 1 8 2 7 3 6 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/016-regression.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/016-regression.out new file mode 100644 index 00000000..92aa1130 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/016-regression.out @@ -0,0 +1 @@ +1 -1 2 -1 3 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/017-regression.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/017-regression.in new file mode 100644 index 00000000..49184691 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/017-regression.in @@ -0,0 +1,2 @@ +6 +1 3 2 4 3 5 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/017-regression.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/017-regression.out new file mode 100644 index 00000000..1935767b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/017-regression.out @@ -0,0 +1 @@ +-1 2 -1 3 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/018-regression.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/018-regression.in new file mode 100644 index 00000000..efa0c2e6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/018-regression.in @@ -0,0 +1,2 @@ +6 +5 5 4 4 3 3 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/018-regression.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/018-regression.out new file mode 100644 index 00000000..3b71c4bb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/018-regression.out @@ -0,0 +1 @@ +4 4 3 3 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/019-regression.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/019-regression.in new file mode 100644 index 00000000..cfc22005 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/019-regression.in @@ -0,0 +1,2 @@ +6 +2 9 1 8 0 7 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/019-regression.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/019-regression.out new file mode 100644 index 00000000..d3928797 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/019-regression.out @@ -0,0 +1 @@ +1 1 0 0 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/020-regression.in b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/020-regression.in new file mode 100644 index 00000000..08c96f86 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/020-regression.in @@ -0,0 +1,2 @@ +7 +11 4 12 3 13 2 14 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/020-regression.out b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/020-regression.out new file mode 100644 index 00000000..99c976ff --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/020-regression.out @@ -0,0 +1 @@ +4 3 3 2 2 -1 -1 diff --git a/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/cases.json b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-13-next-smaller-element/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/Makefile b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/README.md b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/README.md new file mode 100644 index 00000000..8ed321eb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/README.md @@ -0,0 +1,76 @@ +--- +title: "Lab 02-E-14:农夫抓牛" +description: "用 FIFO 队列按层扩散,求单位代价状态转移的最短步数。" +order: 25 +chapter: 2 +labId: "02E14" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-14:农夫抓牛 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用 FIFO 队列按层扩散,求单位代价状态转移的最短步数。 + +## 输入格式 + +一行 `start target limit`,位置均在 `[0, limit)` 内。 + +## 输出格式 + +输出从 start 到 target 的最少步数。 + +## 数据范围与限制 + +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- `1 <= limit <= 200000`,且 `0 <= start,target < limit`;三种移动分别为 `x-1`、`x+1`、`2*x`,越界移动忽略。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-14-farmer-cow-bfs +pnpm lab:run -- labs/chapter-02/exercise/E-02-14-farmer-cow-bfs +pnpm lab:score -- labs/chapter-02/exercise/E-02-14-farmer-cow-bfs +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/lab.json b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/solution/main.cpp b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/solution/main.cpp new file mode 100644 index 00000000..fcaa6050 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/solution/main.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + +int main() { + int start = 0, target = 0, limit = 0; + if (!(std::cin >> start >> target >> limit)) return 0; + std::vector distance(limit, -1); std::queue queue; + distance[start] = 0; queue.push(start); + while (!queue.empty()) { + const int current = queue.front(); queue.pop(); + if (current == target) break; + const int nexts[3] = {current - 1, current + 1, current * 2}; + for (int next : nexts) if (next >= 0 && next < limit && distance[next] == -1) { distance[next] = distance[current] + 1; queue.push(next); } + } + std::cout << distance[target] << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/student/main.cpp b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/001-sample.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/001-sample.in new file mode 100644 index 00000000..f5026a1e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/001-sample.in @@ -0,0 +1 @@ +5 17 100 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/001-sample.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/001-sample.out new file mode 100644 index 00000000..b8626c4c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/001-sample.out @@ -0,0 +1 @@ +4 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/002-boundary.in new file mode 100644 index 00000000..668d3d03 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/002-boundary.in @@ -0,0 +1 @@ +0 0 10 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/002-boundary.out new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/002-boundary.out @@ -0,0 +1 @@ +0 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/003-boundary.in new file mode 100644 index 00000000..cdfae9f7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/003-boundary.in @@ -0,0 +1 @@ +0 9 10 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/003-boundary.out new file mode 100644 index 00000000..7ed6ff82 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/003-boundary.out @@ -0,0 +1 @@ +5 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/004-boundary.in new file mode 100644 index 00000000..7f00e93d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/004-boundary.in @@ -0,0 +1 @@ +1 2 20 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/004-boundary.out new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/004-boundary.out @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/005-boundary.in new file mode 100644 index 00000000..9154d26d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/005-boundary.in @@ -0,0 +1 @@ +10 1 30 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/005-boundary.out new file mode 100644 index 00000000..ec635144 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/005-boundary.out @@ -0,0 +1 @@ +9 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/006-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/006-normal.in new file mode 100644 index 00000000..02296aba --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/006-normal.in @@ -0,0 +1 @@ +7 14 30 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/006-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/006-normal.out new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/006-normal.out @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/007-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/007-normal.in new file mode 100644 index 00000000..e41a1f4b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/007-normal.in @@ -0,0 +1 @@ +3 10 20 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/007-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/007-normal.out new file mode 100644 index 00000000..00750edc --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/007-normal.out @@ -0,0 +1 @@ +3 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/008-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/008-normal.in new file mode 100644 index 00000000..a2f11cb5 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/008-normal.in @@ -0,0 +1 @@ +4 6 15 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/008-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/008-normal.out new file mode 100644 index 00000000..0cfbf088 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/008-normal.out @@ -0,0 +1 @@ +2 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/009-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/009-normal.in new file mode 100644 index 00000000..dd680051 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/009-normal.in @@ -0,0 +1 @@ +9 3 20 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/009-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/009-normal.out new file mode 100644 index 00000000..1e8b3149 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/009-normal.out @@ -0,0 +1 @@ +6 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/010-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/010-normal.in new file mode 100644 index 00000000..2765e7a1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/010-normal.in @@ -0,0 +1 @@ +0 31 64 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/022-alternating-heights.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/010-normal.out similarity index 100% rename from labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/022-alternating-heights.out rename to labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/010-normal.out diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/011-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/011-normal.in new file mode 100644 index 00000000..36f1f1c3 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/011-normal.in @@ -0,0 +1 @@ +15 0 32 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/011-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/011-normal.out new file mode 100644 index 00000000..60d3b2f4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/011-normal.out @@ -0,0 +1 @@ +15 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/012-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/012-normal.in new file mode 100644 index 00000000..77ecb67e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/012-normal.in @@ -0,0 +1 @@ +2 19 40 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/012-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/012-normal.out new file mode 100644 index 00000000..7ed6ff82 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/012-normal.out @@ -0,0 +1 @@ +5 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/013-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/013-normal.in new file mode 100644 index 00000000..ce282ab9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/013-normal.in @@ -0,0 +1 @@ +20 5 50 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/013-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/013-normal.out new file mode 100644 index 00000000..60d3b2f4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/013-normal.out @@ -0,0 +1 @@ +15 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/014-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/014-normal.in new file mode 100644 index 00000000..b4a07df0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/014-normal.in @@ -0,0 +1 @@ +1 31 40 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/014-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/014-normal.out new file mode 100644 index 00000000..1e8b3149 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/014-normal.out @@ -0,0 +1 @@ +6 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/015-normal.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/015-normal.in new file mode 100644 index 00000000..610cbcef --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/015-normal.in @@ -0,0 +1 @@ +8 23 40 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/015-normal.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/015-normal.out new file mode 100644 index 00000000..7ed6ff82 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/015-normal.out @@ -0,0 +1 @@ +5 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/016-regression.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/016-regression.in new file mode 100644 index 00000000..f1d2ee2f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/016-regression.in @@ -0,0 +1 @@ +6 18 30 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/016-regression.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/016-regression.out new file mode 100644 index 00000000..b8626c4c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/016-regression.out @@ -0,0 +1 @@ +4 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/017-regression.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/017-regression.in new file mode 100644 index 00000000..db9f6eee --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/017-regression.in @@ -0,0 +1 @@ +12 25 40 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/017-regression.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/017-regression.out new file mode 100644 index 00000000..0cfbf088 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/017-regression.out @@ -0,0 +1 @@ +2 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/018-regression.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/018-regression.in new file mode 100644 index 00000000..e6206b9a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/018-regression.in @@ -0,0 +1 @@ +30 1 64 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/018-regression.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/018-regression.out new file mode 100644 index 00000000..f04c001f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/018-regression.out @@ -0,0 +1 @@ +29 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/019-regression.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/019-regression.in new file mode 100644 index 00000000..4cf2197e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/019-regression.in @@ -0,0 +1 @@ +3 3 8 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/019-regression.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/019-regression.out new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/019-regression.out @@ -0,0 +1 @@ +0 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/020-regression.in b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/020-regression.in new file mode 100644 index 00000000..07cc76e2 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/020-regression.in @@ -0,0 +1 @@ +11 27 50 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/020-regression.out b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/020-regression.out new file mode 100644 index 00000000..b8626c4c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/020-regression.out @@ -0,0 +1 @@ +4 diff --git a/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/cases.json b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-14-farmer-cow-bfs/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/Makefile b/labs/chapter-02/exercise/E-02-15-josephus-order/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/README.md b/labs/chapter-02/exercise/E-02-15-josephus-order/README.md new file mode 100644 index 00000000..de0e47c7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/README.md @@ -0,0 +1,77 @@ +--- +title: "Lab 02-E-15:Josephus 出列顺序" +description: "用循环队列模拟报数与出列。" +order: 26 +chapter: 2 +labId: "02E15" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-15:Josephus 出列顺序 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用循环队列模拟报数与出列。 + +## 输入格式 + +一行正整数 `n k`。 + +## 输出格式 + +输出从 1 到 n 的完整出列顺序,各编号以单个空格分隔,末尾输出换行。 + +## 数据范围与限制 + +- 除题面另有说明外整数使用 64 位有符号范围。 +- `1 <= n <= 5000` 且 `1 <= k <= 5000`;报数从当前队头开始,数到 `k` 的元素出列,下一轮从其后一个元素继续。 +- 本题的循环队列模拟允许 `O(nk)` 最坏时间,辅助空间为 `O(n)`。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应符合本题给出的 `O(nk)` 循环队列模拟要求。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-15-josephus-order +pnpm lab:run -- labs/chapter-02/exercise/E-02-15-josephus-order +pnpm lab:score -- labs/chapter-02/exercise/E-02-15-josephus-order +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/lab.json b/labs/chapter-02/exercise/E-02-15-josephus-order/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/solution/main.cpp b/labs/chapter-02/exercise/E-02-15-josephus-order/solution/main.cpp new file mode 100644 index 00000000..c7a53109 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/solution/main.cpp @@ -0,0 +1,14 @@ +#include +#include + +int main() { + int n = 0, step = 0; if (!(std::cin >> n >> step)) return 0; + std::queue circle; for (int i = 1; i <= n; ++i) circle.push(i); + bool first = true; + while (!circle.empty()) { + for (int i = 1; i < step; ++i) { circle.push(circle.front()); circle.pop(); } + if (!first) std::cout << ' '; first = false; + std::cout << circle.front(); circle.pop(); + } + std::cout << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/student/main.cpp b/labs/chapter-02/exercise/E-02-15-josephus-order/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/001-sample.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/001-sample.in new file mode 100644 index 00000000..7f036e7b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/001-sample.in @@ -0,0 +1 @@ +7 3 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/001-sample.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/001-sample.out new file mode 100644 index 00000000..01b53993 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/001-sample.out @@ -0,0 +1 @@ +3 6 2 7 5 1 4 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/002-boundary.in new file mode 100644 index 00000000..2fb73a07 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/002-boundary.in @@ -0,0 +1 @@ +1 1 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/002-boundary.out new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/002-boundary.out @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/003-boundary.in new file mode 100644 index 00000000..2c3e36b5 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/003-boundary.in @@ -0,0 +1 @@ +5 1 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/003-boundary.out new file mode 100644 index 00000000..aacb5952 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/003-boundary.out @@ -0,0 +1 @@ +1 2 3 4 5 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/004-boundary.in new file mode 100644 index 00000000..69010fb2 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/004-boundary.in @@ -0,0 +1 @@ +5 2 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/004-boundary.out new file mode 100644 index 00000000..5e4d8855 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/004-boundary.out @@ -0,0 +1 @@ +2 4 1 5 3 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/005-boundary.in new file mode 100644 index 00000000..299a71f3 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/005-boundary.in @@ -0,0 +1 @@ +5 5 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/005-boundary.out new file mode 100644 index 00000000..31d9d701 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/005-boundary.out @@ -0,0 +1 @@ +5 1 3 4 2 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/006-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/006-normal.in new file mode 100644 index 00000000..395f206d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/006-normal.in @@ -0,0 +1 @@ +10 3 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/006-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/006-normal.out new file mode 100644 index 00000000..ce53a8df --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/006-normal.out @@ -0,0 +1 @@ +3 6 9 2 7 1 8 5 10 4 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/007-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/007-normal.in new file mode 100644 index 00000000..9efb403a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/007-normal.in @@ -0,0 +1 @@ +6 4 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/007-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/007-normal.out new file mode 100644 index 00000000..c1d5f1fb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/007-normal.out @@ -0,0 +1 @@ +4 2 1 3 6 5 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/008-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/008-normal.in new file mode 100644 index 00000000..b931b626 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/008-normal.in @@ -0,0 +1 @@ +8 7 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/008-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/008-normal.out new file mode 100644 index 00000000..c77f02f8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/008-normal.out @@ -0,0 +1 @@ +7 6 8 2 5 1 3 4 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/009-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/009-normal.in new file mode 100644 index 00000000..63eba865 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/009-normal.in @@ -0,0 +1 @@ +9 2 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/009-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/009-normal.out new file mode 100644 index 00000000..b7cd0365 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/009-normal.out @@ -0,0 +1 @@ +2 4 6 8 1 5 9 7 3 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/010-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/010-normal.in new file mode 100644 index 00000000..4e3994f8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/010-normal.in @@ -0,0 +1 @@ +12 5 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/010-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/010-normal.out new file mode 100644 index 00000000..e4869624 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/010-normal.out @@ -0,0 +1 @@ +5 10 3 9 4 12 8 7 11 2 6 1 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/011-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/011-normal.in new file mode 100644 index 00000000..5d737c73 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/011-normal.in @@ -0,0 +1 @@ +3 10 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/011-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/011-normal.out new file mode 100644 index 00000000..d849ff9f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/011-normal.out @@ -0,0 +1 @@ +1 3 2 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/012-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/012-normal.in new file mode 100644 index 00000000..a222f032 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/012-normal.in @@ -0,0 +1 @@ +15 1 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/012-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/012-normal.out new file mode 100644 index 00000000..3311a153 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/012-normal.out @@ -0,0 +1 @@ +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/013-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/013-normal.in new file mode 100644 index 00000000..58683797 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/013-normal.in @@ -0,0 +1 @@ +11 6 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/013-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/013-normal.out new file mode 100644 index 00000000..72b51bcb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/013-normal.out @@ -0,0 +1 @@ +6 1 8 4 2 11 3 7 5 10 9 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/014-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/014-normal.in new file mode 100644 index 00000000..13786522 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/014-normal.in @@ -0,0 +1 @@ +20 3 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/014-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/014-normal.out new file mode 100644 index 00000000..384f2734 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/014-normal.out @@ -0,0 +1 @@ +3 6 9 12 15 18 1 5 10 14 19 4 11 17 7 16 8 2 13 20 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/015-normal.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/015-normal.in new file mode 100644 index 00000000..aaf55727 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/015-normal.in @@ -0,0 +1 @@ +4 9 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/015-normal.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/015-normal.out new file mode 100644 index 00000000..8aa271ad --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/015-normal.out @@ -0,0 +1 @@ +1 4 2 3 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/016-regression.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/016-regression.in new file mode 100644 index 00000000..3de4f6dd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/016-regression.in @@ -0,0 +1 @@ +13 4 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/016-regression.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/016-regression.out new file mode 100644 index 00000000..deb42a8b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/016-regression.out @@ -0,0 +1 @@ +4 8 12 3 9 1 7 2 11 10 13 6 5 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/017-regression.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/017-regression.in new file mode 100644 index 00000000..d4ef52b9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/017-regression.in @@ -0,0 +1 @@ +16 8 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/017-regression.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/017-regression.out new file mode 100644 index 00000000..18fe5ea2 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/017-regression.out @@ -0,0 +1 @@ +8 16 9 2 12 6 3 15 14 1 5 11 10 4 13 7 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/018-regression.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/018-regression.in new file mode 100644 index 00000000..11a0a477 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/018-regression.in @@ -0,0 +1 @@ +2 100 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/018-regression.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/018-regression.out new file mode 100644 index 00000000..cba3471e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/018-regression.out @@ -0,0 +1 @@ +2 1 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/019-regression.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/019-regression.in new file mode 100644 index 00000000..f7f77f4d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/019-regression.in @@ -0,0 +1 @@ +18 7 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/019-regression.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/019-regression.out new file mode 100644 index 00000000..c5c5ed8a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/019-regression.out @@ -0,0 +1 @@ +7 14 3 11 1 10 2 13 6 18 16 15 17 5 12 4 8 9 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/020-regression.in b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/020-regression.in new file mode 100644 index 00000000..13ceeccd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/020-regression.in @@ -0,0 +1 @@ +25 2 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/020-regression.out b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/020-regression.out new file mode 100644 index 00000000..3895dbd4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/020-regression.out @@ -0,0 +1 @@ +2 4 6 8 10 12 14 16 18 20 22 24 1 5 9 13 17 21 25 7 15 23 11 3 19 diff --git a/labs/chapter-02/exercise/E-02-15-josephus-order/tests/cases.json b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-15-josephus-order/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/Makefile b/labs/chapter-02/exercise/E-02-16-binary-number-queue/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/README.md b/labs/chapter-02/exercise/E-02-16-binary-number-queue/README.md new file mode 100644 index 00000000..a5d7504a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/README.md @@ -0,0 +1,76 @@ +--- +title: "Lab 02-E-16:队列生成二进制数" +description: "用 FIFO 队列按数值顺序生成二进制表示。" +order: 27 +chapter: 2 +labId: "02E16" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-16:队列生成二进制数 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用 FIFO 队列按数值顺序生成二进制表示。 + +## 输入格式 + +一行正整数 `n`。 + +## 输出格式 + +输出从 1 到 n 的二进制表示,各表示以单个空格分隔,末尾输出换行。 + +## 数据范围与限制 + +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- `1 <= n <= 200000`;输出总字符数为 `O(n log n)`,算法时间和输出规模同阶,辅助队列空间为 `O(n log n)`。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法时间和输出规模同阶(`O(n log n)`),辅助队列空间为 `O(n log n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-16-binary-number-queue +pnpm lab:run -- labs/chapter-02/exercise/E-02-16-binary-number-queue +pnpm lab:score -- labs/chapter-02/exercise/E-02-16-binary-number-queue +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/lab.json b/labs/chapter-02/exercise/E-02-16-binary-number-queue/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/solution/main.cpp b/labs/chapter-02/exercise/E-02-16-binary-number-queue/solution/main.cpp new file mode 100644 index 00000000..fbcf0d6e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/solution/main.cpp @@ -0,0 +1,14 @@ +#include +#include +#include + +int main() { + int n = 0; if (!(std::cin >> n)) return 0; + std::queue queue; queue.push("1"); + for (int i = 0; i < n; ++i) { + const std::string current = queue.front(); queue.pop(); + if (i) std::cout << ' '; std::cout << current; + queue.push(current + "0"); queue.push(current + "1"); + } + std::cout << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/student/main.cpp b/labs/chapter-02/exercise/E-02-16-binary-number-queue/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/001-sample.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/001-sample.in new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/001-sample.in @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/001-sample.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/001-sample.out new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/001-sample.out @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/002-boundary.in new file mode 100644 index 00000000..0cfbf088 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/002-boundary.in @@ -0,0 +1 @@ +2 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/002-boundary.out new file mode 100644 index 00000000..aba06649 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/002-boundary.out @@ -0,0 +1 @@ +1 10 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/003-boundary.in new file mode 100644 index 00000000..00750edc --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/003-boundary.in @@ -0,0 +1 @@ +3 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/003-boundary.out new file mode 100644 index 00000000..0b4beccb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/003-boundary.out @@ -0,0 +1 @@ +1 10 11 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/004-boundary.in new file mode 100644 index 00000000..b8626c4c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/004-boundary.in @@ -0,0 +1 @@ +4 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/004-boundary.out new file mode 100644 index 00000000..f6de113a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/004-boundary.out @@ -0,0 +1 @@ +1 10 11 100 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/005-boundary.in new file mode 100644 index 00000000..7ed6ff82 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/005-boundary.in @@ -0,0 +1 @@ +5 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/005-boundary.out new file mode 100644 index 00000000..70e5d45e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/005-boundary.out @@ -0,0 +1 @@ +1 10 11 100 101 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/006-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/006-normal.in new file mode 100644 index 00000000..1e8b3149 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/006-normal.in @@ -0,0 +1 @@ +6 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/006-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/006-normal.out new file mode 100644 index 00000000..a6600554 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/006-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/007-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/007-normal.in new file mode 100644 index 00000000..7f8f011e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/007-normal.in @@ -0,0 +1 @@ +7 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/007-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/007-normal.out new file mode 100644 index 00000000..8bd3bd66 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/007-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/008-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/008-normal.in new file mode 100644 index 00000000..45a4fb75 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/008-normal.in @@ -0,0 +1 @@ +8 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/008-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/008-normal.out new file mode 100644 index 00000000..47e268c0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/008-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/009-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/009-normal.in new file mode 100644 index 00000000..ec635144 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/009-normal.in @@ -0,0 +1 @@ +9 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/009-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/009-normal.out new file mode 100644 index 00000000..aefcb79f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/009-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 diff --git a/labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/023-double-plateau.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/010-normal.in similarity index 100% rename from labs/chapter-02/exercise/E-02-08-largest-rectangle-histogram/tests/023-double-plateau.out rename to labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/010-normal.in diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/010-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/010-normal.out new file mode 100644 index 00000000..0d6d0fd2 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/010-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/011-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/011-normal.in new file mode 100644 index 00000000..48082f72 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/011-normal.in @@ -0,0 +1 @@ +12 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/011-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/011-normal.out new file mode 100644 index 00000000..188216f8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/011-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/012-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/012-normal.in new file mode 100644 index 00000000..60d3b2f4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/012-normal.in @@ -0,0 +1 @@ +15 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/012-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/012-normal.out new file mode 100644 index 00000000..c4eab1be --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/012-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/013-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/013-normal.in new file mode 100644 index 00000000..b6a7d89c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/013-normal.in @@ -0,0 +1 @@ +16 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/013-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/013-normal.out new file mode 100644 index 00000000..cd12ac5c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/013-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/014-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/014-normal.in new file mode 100644 index 00000000..209e3ef4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/014-normal.in @@ -0,0 +1 @@ +20 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/014-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/014-normal.out new file mode 100644 index 00000000..e5b6d8c9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/014-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/015-normal.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/015-normal.in new file mode 100644 index 00000000..7273c0fa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/015-normal.in @@ -0,0 +1 @@ +25 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/015-normal.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/015-normal.out new file mode 100644 index 00000000..112e3681 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/015-normal.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/016-regression.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/016-regression.in new file mode 100644 index 00000000..e85087af --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/016-regression.in @@ -0,0 +1 @@ +31 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/016-regression.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/016-regression.out new file mode 100644 index 00000000..9f020a99 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/016-regression.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/017-regression.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/017-regression.in new file mode 100644 index 00000000..f5c89552 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/017-regression.in @@ -0,0 +1 @@ +32 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/017-regression.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/017-regression.out new file mode 100644 index 00000000..e169c0b5 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/017-regression.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111 100000 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/018-regression.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/018-regression.in new file mode 100644 index 00000000..425151f3 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/018-regression.in @@ -0,0 +1 @@ +40 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/018-regression.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/018-regression.out new file mode 100644 index 00000000..a15416c5 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/018-regression.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111 100000 100001 100010 100011 100100 100101 100110 100111 101000 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/019-regression.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/019-regression.in new file mode 100644 index 00000000..e373ee69 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/019-regression.in @@ -0,0 +1 @@ +50 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/019-regression.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/019-regression.out new file mode 100644 index 00000000..a0c4b582 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/019-regression.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111 100000 100001 100010 100011 100100 100101 100110 100111 101000 101001 101010 101011 101100 101101 101110 101111 110000 110001 110010 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/020-regression.in b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/020-regression.in new file mode 100644 index 00000000..4b9026d8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/020-regression.in @@ -0,0 +1 @@ +63 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/020-regression.out b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/020-regression.out new file mode 100644 index 00000000..e1f76619 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/020-regression.out @@ -0,0 +1 @@ +1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111 100000 100001 100010 100011 100100 100101 100110 100111 101000 101001 101010 101011 101100 101101 101110 101111 110000 110001 110010 110011 110100 110101 110110 110111 111000 111001 111010 111011 111100 111101 111110 111111 diff --git a/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/cases.json b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-16-binary-number-queue/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/Makefile b/labs/chapter-02/exercise/E-02-17-palindrome-deque/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/README.md b/labs/chapter-02/exercise/E-02-17-palindrome-deque/README.md new file mode 100644 index 00000000..ad86b958 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/README.md @@ -0,0 +1,75 @@ +--- +title: "Lab 02-E-17:双端队列回文检查" +description: "从两端收缩比较字符,判断字符串是否回文。" +order: 28 +chapter: 2 +labId: "02E17" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-17:双端队列回文检查 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +从两端收缩比较字符,判断字符串是否回文。 + +## 输入格式 + +一行仅含小写字母和数字的非空字符串。 + +## 输出格式 + +回文输出 `YES`,否则输出 `NO`。 + +## 数据范围与限制 + +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-17-palindrome-deque +pnpm lab:run -- labs/chapter-02/exercise/E-02-17-palindrome-deque +pnpm lab:score -- labs/chapter-02/exercise/E-02-17-palindrome-deque +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/lab.json b/labs/chapter-02/exercise/E-02-17-palindrome-deque/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/solution/main.cpp b/labs/chapter-02/exercise/E-02-17-palindrome-deque/solution/main.cpp new file mode 100644 index 00000000..9ad05796 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/solution/main.cpp @@ -0,0 +1,10 @@ +#include +#include + +int main() { + std::string text; if (!(std::cin >> text)) return 0; + std::size_t left = 0, right = text.size(); + bool same = true; + while (left < right) { --right; if (text[left++] != text[right]) { same = false; break; } } + std::cout << (same ? "YES" : "NO") << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/student/main.cpp b/labs/chapter-02/exercise/E-02-17-palindrome-deque/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/001-sample.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/001-sample.in new file mode 100644 index 00000000..78981922 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/001-sample.in @@ -0,0 +1 @@ +a diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/001-sample.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/001-sample.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/001-sample.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/002-boundary.in new file mode 100644 index 00000000..e61ef7b9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/002-boundary.in @@ -0,0 +1 @@ +aa diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/002-boundary.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/002-boundary.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/003-boundary.in new file mode 100644 index 00000000..81bf3969 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/003-boundary.in @@ -0,0 +1 @@ +ab diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/003-boundary.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/003-boundary.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/004-boundary.in new file mode 100644 index 00000000..d9265f82 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/004-boundary.in @@ -0,0 +1 @@ +aba diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/004-boundary.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/004-boundary.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/005-boundary.in new file mode 100644 index 00000000..3656652b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/005-boundary.in @@ -0,0 +1 @@ +abba diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/005-boundary.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/005-boundary.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/006-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/006-normal.in new file mode 100644 index 00000000..2dc757fb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/006-normal.in @@ -0,0 +1 @@ +abcba diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/006-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/006-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/006-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/007-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/007-normal.in new file mode 100644 index 00000000..011bc89a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/007-normal.in @@ -0,0 +1 @@ +abca diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/007-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/007-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/007-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/008-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/008-normal.in new file mode 100644 index 00000000..be91f092 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/008-normal.in @@ -0,0 +1 @@ +12321 diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/008-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/008-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/008-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/009-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/009-normal.in new file mode 100644 index 00000000..66f481a0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/009-normal.in @@ -0,0 +1 @@ +123421 diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/009-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/009-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/009-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/010-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/010-normal.in new file mode 100644 index 00000000..c8d20d38 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/010-normal.in @@ -0,0 +1 @@ +racecar diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/010-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/010-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/010-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/011-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/011-normal.in new file mode 100644 index 00000000..af0f6302 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/011-normal.in @@ -0,0 +1 @@ +level diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/011-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/011-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/011-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/012-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/012-normal.in new file mode 100644 index 00000000..b69c7363 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/012-normal.in @@ -0,0 +1 @@ +stack diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/012-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/012-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/012-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/013-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/013-normal.in new file mode 100644 index 00000000..1fff7bce --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/013-normal.in @@ -0,0 +1 @@ +queue diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/013-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/013-normal.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/013-normal.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/014-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/014-normal.in new file mode 100644 index 00000000..90b45162 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/014-normal.in @@ -0,0 +1 @@ +aaaaaa diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/014-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/014-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/014-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/015-normal.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/015-normal.in new file mode 100644 index 00000000..e3045825 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/015-normal.in @@ -0,0 +1 @@ +abababa diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/015-normal.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/015-normal.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/015-normal.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/016-regression.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/016-regression.in new file mode 100644 index 00000000..753ff4fa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/016-regression.in @@ -0,0 +1 @@ +ababab diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/016-regression.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/016-regression.out new file mode 100644 index 00000000..5e35d1bf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/016-regression.out @@ -0,0 +1 @@ +NO diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/017-regression.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/017-regression.in new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/017-regression.in @@ -0,0 +1 @@ +0 diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/017-regression.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/017-regression.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/017-regression.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/018-regression.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/018-regression.in new file mode 100644 index 00000000..9e8493ea --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/018-regression.in @@ -0,0 +1 @@ +010 diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/018-regression.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/018-regression.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/018-regression.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/019-regression.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/019-regression.in new file mode 100644 index 00000000..2450c585 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/019-regression.in @@ -0,0 +1 @@ +0110 diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/019-regression.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/019-regression.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/019-regression.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/020-regression.in b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/020-regression.in new file mode 100644 index 00000000..decf5e36 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/020-regression.in @@ -0,0 +1 @@ +01110 diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/020-regression.out b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/020-regression.out new file mode 100644 index 00000000..f033a501 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/020-regression.out @@ -0,0 +1 @@ +YES diff --git a/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/cases.json b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-17-palindrome-deque/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/Makefile b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/README.md b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/README.md new file mode 100644 index 00000000..51480160 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/README.md @@ -0,0 +1,75 @@ +--- +title: "Lab 02-E-18:反转队列前 K 个元素" +description: "借助栈反转队头前缀并保持剩余元素相对顺序。" +order: 29 +chapter: 2 +labId: "02E18" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-18:反转队列前 K 个元素 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +借助栈反转队头前缀并保持剩余元素相对顺序。 + +## 输入格式 + +第一行 `n k`;第二行 `n` 个整数,`0 <= k <= n`。 + +## 输出格式 + +输出操作后的整个队列,各元素以单个空格分隔,末尾输出换行。 + +## 数据范围与限制 + +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-18-reverse-first-k-queue +pnpm lab:run -- labs/chapter-02/exercise/E-02-18-reverse-first-k-queue +pnpm lab:score -- labs/chapter-02/exercise/E-02-18-reverse-first-k-queue +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/lab.json b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/solution/main.cpp b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/solution/main.cpp new file mode 100644 index 00000000..0aa8cbae --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/solution/main.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +int main() { + std::size_t n = 0, k = 0; if (!(std::cin >> n >> k)) return 0; + std::queue queue; for (std::size_t i = 0; i < n; ++i) { long long x = 0; std::cin >> x; queue.push(x); } + std::stack stack; for (std::size_t i = 0; i < k; ++i) { stack.push(queue.front()); queue.pop(); } + while (!stack.empty()) { queue.push(stack.top()); stack.pop(); } + for (std::size_t i = k; i < n; ++i) { queue.push(queue.front()); queue.pop(); } + for (std::size_t i = 0; i < n; ++i) { if (i) std::cout << ' '; std::cout << queue.front(); queue.pop(); } + std::cout << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/student/main.cpp b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/001-sample.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/001-sample.in new file mode 100644 index 00000000..a7149637 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/001-sample.in @@ -0,0 +1,2 @@ +1 0 +1 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/001-sample.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/001-sample.out new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/001-sample.out @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/002-boundary.in new file mode 100644 index 00000000..8004acab --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/002-boundary.in @@ -0,0 +1,2 @@ +2 1 +2 1 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/002-boundary.out new file mode 100644 index 00000000..cba3471e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/002-boundary.out @@ -0,0 +1 @@ +2 1 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/003-boundary.in new file mode 100644 index 00000000..b76f1251 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/003-boundary.in @@ -0,0 +1,2 @@ +2 2 +1 2 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/003-boundary.out new file mode 100644 index 00000000..cba3471e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/003-boundary.out @@ -0,0 +1 @@ +2 1 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/004-boundary.in new file mode 100644 index 00000000..035bac8b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/004-boundary.in @@ -0,0 +1,2 @@ +2 0 +2 2 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/004-boundary.out new file mode 100644 index 00000000..78454aab --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/004-boundary.out @@ -0,0 +1 @@ +2 2 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/005-boundary.in new file mode 100644 index 00000000..b2a1e5cf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/005-boundary.in @@ -0,0 +1,2 @@ +3 0 +2 1 2 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/005-boundary.out new file mode 100644 index 00000000..c3b04ccd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/005-boundary.out @@ -0,0 +1 @@ +2 1 2 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/006-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/006-normal.in new file mode 100644 index 00000000..1edb8071 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/006-normal.in @@ -0,0 +1,2 @@ +5 5 +5 1 4 2 6 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/006-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/006-normal.out new file mode 100644 index 00000000..4c100d85 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/006-normal.out @@ -0,0 +1 @@ +6 2 4 1 5 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/007-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/007-normal.in new file mode 100644 index 00000000..8337c068 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/007-normal.in @@ -0,0 +1,2 @@ +6 6 +6 5 4 3 2 1 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/007-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/007-normal.out new file mode 100644 index 00000000..b61b7c26 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/007-normal.out @@ -0,0 +1 @@ +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/008-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/008-normal.in new file mode 100644 index 00000000..991ec4a0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/008-normal.in @@ -0,0 +1,2 @@ +6 0 +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/008-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/008-normal.out new file mode 100644 index 00000000..b61b7c26 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/008-normal.out @@ -0,0 +1 @@ +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/009-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/009-normal.in new file mode 100644 index 00000000..edd2733c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/009-normal.in @@ -0,0 +1,2 @@ +4 3 +-1 -3 -2 -4 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/009-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/009-normal.out new file mode 100644 index 00000000..d2f68ace --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/009-normal.out @@ -0,0 +1 @@ +-2 -3 -1 -4 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/010-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/010-normal.in new file mode 100644 index 00000000..7c9ea445 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/010-normal.in @@ -0,0 +1,2 @@ +4 4 +0 0 0 0 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/010-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/010-normal.out new file mode 100644 index 00000000..6a20ce40 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/010-normal.out @@ -0,0 +1 @@ +0 0 0 0 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/011-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/011-normal.in new file mode 100644 index 00000000..b349a63e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/011-normal.in @@ -0,0 +1,2 @@ +5 4 +7 1 7 1 7 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/011-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/011-normal.out new file mode 100644 index 00000000..ba962b93 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/011-normal.out @@ -0,0 +1 @@ +1 7 1 7 7 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/012-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/012-normal.in new file mode 100644 index 00000000..22971fc6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/012-normal.in @@ -0,0 +1,2 @@ +6 4 +3 8 2 9 1 10 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/012-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/012-normal.out new file mode 100644 index 00000000..d9bf442c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/012-normal.out @@ -0,0 +1 @@ +9 2 8 3 1 10 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/013-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/013-normal.in new file mode 100644 index 00000000..a988c109 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/013-normal.in @@ -0,0 +1,2 @@ +5 0 +10 9 8 7 6 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/013-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/013-normal.out new file mode 100644 index 00000000..271bb735 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/013-normal.out @@ -0,0 +1 @@ +10 9 8 7 6 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/014-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/014-normal.in new file mode 100644 index 00000000..325c0e0b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/014-normal.in @@ -0,0 +1,2 @@ +5 1 +4 1 2 3 0 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/014-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/014-normal.out new file mode 100644 index 00000000..93f533b2 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/014-normal.out @@ -0,0 +1 @@ +4 1 2 3 0 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/015-normal.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/015-normal.in new file mode 100644 index 00000000..040619ca --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/015-normal.in @@ -0,0 +1,2 @@ +5 2 +100 -100 50 -50 0 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/015-normal.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/015-normal.out new file mode 100644 index 00000000..a0245e06 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/015-normal.out @@ -0,0 +1 @@ +-100 100 50 -50 0 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/016-regression.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/016-regression.in new file mode 100644 index 00000000..e760cbc7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/016-regression.in @@ -0,0 +1,2 @@ +7 7 +9 1 8 2 7 3 6 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/016-regression.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/016-regression.out new file mode 100644 index 00000000..c796e597 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/016-regression.out @@ -0,0 +1 @@ +6 3 7 2 8 1 9 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/017-regression.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/017-regression.in new file mode 100644 index 00000000..73ebdb90 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/017-regression.in @@ -0,0 +1,2 @@ +6 2 +1 3 2 4 3 5 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/017-regression.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/017-regression.out new file mode 100644 index 00000000..62f559e9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/017-regression.out @@ -0,0 +1 @@ +3 1 2 4 3 5 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/018-regression.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/018-regression.in new file mode 100644 index 00000000..55228578 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/018-regression.in @@ -0,0 +1,2 @@ +6 3 +5 5 4 4 3 3 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/018-regression.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/018-regression.out new file mode 100644 index 00000000..8bfdcdd4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/018-regression.out @@ -0,0 +1 @@ +4 5 5 4 3 3 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/019-regression.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/019-regression.in new file mode 100644 index 00000000..cfd03c32 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/019-regression.in @@ -0,0 +1,2 @@ +6 4 +2 9 1 8 0 7 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/019-regression.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/019-regression.out new file mode 100644 index 00000000..62beeb68 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/019-regression.out @@ -0,0 +1 @@ +8 1 9 2 0 7 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/020-regression.in b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/020-regression.in new file mode 100644 index 00000000..75a307d9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/020-regression.in @@ -0,0 +1,2 @@ +7 3 +11 4 12 3 13 2 14 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/020-regression.out b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/020-regression.out new file mode 100644 index 00000000..9f1188f0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/020-regression.out @@ -0,0 +1 @@ +12 4 11 3 13 2 14 diff --git a/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/cases.json b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-18-reverse-first-k-queue/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/Makefile b/labs/chapter-02/exercise/E-02-19-stack-sorting/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/README.md b/labs/chapter-02/exercise/E-02-19-stack-sorting/README.md new file mode 100644 index 00000000..86489177 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/README.md @@ -0,0 +1,76 @@ +--- +title: "Lab 02-E-19:仅用辅助栈排序" +description: "用一个辅助栈完成整数序列的升序输出。" +order: 30 +chapter: 2 +labId: "02E19" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-19:仅用辅助栈排序 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用一个辅助栈完成整数序列的升序输出。 + +## 输入格式 + +第一行 `n`;第二行 `n` 个整数,按给定顺序依次入栈。 + +## 输出格式 + +输出从小到大的排序结果,各元素以单个空格分隔,末尾输出换行。 + +## 数据范围与限制 + +- 除题面另有说明外整数使用 64 位有符号范围。 +- `0 <= n <= 5000`;仅允许使用输入栈和一个辅助栈完成排序,栈排序最坏时间为 `O(n^2)`,辅助空间为 `O(n)`。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法使用一个辅助栈完成排序,最坏时间 `O(n^2)`,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-19-stack-sorting +pnpm lab:run -- labs/chapter-02/exercise/E-02-19-stack-sorting +pnpm lab:score -- labs/chapter-02/exercise/E-02-19-stack-sorting +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/lab.json b/labs/chapter-02/exercise/E-02-19-stack-sorting/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/solution/main.cpp b/labs/chapter-02/exercise/E-02-19-stack-sorting/solution/main.cpp new file mode 100644 index 00000000..803e4398 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/solution/main.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +int main() { + std::size_t n = 0; if (!(std::cin >> n)) return 0; + std::stack source, helper; + for (std::size_t i = 0; i < n; ++i) { long long x = 0; std::cin >> x; source.push(x); } + while (!source.empty()) { + const long long current = source.top(); source.pop(); + while (!helper.empty() && helper.top() > current) { source.push(helper.top()); helper.pop(); } + helper.push(current); + } + // Move the sorted stack back to the input stack so its top is the smallest value. + while (!helper.empty()) { source.push(helper.top()); helper.pop(); } + for (std::size_t i = 0; i < n; ++i) { + if (i) std::cout << ' '; + std::cout << source.top(); + source.pop(); + } + std::cout << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/student/main.cpp b/labs/chapter-02/exercise/E-02-19-stack-sorting/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/001-sample.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/001-sample.in new file mode 100644 index 00000000..6ed281c7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/001-sample.in @@ -0,0 +1,2 @@ +1 +1 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/001-sample.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/001-sample.out new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/001-sample.out @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/002-boundary.in new file mode 100644 index 00000000..65cd4f58 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/002-boundary.in @@ -0,0 +1,2 @@ +2 +2 1 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/002-boundary.out new file mode 100644 index 00000000..8d04f961 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/002-boundary.out @@ -0,0 +1 @@ +1 2 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/003-boundary.in new file mode 100644 index 00000000..a789e877 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/003-boundary.in @@ -0,0 +1,2 @@ +2 +1 2 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/003-boundary.out new file mode 100644 index 00000000..8d04f961 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/003-boundary.out @@ -0,0 +1 @@ +1 2 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/004-boundary.in new file mode 100644 index 00000000..2d777856 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/004-boundary.in @@ -0,0 +1,2 @@ +2 +2 2 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/004-boundary.out new file mode 100644 index 00000000..78454aab --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/004-boundary.out @@ -0,0 +1 @@ +2 2 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/005-boundary.in new file mode 100644 index 00000000..8b98c7f1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/005-boundary.in @@ -0,0 +1,2 @@ +3 +2 1 2 diff --git a/labs/chapter-02/exercise/E-02-03-recent-counter/tests/023-large-timestamps.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/005-boundary.out similarity index 100% rename from labs/chapter-02/exercise/E-02-03-recent-counter/tests/023-large-timestamps.out rename to labs/chapter-02/exercise/E-02-19-stack-sorting/tests/005-boundary.out diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/006-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/006-normal.in new file mode 100644 index 00000000..cca7fe34 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/006-normal.in @@ -0,0 +1,2 @@ +5 +5 1 4 2 6 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/006-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/006-normal.out new file mode 100644 index 00000000..2dae32d1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/006-normal.out @@ -0,0 +1 @@ +1 2 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/007-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/007-normal.in new file mode 100644 index 00000000..8db8ab6d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/007-normal.in @@ -0,0 +1,2 @@ +6 +6 5 4 3 2 1 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/007-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/007-normal.out new file mode 100644 index 00000000..b61b7c26 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/007-normal.out @@ -0,0 +1 @@ +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/008-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/008-normal.in new file mode 100644 index 00000000..5ee886e1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/008-normal.in @@ -0,0 +1,2 @@ +6 +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/008-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/008-normal.out new file mode 100644 index 00000000..b61b7c26 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/008-normal.out @@ -0,0 +1 @@ +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/009-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/009-normal.in new file mode 100644 index 00000000..5aab6eaa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/009-normal.in @@ -0,0 +1,2 @@ +4 +-1 -3 -2 -4 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/009-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/009-normal.out new file mode 100644 index 00000000..1190f593 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/009-normal.out @@ -0,0 +1 @@ +-4 -3 -2 -1 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/010-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/010-normal.in new file mode 100644 index 00000000..3f62807b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/010-normal.in @@ -0,0 +1,2 @@ +4 +0 0 0 0 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/010-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/010-normal.out new file mode 100644 index 00000000..6a20ce40 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/010-normal.out @@ -0,0 +1 @@ +0 0 0 0 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/011-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/011-normal.in new file mode 100644 index 00000000..481b7578 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/011-normal.in @@ -0,0 +1,2 @@ +5 +7 1 7 1 7 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/011-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/011-normal.out new file mode 100644 index 00000000..3944c011 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/011-normal.out @@ -0,0 +1 @@ +1 1 7 7 7 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/012-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/012-normal.in new file mode 100644 index 00000000..824a30e0 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/012-normal.in @@ -0,0 +1,2 @@ +6 +3 8 2 9 1 10 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/012-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/012-normal.out new file mode 100644 index 00000000..3e5f0b1b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/012-normal.out @@ -0,0 +1 @@ +1 2 3 8 9 10 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/013-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/013-normal.in new file mode 100644 index 00000000..ece8fbdb --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/013-normal.in @@ -0,0 +1,2 @@ +5 +10 9 8 7 6 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/013-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/013-normal.out new file mode 100644 index 00000000..2134a099 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/013-normal.out @@ -0,0 +1 @@ +6 7 8 9 10 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/014-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/014-normal.in new file mode 100644 index 00000000..8215b61b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/014-normal.in @@ -0,0 +1,2 @@ +5 +4 1 2 3 0 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/014-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/014-normal.out new file mode 100644 index 00000000..926717b1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/014-normal.out @@ -0,0 +1 @@ +0 1 2 3 4 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/015-normal.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/015-normal.in new file mode 100644 index 00000000..73c8c8e6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/015-normal.in @@ -0,0 +1,2 @@ +5 +100 -100 50 -50 0 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/015-normal.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/015-normal.out new file mode 100644 index 00000000..29be3a41 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/015-normal.out @@ -0,0 +1 @@ +-100 -50 0 50 100 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/016-regression.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/016-regression.in new file mode 100644 index 00000000..430eb31e --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/016-regression.in @@ -0,0 +1,2 @@ +7 +9 1 8 2 7 3 6 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/016-regression.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/016-regression.out new file mode 100644 index 00000000..20c03c80 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/016-regression.out @@ -0,0 +1 @@ +1 2 3 6 7 8 9 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/017-regression.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/017-regression.in new file mode 100644 index 00000000..49184691 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/017-regression.in @@ -0,0 +1,2 @@ +6 +1 3 2 4 3 5 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/017-regression.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/017-regression.out new file mode 100644 index 00000000..4faad4da --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/017-regression.out @@ -0,0 +1 @@ +1 2 3 3 4 5 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/018-regression.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/018-regression.in new file mode 100644 index 00000000..efa0c2e6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/018-regression.in @@ -0,0 +1,2 @@ +6 +5 5 4 4 3 3 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/018-regression.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/018-regression.out new file mode 100644 index 00000000..57fdeec7 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/018-regression.out @@ -0,0 +1 @@ +3 3 4 4 5 5 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/019-regression.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/019-regression.in new file mode 100644 index 00000000..cfc22005 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/019-regression.in @@ -0,0 +1,2 @@ +6 +2 9 1 8 0 7 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/019-regression.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/019-regression.out new file mode 100644 index 00000000..2096d765 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/019-regression.out @@ -0,0 +1 @@ +0 1 2 7 8 9 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/020-regression.in b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/020-regression.in new file mode 100644 index 00000000..08c96f86 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/020-regression.in @@ -0,0 +1,2 @@ +7 +11 4 12 3 13 2 14 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/020-regression.out b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/020-regression.out new file mode 100644 index 00000000..e95d80dc --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/020-regression.out @@ -0,0 +1 @@ +2 3 4 11 12 13 14 diff --git a/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/cases.json b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-19-stack-sorting/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/Makefile b/labs/chapter-02/exercise/E-02-20-stock-span/Makefile new file mode 100644 index 00000000..314d5329 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/Makefile @@ -0,0 +1,3 @@ +LAB_DIR := $(CURDIR) +REPO_ROOT := $(LAB_DIR)/../../../.. +include ../../../../tools/lab/lab.mk diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/README.md b/labs/chapter-02/exercise/E-02-20-stock-span/README.md new file mode 100644 index 00000000..f0c27d5c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/README.md @@ -0,0 +1,76 @@ +--- +title: "Lab 02-E-20:股票价格跨度" +description: "用单调栈计算连续不高于当天价格的天数。" +order: 31 +chapter: 2 +labId: "02E20" +chapterTitle: "栈与队列" +updated: "2026-09-11" +contributors: ["DSA Mastery Team"] +status: "draft" +lab: true +difficulty: "基础" +duration: "35~50 分钟" +--- + +# Lab 02-E-20:股票价格跨度 + +## 学习目标 + +- 用栈、队列或双端队列维护本题要求的访问顺序。 +- 处理正常、边界和失败/非法输入场景。 +- 说明每个元素至多进入和离开辅助结构一次的复杂度原因。 + +## 前置知识与环境 + +建议先学习[第 2 章栈与队列](../../../../content/chapter-02-stack-queue/00-overview.md)。使用 ISO C++17;开始前可在本目录运行 `make doctor`。 + +## 任务 + +用单调栈计算连续不高于当天价格的天数。 + +## 输入格式 + +第一行 `n`;第二行 `n` 个正整数价格。 + +## 输出格式 + +输出每一天的价格跨度,各数值以单个空格分隔,末尾输出换行。 + +## 数据范围与限制 + +- `1 <= n <= 200000`,且每个价格为正整数。 +- 输入规模不超过 `200000`,除题面另有说明外整数使用 64 位有符号范围。 +- 标准输出只保留题目要求的结果;调试信息写入标准错误。 +- 目标算法应为线性或摊还线性时间,辅助空间不超过 `O(n)`。 + +## 边界与验收重点 + +- 最小规模、单元素和重复元素。 +- 结构为空、候选全部过期或没有可匹配对象的情况。 +- 极值、嵌套结构和混合操作后的状态一致性。 + +## 如何验证 + +```powershell +make doctor +make run +make score +``` + +未安装 Make 时,在仓库根目录运行: + +```powershell +pnpm lab:validate -- labs/chapter-02/exercise/E-02-20-stock-span +pnpm lab:run -- labs/chapter-02/exercise/E-02-20-stock-span +pnpm lab:score -- labs/chapter-02/exercise/E-02-20-stock-span +``` + +- [ ] 全部 20 个公开测试通过。 +- [ ] 能解释所选结构为何满足 LIFO/FIFO/单调候选的题意。 +- [ ] 能说明最坏或摊还时间复杂度。 + +## 思考与复盘 + +1. 若把本题核心结构换成相反的访问顺序,会在哪类输入上出错? +2. 哪个边界用例最能揭示结构不变量被破坏? diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/lab.json b/labs/chapter-02/exercise/E-02-20-stock-span/lab.json new file mode 100644 index 00000000..34bbd148 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/lab.json @@ -0,0 +1,33 @@ +{ + "$schema": "../../../../schemas/lab.schema.json", + "schemaVersion": 1, + "type": "program", + "language": "cpp", + "toolchain": { + "standard": "c++17", + "profile": "course-default" + }, + "targets": { + "student": { + "sources": [ + "student/main.cpp" + ] + }, + "solution": { + "sources": [ + "solution/main.cpp" + ] + } + }, + "judge": { + "kind": "stdio", + "cases": "tests/cases.json", + "compare": { + "mode": "tokens" + }, + "limits": { + "timeMs": 2000, + "outputKb": 1024 + } + } +} diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/solution/main.cpp b/labs/chapter-02/exercise/E-02-20-stock-span/solution/main.cpp new file mode 100644 index 00000000..42cada5f --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/solution/main.cpp @@ -0,0 +1,15 @@ +#include +#include +#include + +int main() { + std::size_t n = 0; if (!(std::cin >> n)) return 0; + std::vector price(n); std::vector stack; + for (auto& x : price) std::cin >> x; + for (std::size_t i = 0; i < n; ++i) { + while (!stack.empty() && price[stack.back()] <= price[i]) stack.pop_back(); + const std::size_t span = stack.empty() ? i + 1 : i - stack.back(); + if (i) std::cout << ' '; std::cout << span; stack.push_back(i); + } + std::cout << '\n'; +} diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/student/main.cpp b/labs/chapter-02/exercise/E-02-20-stock-span/student/main.cpp new file mode 100644 index 00000000..701993a6 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/student/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + // TODO: 读取输入并完成任务。学生骨架应可编译,但不能直接得到满分。 + return 0; +} diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/001-sample.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/001-sample.in new file mode 100644 index 00000000..1191247b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/001-sample.in @@ -0,0 +1,2 @@ +1 +2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/001-sample.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/001-sample.out new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/001-sample.out @@ -0,0 +1 @@ +1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/002-boundary.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/002-boundary.in new file mode 100644 index 00000000..9f411815 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/002-boundary.in @@ -0,0 +1,2 @@ +2 +3 2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/002-boundary.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/002-boundary.out new file mode 100644 index 00000000..2fb73a07 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/002-boundary.out @@ -0,0 +1 @@ +1 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/003-boundary.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/003-boundary.in new file mode 100644 index 00000000..c6b80dc1 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/003-boundary.in @@ -0,0 +1,2 @@ +2 +2 3 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/003-boundary.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/003-boundary.out new file mode 100644 index 00000000..8d04f961 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/003-boundary.out @@ -0,0 +1 @@ +1 2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/004-boundary.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/004-boundary.in new file mode 100644 index 00000000..da7e2683 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/004-boundary.in @@ -0,0 +1,2 @@ +2 +3 3 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/004-boundary.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/004-boundary.out new file mode 100644 index 00000000..8d04f961 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/004-boundary.out @@ -0,0 +1 @@ +1 2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/005-boundary.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/005-boundary.in new file mode 100644 index 00000000..60255dc3 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/005-boundary.in @@ -0,0 +1,2 @@ +3 +3 2 3 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/005-boundary.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/005-boundary.out new file mode 100644 index 00000000..363c9123 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/005-boundary.out @@ -0,0 +1 @@ +1 1 3 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/006-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/006-normal.in new file mode 100644 index 00000000..1d932d58 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/006-normal.in @@ -0,0 +1,2 @@ +5 +6 2 5 3 7 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/006-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/006-normal.out new file mode 100644 index 00000000..f3f497b4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/006-normal.out @@ -0,0 +1 @@ +1 1 2 1 5 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/007-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/007-normal.in new file mode 100644 index 00000000..d519b4f4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/007-normal.in @@ -0,0 +1,2 @@ +6 +7 6 5 4 3 2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/007-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/007-normal.out new file mode 100644 index 00000000..73c3ca10 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/007-normal.out @@ -0,0 +1 @@ +1 1 1 1 1 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/008-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/008-normal.in new file mode 100644 index 00000000..9857460a --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/008-normal.in @@ -0,0 +1,2 @@ +6 +2 3 4 5 6 7 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/008-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/008-normal.out new file mode 100644 index 00000000..b61b7c26 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/008-normal.out @@ -0,0 +1 @@ +1 2 3 4 5 6 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/009-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/009-normal.in new file mode 100644 index 00000000..5ce6e0a9 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/009-normal.in @@ -0,0 +1,2 @@ +4 +2 4 3 5 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/009-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/009-normal.out new file mode 100644 index 00000000..e8d7cd80 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/009-normal.out @@ -0,0 +1 @@ +1 2 1 4 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/010-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/010-normal.in new file mode 100644 index 00000000..6c95aee8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/010-normal.in @@ -0,0 +1,2 @@ +4 +1 1 1 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/010-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/010-normal.out new file mode 100644 index 00000000..93a48451 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/010-normal.out @@ -0,0 +1 @@ +1 2 3 4 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/011-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/011-normal.in new file mode 100644 index 00000000..243d2fdf --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/011-normal.in @@ -0,0 +1,2 @@ +5 +8 2 8 2 8 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/011-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/011-normal.out new file mode 100644 index 00000000..e4407ba8 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/011-normal.out @@ -0,0 +1 @@ +1 1 3 1 5 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/012-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/012-normal.in new file mode 100644 index 00000000..61d64a95 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/012-normal.in @@ -0,0 +1,2 @@ +6 +4 9 3 10 2 11 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/012-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/012-normal.out new file mode 100644 index 00000000..3e81cfbd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/012-normal.out @@ -0,0 +1 @@ +1 2 1 4 1 6 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/013-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/013-normal.in new file mode 100644 index 00000000..2e1b7a4d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/013-normal.in @@ -0,0 +1,2 @@ +5 +11 10 9 8 7 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/013-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/013-normal.out new file mode 100644 index 00000000..ac7d099c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/013-normal.out @@ -0,0 +1 @@ +1 1 1 1 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/014-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/014-normal.in new file mode 100644 index 00000000..be94e490 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/014-normal.in @@ -0,0 +1,2 @@ +5 +5 2 3 4 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/014-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/014-normal.out new file mode 100644 index 00000000..25af3a39 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/014-normal.out @@ -0,0 +1 @@ +1 1 2 3 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/015-normal.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/015-normal.in new file mode 100644 index 00000000..7ceea766 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/015-normal.in @@ -0,0 +1,2 @@ +5 +101 101 51 51 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/015-normal.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/015-normal.out new file mode 100644 index 00000000..7679d27d --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/015-normal.out @@ -0,0 +1 @@ +1 2 1 2 1 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/016-regression.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/016-regression.in new file mode 100644 index 00000000..1a146b89 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/016-regression.in @@ -0,0 +1,2 @@ +7 +10 2 9 3 8 4 7 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/016-regression.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/016-regression.out new file mode 100644 index 00000000..a073493c --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/016-regression.out @@ -0,0 +1 @@ +1 1 2 1 2 1 2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/017-regression.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/017-regression.in new file mode 100644 index 00000000..fe1b80aa --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/017-regression.in @@ -0,0 +1,2 @@ +6 +2 4 3 5 4 6 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/017-regression.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/017-regression.out new file mode 100644 index 00000000..3e81cfbd --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/017-regression.out @@ -0,0 +1 @@ +1 2 1 4 1 6 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/018-regression.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/018-regression.in new file mode 100644 index 00000000..4de578f3 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/018-regression.in @@ -0,0 +1,2 @@ +6 +6 6 5 5 4 4 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/018-regression.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/018-regression.out new file mode 100644 index 00000000..c4bc6443 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/018-regression.out @@ -0,0 +1 @@ +1 2 1 2 1 2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/019-regression.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/019-regression.in new file mode 100644 index 00000000..7a0bff9b --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/019-regression.in @@ -0,0 +1,2 @@ +6 +3 10 2 9 1 8 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/019-regression.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/019-regression.out new file mode 100644 index 00000000..c4bc6443 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/019-regression.out @@ -0,0 +1 @@ +1 2 1 2 1 2 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/020-regression.in b/labs/chapter-02/exercise/E-02-20-stock-span/tests/020-regression.in new file mode 100644 index 00000000..5bff3c53 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/020-regression.in @@ -0,0 +1,2 @@ +7 +12 5 13 4 14 3 15 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/020-regression.out b/labs/chapter-02/exercise/E-02-20-stock-span/tests/020-regression.out new file mode 100644 index 00000000..a5cb6a04 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/020-regression.out @@ -0,0 +1 @@ +1 1 3 1 5 1 7 diff --git a/labs/chapter-02/exercise/E-02-20-stock-span/tests/cases.json b/labs/chapter-02/exercise/E-02-20-stock-span/tests/cases.json new file mode 100644 index 00000000..4859c8e4 --- /dev/null +++ b/labs/chapter-02/exercise/E-02-20-stock-span/tests/cases.json @@ -0,0 +1,182 @@ +[ + { + "id": "001-sample", + "input": "tests/001-sample.in", + "expected": "tests/001-sample.out", + "points": 8, + "tags": [ + "sample" + ] + }, + { + "id": "002-boundary", + "input": "tests/002-boundary.in", + "expected": "tests/002-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "003-boundary", + "input": "tests/003-boundary.in", + "expected": "tests/003-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "004-boundary", + "input": "tests/004-boundary.in", + "expected": "tests/004-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "005-boundary", + "input": "tests/005-boundary.in", + "expected": "tests/005-boundary.out", + "points": 8, + "tags": [ + "boundary" + ] + }, + { + "id": "006-normal", + "input": "tests/006-normal.in", + "expected": "tests/006-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "007-normal", + "input": "tests/007-normal.in", + "expected": "tests/007-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "008-normal", + "input": "tests/008-normal.in", + "expected": "tests/008-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "009-normal", + "input": "tests/009-normal.in", + "expected": "tests/009-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "010-normal", + "input": "tests/010-normal.in", + "expected": "tests/010-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "011-normal", + "input": "tests/011-normal.in", + "expected": "tests/011-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "012-normal", + "input": "tests/012-normal.in", + "expected": "tests/012-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "013-normal", + "input": "tests/013-normal.in", + "expected": "tests/013-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "014-normal", + "input": "tests/014-normal.in", + "expected": "tests/014-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "015-normal", + "input": "tests/015-normal.in", + "expected": "tests/015-normal.out", + "points": 4, + "tags": [ + "normal" + ] + }, + { + "id": "016-regression", + "input": "tests/016-regression.in", + "expected": "tests/016-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "017-regression", + "input": "tests/017-regression.in", + "expected": "tests/017-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "018-regression", + "input": "tests/018-regression.in", + "expected": "tests/018-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "019-regression", + "input": "tests/019-regression.in", + "expected": "tests/019-regression.out", + "points": 4, + "tags": [ + "regression" + ] + }, + { + "id": "020-regression", + "input": "tests/020-regression.in", + "expected": "tests/020-regression.out", + "points": 4, + "tags": [ + "regression" + ] + } +] diff --git a/tests/pages-navigation.spec.mjs b/tests/pages-navigation.spec.mjs index ba26b2de..19d153de 100644 --- a/tests/pages-navigation.spec.mjs +++ b/tests/pages-navigation.spec.mjs @@ -1251,7 +1251,7 @@ test("chapter 2 Lab sidebar groups labs into categorized 本章 Labs", async ({ await page.keyboard.press("Enter"); await expect(exerciseGroup).not.toHaveClass(/collapsed/); await expect(exerciseGroup.locator(".course-lab-category__empty")).toHaveCount(0); - await expect(exerciseGroup.locator(":scope > .items a")).toHaveCount(8); + await expect(exerciseGroup.locator(":scope > .items a")).toHaveCount(20); const exerciseLabs = [ { title: "Lab 02-E-01:验证栈序列", slug: "exercise/E-02-01-validate-stack-sequences" }, { title: "Lab 02-E-02:最小栈", slug: "exercise/E-02-02-min-stack" }, @@ -1264,6 +1264,18 @@ test("chapter 2 Lab sidebar groups labs into categorized 本章 Labs", async ({ title: "Lab 02-E-08:柱状图中最大的矩形", slug: "exercise/E-02-08-largest-rectangle-histogram", }, + { title: "Lab 02-E-09:括号匹配", slug: "exercise/E-02-09-bracket-matching" }, + { title: "Lab 02-E-10:逆波兰表达式求值", slug: "exercise/E-02-10-rpn-evaluation" }, + { title: "Lab 02-E-11:中缀表达式转后缀", slug: "exercise/E-02-11-infix-to-postfix" }, + { title: "Lab 02-E-12:下一个更大元素", slug: "exercise/E-02-12-next-greater-element" }, + { title: "Lab 02-E-13:下一个更小元素", slug: "exercise/E-02-13-next-smaller-element" }, + { title: "Lab 02-E-14:农夫抓牛", slug: "exercise/E-02-14-farmer-cow-bfs" }, + { title: "Lab 02-E-15:Josephus 出列顺序", slug: "exercise/E-02-15-josephus-order" }, + { title: "Lab 02-E-16:队列生成二进制数", slug: "exercise/E-02-16-binary-number-queue" }, + { title: "Lab 02-E-17:双端队列回文检查", slug: "exercise/E-02-17-palindrome-deque" }, + { title: "Lab 02-E-18:反转队列前 K 个元素", slug: "exercise/E-02-18-reverse-first-k-queue" }, + { title: "Lab 02-E-19:仅用辅助栈排序", slug: "exercise/E-02-19-stack-sorting" }, + { title: "Lab 02-E-20:股票价格跨度", slug: "exercise/E-02-20-stock-span" }, ]; for (const { title, slug } of exerciseLabs) { const link = exerciseGroup.getByRole("link", { name: labSidebarTitle(title) });