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
160
資訊實務應用讀書會 第七堂課:jQuery
sinmaplewing
0
2.1k
師大資工系ACM-ICPC讀書會:貳、排序搜尋與數學基礎
sinmaplewing
0
210
資訊實務應用讀書會 第六堂課:Basic CSS
sinmaplewing
0
1.7k
師大資工系ACM-ICPC讀書會:壹、字串處理與大數運算
sinmaplewing
0
150
師大資工系ACM-ICPC讀書會:零、比賽介紹、解題系統介紹
sinmaplewing
0
240
資訊實務應用讀書會 第四堂課(支線):Linux
sinmaplewing
0
1.9k
資訊實務應用讀書會 第一堂課:Basic HTML
sinmaplewing
1
2.2k
Other Decks in Programming
See All in Programming
スマートグラスで並列バイブコーディング
hyshu
0
280
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.6k
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
130
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
350
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
830
Agentic UI
manfredsteyer
PRO
0
220
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
820
The NotImplementedError Problem in Ruby
koic
1
1.1k
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
150
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
340
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.9k
AIで効率化できた業務・日常
ochtum
0
160
Featured
See All Featured
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Are puppies a ranking factor?
jonoalderson
1
3.7k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
760
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Navigating Weather and Climate Data
rabernat
0
270
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
960
A Tale of Four Properties
chriscoyier
163
24k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.5k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.9k
Why Our Code Smells
bkeepers
PRO
340
58k
Believing is Seeing
oripsolob
1
160
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