1 / 24

第一次小考解答

第一次小考解答. 張啟中. 1. Compute the Address of Array. 由下圖可知,對於陣列元素 A[i][j][k] 而言,其位址為 α+ (i-1) *200 + (j-1) *20 + (k-1) 所以 A[3][7][2] = α+400+120+1 = α+521. A[1][1][1]. A[1][1][2]. A[1][1][3]. ‥‥. A[1][1][19]. A[1][1][20]. α. α+1. α+2. α+18. α+19. A[1][2][1]. A[1][2][2].

joanne
Download Presentation

第一次小考解答

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第一次小考解答 張啟中

  2. 1. Compute the Address of Array • 由下圖可知,對於陣列元素 A[i][j][k] 而言,其位址為 α+(i-1)*200 + (j-1)*20 + (k-1) • 所以 A[3][7][2] = α+400+120+1 = α+521 A[1][1][1] A[1][1][2] A[1][1][3] ‥‥ A[1][1][19] A[1][1][20] α α+1 α+2 α+18 α+19 A[1][2][1] A[1][2][2] A[1][2][3] ‥‥ A[1][2][19] A[1][2][20] α+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

  3. 3+1=4 0+1=1 -1+1=0 2. Compute the Failure Function b ≠c a ≠c 0 1 2 3 4 5 6 7 8 9 a b c a b c a c a b 3 -1 -1 -1 0 1 2 -1 ?

  4. 2. Compute the Failure Function 0 1 2 3 4 5 6 7 8 9 a b c a b c a c a b -1 -1 -1 0 1 2 3 ?

  5. 2. Compute the Failure Function 0 1 2 3 4 5 a a a a a b -1 -1 0 1 2 3 ?

  6. 2. Compute the Failure Function void String::fail() //Compute the failure function for the pattern p(*this) //p is a string { intLengthP = 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)

  7. -1 -1 -1 0 1 2 3 -1 0 1 a b c a b c a c a b a b c a b c a b c a b c a c a b 2. String Pattern Matching 3+1=4 b ≠ c

  8. -1 -1 -1 0 1 2 3 -1 0 1 a b c a b c a c a b a b c a b c a b c a b c a c a b 2. String Pattern Matching 3+1=4 OK b ≠ c 懂了沒?

  9. -1 -1 -1 0 1 2 3 -1 0 1 a b c a b c a c a b a b c a b c a a c a b c a c a b 2. String Pattern Matching 3+1=4 a ≠ c

  10. -1 -1 -1 0 1 2 3 -1 0 1 a b c a b c a c a b a b c a b c a a c a b c a c a b 2. String Pattern Matching 0+1=1 -1+1=0 a ≠ b

  11. -1 -1 -1 0 1 2 3 -1 0 1 a b c a b c a c a b a b c a b c a a c a b c a c a b 2. String Pattern Matching -1+1=0 剩下的自己追蹤了!

  12. 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)

  13. 1 2 4 8 16 32 0 1 2 3 4 5 0 2 8 24 64 160 1 4 16 64 256 1024 1 8 64 512 4096 32,768 2 4 16 256 65,536 4,294,967,296 1 2 24 40,320 20,922,789,888,000 ……. 3. Order the Time Complexity O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n) < O(n!)

  14. 4. Show that 10n2+9 = O(n2) • By Big-O definition f (n) = O(g(n)) 若且唯若存在有 c>0和 n0N, 對所有的 n, n≧n0 使得 f (n) ≦ c.g(n) 。 • 本題只要找出 c與 n0即可得證 取 c = 11且 n0 = 3,則對所有的 n, n≧3會使得 10n2 + 9 ≦ 11.g(n) = 11n2 即 10n2+9 = O(n2) 得證

  15. 5. Recursive Program n! int factor(int n) { if (n==0 || n==1) return 1; else return n*factor(n-1); }

  16. 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] } } } 想想看,本程式的時間複雜度為多少?

  17. 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 = 22 T(n-2) + 2 +1 = 22[ 2T(n-3)+1 ] + 2 + 1 = 23T(n-3) + 22+ 2+ 1 = …… = 2n-1 T(1) + 2n-2 + 2n-3 + …. + 2 + 1 = 2n-1 + 2n-2 + …. + 2 + 1  (等比級數,公比=2,有n 項) = 2n – 1

  18. 7. 表示式的運算 • 注意這三小題的區別。 • 人工筆算 將中序表示式 (A+B)*D + E/(F+A*D)+C轉換為後序表示式 • 電腦運算 利用堆疊計算後序表示式,得到結果。 • 電腦運算 利用堆疊將一個中序表示式轉換為後序表示式。 請注意第 2 與 3 小題的區別

  19. 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+

  20. 2 3 4 + * 4 + * * 7 2 3 2 3 4 + * + * 4 3 2 2 14 7. 表示式的運算

  21. 7. 表示式的運算 中置表示式轉後置表示式 1. 當碰到運算元時,直接輸出此運算元。 2. 當碰到左括號時,將其 push 至堆疊中。 3. 當碰到右括號時,將堆疊中的運算子 pop 出來並輸出,直到碰到左括號為止,然後再將此左括號 pop 掉。 4. 當碰到運算子時, 依序將堆疊中運算優先次序較高或相同的運算子 pop 出來並輸出,直到遇到下列情形之一 (1) 碰到左括號 (2) 碰到優先次序較低的運算子 (3) 堆疊變為空 最後將此運算子 push 至堆疊中。 5. 當輸入字串全部處理完時,將堆疊中所剩餘的運算子逐一地 pop 出來並輸出,直到堆疊變為空為止。

  22. 0 1 2 3 4 5 0 1 2 3 4 5 8. Sparse Matrix

  23. 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 = newint[Cols]; int *RowStart = newint[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

  24. 8. Sparse Matrix

More Related