Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
師大資工系ACM-ICPC讀書會:參、堆疊與佇列
Search
Maplewing
October 25, 2013
Programming
290
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
190
資訊實務應用讀書會 第七堂課:jQuery
sinmaplewing
0
2.1k
師大資工系ACM-ICPC讀書會:貳、排序搜尋與數學基礎
sinmaplewing
0
230
資訊實務應用讀書會 第六堂課:Basic CSS
sinmaplewing
0
1.8k
師大資工系ACM-ICPC讀書會:壹、字串處理與大數運算
sinmaplewing
0
190
師大資工系ACM-ICPC讀書會:零、比賽介紹、解題系統介紹
sinmaplewing
0
270
資訊實務應用讀書會 第四堂課(支線):Linux
sinmaplewing
0
2k
資訊實務應用讀書會 第一堂課:Basic HTML
sinmaplewing
1
2.2k
Other Decks in Programming
See All in Programming
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
4
3.8k
クラウドゲーミングのパフォーマンス最適化に挑む / Cloud Gaming Performance Tuning
vivion
0
330
조합이폭발하는디자인시스템이걸눈으로도테스트하라고요
giljongyu
0
740
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
170
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
2k
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
190
Railsバージョンを4倍にした話
optim
0
1k
Distance field of Julia set
kinakomoti321
0
310
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
370
そのデザイントークン、Flutterでも同じ見た目になりますか?
nakamizu
0
330
LLM 101: Exploring LLMs, Starting with Tokens.
dalinaum
0
100
Getting Started with Vibe Coding
miccheng
0
200
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
4k
Prompt Engineering for Job Search
mfonobong
0
580
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
560
New Earth Scene 8
popppiees
5
2.8k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Paper Plane (Part 1)
katiecoart
PRO
3
12k
Building Better People: How to give real-time feedback that sticks.
wjessup
371
20k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
From π to Pie charts
rasagy
1
440
Heart Work Chapter 1 - Part 1
lfama
PRO
11
37k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
660
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