Homework - Chapter 03 Solution.

Slides:



Advertisements
Similar presentations
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Advertisements

布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
1 Q10276: Hanoi Tower Troubles Again! 星級 : ★★★ 題組: Online-judge.uva.es PROBLEM SET Volume CII 題號: Q10276: Hanoi Tower Troubles Again! 解題者:薛祖淵 解題日期: 2006.
Section 1.2 Describing Distributions with Numbers 用數字描述分配.
3Com Switch 4500 切VLAN教學.
1 Web of Science 利用指引 單元二 瀏覽與處理查詢結果. 2 瀏覽檢索結果 查出的結果,預設以時間排列, 使用者可改變結果的排列方式: 還可以依被引用次數、相關度、 第一作者、刊名、出版年等排序 回到前先查的結果畫面 點選想看資料的完整書目 本館訂購範圍的期刊 全文,便可直接連結.
: Factstone Benchmark ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: : Factstone Benchmark 解題者:鐘緯駿 解題日期: 2006 年 06 月 06 日 題意: 假設 1960.
Graph V(G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E(G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5,
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
: Matrix Decompressing ★★★★☆ 題組: Contest Volumes with Online Judge 題號: 11082: Matrix Decompressing 解題者:蔡權昱、劉洙愷 解題日期: 2008 年 4 月 18 日 題意:假設有一矩陣 R*C,
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
8.1 何謂高度平衡二元搜尋樹 8.2 高度平衡二元搜尋樹的加入 8.3 高度平衡二元搜尋樹的刪除
Analyzing Algorithms Based on: 1. 物件導向資料結構 — 使用 Java 語言, 江振 瑞 著, 松崗圖書公司, Introduction to the Design and Analysis of Algorithms -- A strategic.
第一章 演算法:效率、分析與量級 1.1演算法 1.2發展有效率演算法的重要性 1.3演算法的分析 1.4量級(Order)
: The Battle II ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11098: The Battle II 解題者:許桓偉 解題日期: 2007 年 3 月 13 日 題意:給一連串炸彈座標、半徑、爆炸範 圍 (Xi,Yi,Ri,Ei)
Network Connections ★★★☆☆ 題組: Contest Archive with Online Judge 題號: Network Connections 解題者:蔡宗翰 解題日期: 2008 年 10 月 20 日 題意:給你電腦之間互相連線的狀況後,題.
24-6 設定開始與結束場景中的 程式 最後我們要替這個遊戲收個尾, 幫它把開始 的等待畫面跟結束畫面處理一下。
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
: Tight words ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: : Tight Words 解題者:鐘緯駿、林一帆 解題日期: 2006 年 03 月 14 日 題意: 給定數字 k 與 n (0 ≦ k.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
選舉制度、政府結構與政 黨體系 Cox (1997) Electoral institutions, cleavage strucuters, and the number of parties.
: Little Red Riding Hood ★★★☆☆ 題組: Contest Volumes Archive with Online Judge 題號: 11067: Little Red Riding Hood 解題者:陳明凱 解題日期: 2008 年 3 月 14 日 題意:
Johnson’s algorithm Johnson’s演算法可用於計算All pairs shortest path問題。
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
: Count DePrimes ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11408: Count DePrimes 解題者:李育賢 解題日期: 2008 年 9 月 2 日 題意: 題目會給你二個數字 a,b( 2 ≦ a ≦ 5,000,000,a.
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
Amortized Analysis.
: Selfdescribing Sequence ★★★ 題組: Problem Set Archive with Online Judge 題號: Problem C: Selfdescribing Sequence 解題者:林峰世 解題日期: 2006 年 3 月 21 日 題意:
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
資料結構實習-一 參數傳遞.
: Flea circus ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10938: Flea circus 解題者:李育賢 解題日期: 2008 年 6 月 6 日 題意:題目會給定一些點當做樹與樹枝或樹葉連 接的地方 ( 最多.
Lecture 7 Sorting in Linear Time. Sorting in Linear Time2 7.1 Lower bounds for sorting 本節探討排序所耗用的時間複雜度下限。 任何一個以比較為基礎排序的演算法,排序 n 個元 素時至少耗用 Ω(nlogn) 次比較。
Dynamic Multi-signatures for Secure Autonomous Agents Panayiotis Kotzanikolaou Mike Burmester.
: Efficient Solutions ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11020: Efficient Solutions 解題者:陳宜佐 解題日期: 2007 年 4 月 24 日 題意:給定 M 個 case.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
公用品.  該物品的數量不會因一人的消費而受到 影響,它可以同時地被多人享用。 角色分配  兩位同學當我的助手,負責:  其餘各人是投資者,每人擁有 $100 , 可以投資在兩種資產上。  記錄  計算  協助同學討論.
: Problem G e-Coins ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10306: Problem G e-Coins 解題者:陳瀅文 解題日期: 2006 年 5 月 2 日 題意:給定一個正整數 S (0
: A-Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10930: A-Sequence 解題者:陳盈村 解題日期: 2008 年 5 月 30 日 題意: A-Sequence 需符合以下的條件, 1 ≤ a.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
1 100: The 3n+1 Problem ★★★☆☆ 題組: VOLUME CII 題號: 10721: Problem C-Chopsticks 陳冠男 解題者:陳冠男、侯沛彣 解題日期: 2006 年 4 月 23 日 給定一個正整數 n (n>1) ,當 n 為奇數時令 n  3n+1.
第 9 章 TSR 程式基本教練. 本章提要 TSR 程式 以熱鍵 (Hot key) 叫用 TSR 程式 Clock 中斷 int 08h 、 int 1ch DOS reentrant 的問題 有用的 TSR 程式.
: Lucky Number ★★★★☆ 題組: Proble Set Archive with Online Judge 題號: 10909: Lucky Number 解題者:李育賢 解題日期: 2008 年 4 月 25 日 題意:給一個奇數數列 1,3,5,7,9,11,13,15…
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 10 m-way 搜尋樹與B-Tree
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
1 523: Minimum Transport Cost ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 523: Minimum Transport Cost 解題者:林祺光 解題日期: 2006 年 6 月 12 日 題意:計算兩個城市之間最小的運輸成本,運輸.
網路介紹及其運用 講師陳炯勳. 5-2 IP 協定 ( 一 ) IP 協定運作 (1) – 網路成員:主機 (Host) 與路由器 (Router) – 路由表 – 電報傳輸運作.
時間序列 Chap7 1 Chap 7 Decomposition method 4 components : Trend (TR t ) :長期向上或向下的移動趨勢 Seasonal variation (SN t ) :以年為基礎的變動原型 Cycle (CL t ) :在 2 到 10 年中向上或向下的改變.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
: Light, more light ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10110: Light, more light 解題者:吳政霖, 洪光燦 解題日期: 2006 年 4 月 9 日 題意:有個神奇的工友負責學校走廊上的燈泡的開關,
: Longest Paths ★★★☆☆ 題組: VOLUME C 題號: 10000: Longest Paths 解題者:陳冠男、邱元甫 解題日期: 2006 年 3 月 12 日 題意:在一個 no cycle 的 digraph 裡,計算從指 定起點可行走的最長路徑,若有多個最長路.
連續隨機變數 連續變數:時間、分數、重量、……
:Commandos ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11463: Commandos 解題者:李重儀 解題日期: 2008 年 8 月 11 日 題意: 題目會給你一個敵營區內總共的建築物數,以及建築物 之間可以互通的路有哪些,並給你起點的建築物和終點.
: SAM I AM ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11419: SAM I AM 解題者:李重儀 解題日期: 2008 年 9 月 11 日 題意: 簡單的說,就是一個長方形的廟裡面有敵人,然 後可以橫的方向開砲或縱向開砲,每次開砲可以.
: Bee Maja ★★☆☆☆ 題組: Contests Hosting Service with Online Judge 題號: 10182: Bee Maja 解題者:林祺光、李哲宇 解題日期: 2006 年 3 月 26 日 題意:現有兩種六邊形座標系,將甲座標系的某一點轉 為相對應的乙座標系。
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
:Rings and Glue ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10301: Rings and Glue 解題者:施博修 解題日期: 2011 年 5 月 18 日 題意:小約翰有了個大麻煩,他不小心將 rings.
幼兒行為觀察與記錄 第八章 事件取樣法.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
Amortized Analysis. p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential.
Activity Networks AOV 網路 AOE 網路. AOV 網路 (Activity on Vertx Networks)  為了表示一件工作中,各子工程間的先後 關係,我們可以利用有向圖中的有向邊代 表事情進行的順序,位於一條有向邊終點 的事件必須要等待起點的事情完成後,才 可以進行。
Presentation transcript:

Homework - Chapter 03 Solution

Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue should now be able to use all positions to hold up elements. The complexity of each of your functions should be O(1) (exclusive of the time taken to double queue capacity when needed).

Solution Suppose lastOp is of string type. template <class T> void Queue<T>::Push(const &x) { if (rear == front && lastOp == “INSERT”) Resize(); rear = (rear + 1) % capacity; queue[rear] = x; lastOp = “INSERT”; };

Solution template <class T> void Queue<T>::Pop() { if (rear == front && lastOp == “DELETE”) //Empty throw “Queue is empty. Cannot delete.” front= (front + 1) % capacity; queue[front].~T(); lastOp = “DELETE”; };

Problem 2 Please implement the class Queue by using stacks. Suppose that you start with an empty stack. After a sequence of n inqueues (insertion) and dequeues (deletion) operations, what is the average time complexity for each operation. please reason your answer.

Solution class Queue { public: Queue(); ~Queue(); void Push(data value); data Pop(); private: Stack S1, S2; };

Solution Queue::Queue() { Initialize S1 and S2; } Queue::~Queue() deallocate S1 and S2; void Queue::Push(data value) { S1.Push(value); } data Queue::Pop() if (S1.IsEmpty()) throw exception; while (!S1.IsEmpty()) S2.Push(S1.Pop()); data delNode = S2.Pop(); while (!S2.IsEmpty()) S1.Push(S2.Pop()); return delNode;

Solution Insertion的時間複雜度: O(1). Deletion的時間複雜度: O(S), 假設S代表queue當中的個數。 考慮n次的新增與刪除,其中我們令新增執行了i次,刪除總共有n-i次。由於刪除的時間複雜度和queue當中的個數有關,就worst case而言,我們可以連續做完i次都新增後,再連續作n-i次刪除。 時間複雜度為 O(i) + (O(i)+O(i-1)+…+O(i-(n-i)) < O(i)+O(i)+O(i-1)+….+O(1) = O(i2) 平均的時間複雜度為

Problem 3 Given the following maze: 1 2 3 4 5 6 7 8 9 entrance exit Trace out the action of Program 3.16 on the maze and find a path through the maze Rewrite Program 3.16 so that it can find out the shortcut.

Solution The path is (1,1) →(2,2) →(2,3) →(3,3) → 4 5 6 7 8 9 The path is (1,1) →(2,2) →(2,3) →(3,3) → (4,4) →(3,5) →(2,5) →(1,6) →(2,7) → (3,6) →(4,6) →(5,5) →(6,6) →(7,7) → (8,8) →(8,9)

Solution void Path(const in m, const int p) { …. //if a path is found, the points are stored in the stack. //Then, invoke a function to print out the shortcut. ShortCut(m, p, maze, stack); }

Shortcut(m, p, maze, stack) { //step 是一個2D的陣列,step[i][j]記錄一個整數 //用來代表(i,j)會被原路徑中會第幾個點被拜訪到 int step[m][p]; //用來標記路徑 int mark[m][p]; int length = 1; //用來記錄目前路徑走到第幾點 Stack path, result; Initialize step[i][j] with a large number (MAX) for each (i, j); Initialize mark[i][j] with 0 for each (i, j); while (!stack.IsEmpty()) { Let p=(x, y) be a point popped from stack; mark[x][y] = 1; path.Push(p); } previous = path.Pop(); //The first point result.Push(previous); while (!path.IsEmpty()) if (step[x][y] < length) then //表示有更快的方法可以走到p for (i = length; i>step[x][y]; i--) { //pop掉之前走過的 del = result.Pop(); for each t =(a, b) where t denotes any neighbor of del do if step[a][b] equals to i then Restore step[a][b] to MAX; end for length = step[x][y]; end if result.Push(p); //走到p for each q = (m, n), where q is neighbor of p do //將p的每個鄰居以現有的長度(length)來標記 if (q is not previous and mark[m][n] equals to 1 and step[m][n] >= length) step[m][n] = length;

Problem 4 Given an infix expression 6 / 2 * 3 + 4 * 5 Use tables like Section 3.6.3 and Figure 3.16 to describe how you convert it into postfix notation and how you evaluate its value. Write an algorithm to convert an infix expression into prefix notation.

Solution 6 / 2 * 3 + 4 * 5 Next Stack Output none empty 6 Empty / 2 6 2 * 6 2 / 3 6 2 / 3 + 6 2 / 3 * 4 6 2 / 3 * 4 +* 5 + * 6 2 / 3 * 4 5 6 2 / 3 * 4 5 * +

Solution 6 2 / 3 * 4 5 * + Operations Postfix Notation 3 = 6 / 2 3 3 * 4 5 * + 9 4 5 * + 9 = 3 * 3 20 = 4 * 5 9 20 + 29 = 9 + 20 29

void InfixToPrefix (expression e) { Stack S; Stack Output; Reverse the expression e; //for example, 2+4*3 becomes 3*4+2 for (i=0; e[i] is not the end of string; i++) { if (e[i] is an operand) Output.Push(e[i]; else if (e[i] is ‘)’) S.Push(e[i]); else if (e[i] is not ‘(’) { while (S is not empty) { y = S.Pop(); if (y is not ‘(‘ and the priority of y > the priority of e[i]) Output.Push(y); else { S.Push(y); break; } else { //e[i] == ‘(’ if (y != ‘)‘) else Pop out all the elements in S and push them into the stack Output; while (!Output.IsEmpty()) print out Output.Pop();