Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
師大資工系ACM-ICPC讀書會:參、堆疊與佇列
Search
Maplewing
October 25, 2013
Programming
240
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
師大資工系ACM-ICPC讀書會:參、堆疊與佇列
Maplewing
October 25, 2013
More Decks by Maplewing
See All by Maplewing
師大資工系ACM-ICPC讀書會:肆、鏈結串列與二元樹
sinmaplewing
0
150
資訊實務應用讀書會 第七堂課:jQuery
sinmaplewing
0
2.1k
師大資工系ACM-ICPC讀書會:貳、排序搜尋與數學基礎
sinmaplewing
0
200
資訊實務應用讀書會 第六堂課:Basic CSS
sinmaplewing
0
1.7k
師大資工系ACM-ICPC讀書會:壹、字串處理與大數運算
sinmaplewing
0
150
師大資工系ACM-ICPC讀書會:零、比賽介紹、解題系統介紹
sinmaplewing
0
230
資訊實務應用讀書會 第四堂課(支線):Linux
sinmaplewing
0
1.9k
資訊實務應用讀書會 第一堂課:Basic HTML
sinmaplewing
1
2.1k
Other Decks in Programming
See All in Programming
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
240
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
540
Inside Stream API
skrb
1
690
CSC307 Lecture 17
javiergs
PRO
0
320
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
2
660
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
340
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
150
RTSPクライアントを自作してみた話
simotin13
0
600
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
3.7k
AI時代のUIはどこへ行く?その2!
yusukebe
21
7.1k
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
190
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
150
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
210k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
840
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
320
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Six Lessons from altMBA
skipperchong
29
4.3k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
940
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
200
Test your architecture with Archunit
thirion
1
2.3k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Heart Work Chapter 1 - Part 1
lfama
PRO
7
36k
Transcript
參、堆疊與佇列 師大資工系 灆洢 取自於<提升程式設計的邏輯思考力> 6.1
堆疊
•先進後出(FILO、LIFO) •push 1: 1 •push 5: 1 5 •push 3:
1 5 3 •push 4: 1 5 3 4 •pop: 1 5 3 // => 4 •push 6: 1 5 3 6 •pop: 1 5 3 // => 6 堆疊
• ex. • stack<int> aStack; • aStack.empty() // =>判斷堆疊是否為空 •
aStack.size() // =>目前堆疊大小 • aStack.push(2) // => 推入值進堆疊中 • aStack.pop() // => 移除堆疊最上方之值 (注意:不會回傳值) • aStack.top() // => 取得堆疊最上方之值 STL: stack<T>
•中序: 5 * (3 + 2) / (8 - 6)
//前中後 •前序: * 5 / + 3 2 - 8 6 // 中前後 •後序: 5 3 2 + * 8 6 - / // 前後中 前序、中序、後序 運算式
• ( ( 9 + 8 ) * 8 +
( ( 8 + 6 ) * ( 2 + 6 ) ) ) // => O • ( ( 9 + 8 ) * 8 + ( ( 8 + 6 * ( 2 + 6 ) ) ) // => X • 運用堆疊 • 遇左括弧推入堆疊中 • 遇右括弧推出左括弧 括弧檢查
• 中序: 5 * (3 + 2) / (8 -
6) //前中後 • 後序: 5 3 2 + * 8 6 - / // 前後中 • 從左而右,遇值即輸出。 • 遇運算子則檢查堆疊最上層運算子運算優先度是否高於自己 • 同運算優先度則越左邊優先度應越高 • 左括弧必定推入堆疊中,並得等右括弧出現才可推掉 中序式轉後序式
• 5 * (3 + 2) / (8 - 6)
// => 12 (or 12.5) • 利用運算元和運算子兩個堆疊來實作 • 與中序式轉後序式差不多 • 遇到運算元直接推入堆疊中 • 遇到運算子,跟中序式轉後序式一樣的判斷 • 若要推出則拿出兩個運算元計算完後堆回運算元的堆疊中 運算式運算
範例:Rails (UVa 514)
佇列
佇列 •先進先出(FIFO、LILO) •enqueue 1: 1 •enqueue 5: 1 5 •enqueue
3: 1 5 3 •enqueue 4: 1 5 3 4 •dequeue: 5 3 4 // => 1 •enqueue 6: 5 3 4 6 •dequeue: 3 4 6 // => 5
STL: queue<T> • ex. • queue<int> aQueue; • aQueue.empty() //
=>判斷佇列是否為空 • aQueue.size() // =>目前佇列大小 • aQueue.push(2) // => 推入值進佇列中 • aQueue.pop() // => 移除佇列最前方之值 (注意:不會回傳值) • aQueue.front() // => 取得佇列最前方之值 • aQueue.back() // => 取得佇列最後方之值
應用 • 最常被應用於BFS(廣度優先搜尋)當中 • 不過因為尚未講到樹與圖,所以先有印象就好 • 跟排隊有關的問題大抵上都要用到Queue • 作業系統的排程 •
Queueing Model • 作業系統、無線通訊、資料通訊課會上到XD (全部都是賀老師的課XD!!) • Event-Driven Programming (事件處理型程式設計)
範例:Throwing cards away I (UVa 10935)
合併應用
• 將佇列中的元素不再依照進入順序輸出,依照另外一種可計算之 順序重新輸出。 • enqueue 3: 3 • enqueue 1:
1 3 • enqueue 5: 1 3 5 • dequeue: 3 5 // => 1 • dequeue: 5 // => 3 • dequeue: // => 5 • 基本做法:利用二分搜尋找到該值應該插入的位置後插入。 • 可用陣列或串列實作 排序搜索+佇列=優先佇列(Priority Queue)
• ex. 預設是大值先出,可利用compare換 • priority_queue<int> pQueue; • pQueue.empty(); // =>
判斷是否為空 • pQueue.size(); // => 取得優先佇列中的大小 • pQueue.push(30); // =>推值進優先佇列中 • pQueue.pop(); // => 取得優先佇列中最大的值 STL: priority_queue<T(, container, compare)>
• 前後兩端皆可插入跟刪除 • Push Back 1: 1 • Push Back
3: 1 3 • Push Front 5: 5 1 3 • Push Front: 7: 7 5 1 3 • Pop Back: 7 5 1 // => 3 • Pop Front: 5 1 // => 7 堆疊+佇列=雙向佇列(Double-Ended Queue)
• ex. • deque<int> aDeque; • aDeque.empty(); // =>判斷是否為空 •
aDeque.size(); // => 得到目前大小 • aDeque.front(); // => 拿到最前方的值 • aDeque.back(); // => 拿到最後方的值 • aDeque.push_back(10); // => 增加值於後方 • aDeque.push_front(10); // => 增加值於前方 • aDeque.pop_back(); // => 移除最後方的值 • aDeque.pop_front(); // => 移除最前方的值 STL: deque<T>
•127 •101 •133 •10152 •673 作業 •442 •11111 •11234 •540
•10050
謝謝聆聽 資料取自於<提升程式設計的邏輯思考力> 6.1