第一次小考解答 張啟中
1. Compute the Address of Array 由下圖可知,對於陣列元素 A[i][j][k] 而言, 其位址為 α+(i-1)*200 + (j-1)*20 + (k-1) 所以 A[3][7][2] = α = α+521 A[1][1][1]A[1][1][2]A[1][1][3] ‥‥ A[1][1][19]A[1][1][20] A[1][2][1]A[1][2][2]A[1][2][3] ‥‥ A[1][2][19]A[1][2][20] αα+1α+2α+18α+19 α+20α+21α+22α+38α+39 A[1][10][1]A[1][10][2]A[1][10][3] ‥‥ A[1][10][19]A[1][10][20] α+180α+181α+182α+198α+199 A[2][1][1]A[2][1][2]A[2][1][3] ‥‥ A[2][1][19]A[2][1][20] α+200α+201α+202α+218α+219
c 2. Compute the Failure Function abcabcaab ? =40+1=1-1+1=0 b ≠ca ≠c
2. Compute the Failure Function cabcabcaab 0123?
2. Compute the Failure Function aaaaab 0123?
2. Compute the Failure Function void String::fail() //Compute the failure function for the pattern p(*this) //p is a string { int LengthP = Length(); f[0] = -1; for(int j=1; j< LengthP; j++) //compute f[j] { int i=f[j-1]; while (p[j] != p[i+1] && i>=0) i = f[i]; if (p[j] == p[i+1]) f[j] = i+1; else f[j] = -1; } } //end of fail O(LengthP)
2. String Pattern Matching babcabcacabcacab b ≠ c cabcabcaab =4
2. String Pattern Matching babcabcacabcacab cabcabcaab =4 b ≠ c OK
2. String Pattern Matching aabcabcacabcacab a ≠ c cabcabcaab =4
2. String Pattern Matching aabcabcacabcacab a ≠ b cabcabcaab =1 -1+1=0
2. String Pattern Matching aabcabcacabcacab cabcabcaab =0 剩下的自己追蹤了 !
2. String Pattern Matching int String::FastFind(String pat) { //Determine if pat is a substring of s int PosP=0, PosS=0; int LengthP=pat.length(), LengthS=length(); while ((PosP<LengthP)&&(PosS<LengthS)) if (pat[PosP] == str[PosS]) PosP++; PosS++; else if (PosP == 0) PosS++; else PosP = pat.f[PosP-1]+1; if (PosP < LengthP) return -1; else return PosS-LengthP; } //end of FastFind O(LengthS)
3. Order the Time Complexity O(1) < O(log n ) < O(n) < O(nlog n ) < O(n 2 ) < O(n 3 ) < O(2 n ) < O(n!) , ,536 4,294,967, ,320 20,922,789,888,000 …….
4. Show that 10n 2 +9 = O(n 2 ) By Big-O definition f (n) = O(g(n)) 若且唯若存在有 c > 0 和 n 0 N, 對所有的 n, n ≧ n 0 使得 f (n) ≦ c . g(n) 。 本題只要找出 c 與 n 0 即可得證 取 c = 11 且 n 0 = 3 ,則對所有的 n, n ≧ 3 會使得 10n ≦ 11 . g(n) = 11n 2 即 10n 2 +9 = O(n 2 ) 得證
5. Recursive Program n! int factor(int n) { if (n==0 || n==1) return 1; else return n*factor(n-1); }
5. Recursive Program Permutation //pos 表示目前要排列位置,從 pos( 含 ) 以後的字元需要被排列 void permutation(string s, int pos) { if (pos == s.length()-1) { cout << s << endl; } else { for (int i=pos; i<s.length(); i++) { swap(s[i],s[pos]); // 交換 s[i] 與 s[pos] permutation(s, pos+1); swap(s[i],s[pos]); // 交換 s[i] 與 s[pos] } 想想看,本程式的時間複雜度為多少?
6. Compute the Recursive Formula T(n) = 2T(n-1) + 1 and T(1)=1 T(n) = 2T(n-1)+1 = 2[ 2T(n-2)+1 ] + 1 = 2 2 T(n-2) = 2 2 [ 2T(n-3)+1 ] = 2 3 T(n-3) = …… = 2 n-1 T(1) + 2 n n-3 + … = 2 n n-2 + … ( 等比級數,公比 =2 ,有 n 項 ) = 2 n – 1
7. 表示式的運算 注意這三小題的區別。 人工筆算 將中序表示式 (A+B)*D + E/(F+A*D)+C 轉換為後序表 示式 電腦運算 利用堆疊計算後序表示式,得到結果。 電腦運算 利用堆疊將一個中序表示式轉換為後序表示式。 請注意第 2 與 3 小題的區別
7. 表示式的運算 (A+B)*D + E/(F+A*D)+C AB+*D+E/(F+AD*)+C AB+D*+E/FAD*++C AB+D*+EFAD*+/+C AB+D*EFAD*+/++C AB+D*EFAD*+/+C+
7. 表示式的運算 * * * * *
7. 表示式的運算 中置表示式轉後置表示式 1. 當碰到運算元時,直接輸出此運算元。 2. 當碰到左括號時,將其 push 至堆疊中。 3. 當碰到右括號時,將堆疊中的運算子 pop 出來並輸出,直到碰到左括號為止, 然後再將此左括號 pop 掉。 4. 當碰到運算子時, 依序將堆疊中運算優先次序較高或相同的運算子 pop 出 來並輸出,直到遇到下列情形之一 (1) 碰到左括號 (2) 碰到優先次序較低的運算子 (3) 堆疊變為空 最後將此運算子 push 至堆疊中。 5. 當輸入字串全部處理完時,將堆疊中所剩餘的運算子逐一地 pop 出來並輸出, 直到堆疊變為空為止。
8. Sparse Matrix
SparseMatrix SparseMatrix::Transpose() // The transpose of a(*this) is placed in b and is found // in O(terms + columns) time. { int *Rows = new int[Cols]; int *RowStart = new int[Rows]; SparseMatrix b; b.Rows = Cols; b.Cols = Rows; b.Terms = Terms; if (Terms > 0) // nonzero matrix { //compute RowSize[i] = number of terms in row i of b // initialize for (int i = 0; i < Cols; i++) RowSize[i] = 0; for (int i = 0; i < Terms; i++) RowSize[smArray[i].col]++; // RowStart[i] = starting position of row i in b RowStart[0] = 0; for (int i = 1; i < Cols; i++) RowStart[i] = RowStart[i-1] + RowSize[i-1]; ……… } } // end of FastTranspose
8. Sparse Matrix