data_CHAP02 - 55 5 55 6 ˜ ˜ 5 5 k 6  5 5 ø ñ k Á...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: / / / / 55 5 55 6 ˜ ˜ [ / / / 5 5 k 6  / / / / / 5 5 ø ñ / k Á / Data Structures / / / / / / / 555 55 / / / / / 555 555 555 55 / / N Á 6 N ¿ò  o By Hyun-Ju Park / Hanbat National University 555 555 55 / 6 / / Data Structures By Hyun-Ju Park / Hanbat National University / / / 5 5  [ / / 555 55 / 6 ù 6 / / 6 Á 6 / N Á   Àó “÷   ” ⇒ <index, value> −  j  à (mapping) “ o ` ô (correspondence) ‚\ ⇒  “÷  (ADT)  ” \ ÷ / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á    structure Array "SSBZ ª  objects : index    item <index, value>È  . index\ ÷  ‚\ functions : \ ñ  .  A ∈ Array, i ∈ index, x ∈ item, j, size ∈ integer Array Create(j, list) ::= return j  À.  list\ i   j-tuple ° item ó  ô. Item Retrieve(A, i) ::= if (i ∈ index) return À A else return Array Store(A, i, x) ::= if (i ∈ index) return else return end Array j i i  o  › È <i, x> o À A / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á C À   ¼ e ¼  */  */ int list[5]; /* *plist[5]; /* À list ˆ (base address) = list[0]  =α list[2] = α + 2 × sizeof(int) list[4] = α + 4 × sizeof(int) list[1] = α + sizeof(int) list[3] = α + 3 × sizeof(int) À  ¼  e ¼ int *list1; int list2[5]; /* list1$ list2 o list2  oe  */ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á     ª  #define MAX_SIZE 100 float sum(float , int); float input[MAX_SIZE], answer; int i; void main(void) { for (i = 0; i < MAX_SIZE; i++) input[i] = i; answer = sum(input, MAX_SIZE); printf("The sum is: %f\n", answer); } float sum(float list, int n) { int i; float tempsum = 0; for (i = 0; i < n; i++) tempsum += list[i]; return tempsum; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á      – ª  void print1(int *ptr, int row) { /*  §  ÷  À */ int i; printf("Address Contents\n"); for (i = 0; i < rows; i++) printf("%8u%5d\n", ptr + i, *(ptr + i) ); printf("\n"); } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á 2.2.1 (structure) j struct §  \     _  ª    struct { char name[10]; int age; float salary; } person; strcpy(person.name, “james”); person.age = 10; person.salary = 35000; /* /* /* ø À› o ” [ ` §`  ` */ \ \ float */ */ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á  ¾  typedef struct human_being { char name[10]; int age; float salary; }; typedef struct { char name[10]; int age; float salary; } human_being; human_being person1, person2; if (strcmp(person1.name, person2.name)) printf(“r ” ó .\n”); else printf(“r ” ó .\n”); strcpy(person1.name, person2.name); person1.age = person2.age; person1.salary = person2.salary; / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á    typedef struct { int month; int day; int year; } date; typedef struct human_being { char name[10]; int age; float salary; date dob; }; person1.dob.month = 2; person1.dob.day = 11; person1.dob.year = 1944; / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á 2.2.2 (union)    ñ  _  ª ]  typedef struct sex_type { enum tag_field { female, male } sex; union { int children; int beard; } u; }; typedef struct human_being { char name[10]; int age; float salary; date dob; sex_type sex_info; }; human_being person1, person2; person1.sex_info.sex = male; person1.sex_info.u.beard = FALSE; person2.sex_info.sex = female; person2.sex_info.u.children = 4; / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á 2.2.3 r[    59, padding, alignment − ÷ ó› word ›…       r``  § / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á 2.2.4  ó› (self-referential structure) ø § ø  \  [  \   (malloc free) ›  typedef struct list { char data; list *link; }; list item1, item2, item3; item1.data = ‘a’; item2.data = ‘b’; item3.data = ‘c’;  ª  item1.link = item2.link = item3.link = NULL; item1.link = &item2; item2.link = &item3; / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á  ­  2.3.1 §  % (ordered list) = ` "DF          +BDL 2VFFO ,JOH  FNQUZ MJTU  % (linear list) . ⇒ % (item0, item1, ..., itemn – 1), itemi :  S \ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á %      −  − › $  n í i i i    (  í)ó› ÷  a ,0≤i<n , 0 ≤ i < n ›   ,0≤i<n i, i + 1, ..., n – 1 i  § i + 1, i + 2, ..., nó›  û`, 0 ≤ i < n  § i, i + 1, ..., n – 2ó›  i + 1, ..., n – 1 ⇒  { % / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á   − À  á § \   (sequential mapping) j ié  § ó ›     itemi§  ô \ À› û§   ñ \j ` ã overhead   (linked list) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á 2.3.2 ­ à   b ã  a xe (degree) −  ›   A(x) = 3x20 + 2x5 + 4, B(x) = x4 + 10x3 + 3x2 + 1  B ( x) = ∑ bi x i A( x) = ∑ ai x i : A( x) + B ( x) = ∑ (ai + bi ) x i : A( x) ⋅ B ( x) = ∑ (ai x i ⋅ ∑ b j x j ) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á   _     1PMZOPNJBM  ª  struct Polynomial Èó› o  . , ai\ objects : P(x) = a1xe1 + … + anxen : <ei , ai> Coefficient , ei\ Exponents . ei\ 0 ‚\ 0 . functions :  poly, poly1, poly2 ∈ polynomial, coef ∈ Coefficient, expon ∈ Exponents ::= return , p(x) = 0 ::= if (poly) return FALSE else return TRUE Coefficient Coef(poly, expon) ::= if (expon ∈ poly) return else return 0 Exponent Leap_Exp(poly) ::= return poly û÷  Polynomial Attach(poly, coef, expon) ::= if (expon ∈ poly) return else return <coef, expon> o poly Polynomial Remove(poly, expon) ::= if (expon ∈ poly) return  expon ûo poly else return Polynomial SingleMult(poly, coef, expon) ::= return poly · coef · xexpon Polynomial Add(poly1, poly2) ::= return poly1 + poly2 Polynomial Mult(poly1, poly2) ::= return poly1 · poly2 end Polynomial Polynomical Zero() Boolean IsZero() / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á −  \ ñ÷ :  ã   IsZero, Coef, Remove      3*-- ª  /* d = a + b,  a, b, d\ */ d = Zero(); while (!IsZero(a) && !IsZero(b)) do { switch (COMPARE(Lead_Exp(a), Lead_Exp(b))) { case –1 : d = Attach(d, Coef(b, Lead_Exp(b)), Lead_Exp(b)); b = Remove(b, Lead_Exp(b)); break; case 0 : sum = Coef(a, Lead_Exp(a)) + Coef(b, Lead_Exp(b)); if (sum) Attach (d, sum, Lead_Exp(a)); a = Remove(a, Lead_Exp(a)); b = Remove(b, Lead_Exp(b)); break; case 1 : d = Attach(d, Coef(a, Lead_Exp(a)), Lead_Exp(a)); a = Remove(a, Lead_Exp(a)); } } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á   A(x) = anxn + an - 1xn - 1 + ... + a1x + a0 A = (n, an, an - 1, ..., a1, a0)   _  Ò  l ª ]  A( x) = ∑ ai x i a.degree = n a.coef[i] = an-i, 0 ≤ i ≤ n + 1 */ #define MAX_DEGREE 101 /* typedef struct { int degree; float coef[MAX_DEGREE]; } Polynomial; ûú − ó :   §  a.degree << MAX_DEGREE `` A(x) = 2x1000 + 1 / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á      l ª ]  A = (m, em-1, bm-1, em-2, bm-2, ..., e0, b0), em-1> em-2 > ... > e0 ≥ 0, m : 0 #define MAX_TERMS 100 /* À typedef struct { float coef; int expon; } Polynomial; Polynomial terms[MAX_TERMS]; int avail = 0;     ­  ª ]  */ A(x) = 2x1000 + 1 B(x) = x4 + 10x3 + 3x2 + 1 starta finisha startb finishb avail coef exp 2 1000 0 1 0 1 1 4 2 10 3 3 3 2 4 1 0 5 6 / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á   _    ­   ª   void padd(int starta, int finisha, int startb, int finishb, int *startd, int *finishd) { /* A(x)é B(x)§  D(x)§ */ float coefficient; *startd = avail; while (starta <= finisha && startb <= finishb) switch (COMPARE(terms[starta].expon, terms[startb].expon)) { case –1 : /* a expon b expon øó  */ attach(terms[startb].coef, terms[startb].expon); startb++; break; case 0 : /*  ó  */ coefficient = terms[starta].coef + terms[startb].coef; if (coefficient) attach(coefficient, terms[starta].expon); starta++; startb++; break; case 1 : /* a expon b expon  */ attach(terms[starta].coef, terms[starta].expon); starta++; break; } /* A(x) `   */ for (; starta <= finisha; starta++) attach(terms[starta].coef, terms[starta].expon); /* B(x) `   */ for (; startb <= finishb; startb++) attach(terms[startb].coef, terms[startb].expon); *finishd = avail – 1; } / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á  : : : ûú − availó MAX_TERMS : − o|  \     \ \   ã m : # of nonzero terms in A n : # of nonzero terms in B O(n + m) MAX_TERMS§  \ \  À / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á   2.4.1 }  ó  (sparse matrix)  0 ˜    ª ]  3 4 − 27 6 82 − 0.3 109 − 64 4 8 9 .12 3.4 36 27 −  ˜ , 36[  8 0 15 0 0 0 11 0 0 30 22 0 − 6 0 00 − 15 0 0 › ˜“ 0 91 0 0 0 0 0 0 0 0 0 0 0 0 28 0 0 0 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á : −   a[i][j]› ˜ − ˜ 0 , \ ˜  , a[MAX_ROWS][MAX_COLS] b  § \  \ ›  › z  ,  ⇒ 3 È <row, col, value> − \ ›/  ó  \ 15 0 0 22 0 11 3 0 0 0 0 −6 0 0 0 0 91 0 0 0 0 0 28 0 0 − 15 0 0 0 0 0 0 0 0 0 0 < 0, 0, 15 > < 0, 3, 2 > < 0, 5, -15 > < 1, 1, 11 > < 1, 2, 3 > < 2, 3, -6> < 4, 0, 91 > < 5, 2, 28> / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á      [email protected]  ª  struct Sparse_Matrix objects : 3 È < , , >  . , ó ó ñ÷ °, ó item   . functions :  a, b ∈ Spart_Matrix, x ∈ item, i, j, max_col, max_row ∈ index Sparse_Matrix Create(max_row, max_col) ::= return max_items   \ Sparse_Matrix,  \ max_row  \ max_col “ | max_items = max_row × max_col . Sparse_Matrix Transpose(a) ::= return  3 È   ó ˜ Sparse_Matrix Add(a, b) ::= if aé b  ó return ô , ‚ó     ˜ else return Sparse_Matrix Multiply(a, b) ::= if a  éb ó return ô  z“ aé b§ o ˜d: d[i][j] = Σ(a[i][k] ⋅ b[k][j]),  d[i][j]\ (i, j)  else return  ó   / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á   _    l­ \ ›/ ó   ª \  #define MAX_TERMS 101 /* typedef struct { int col; int row; int value; } Term; Term a[MAX_TERMS]; + 1 */ row col value a[0] [1] [2] [3] [4] [5] [6] [7 ] [8] 6 0 0 0 1 1 2 4 5 6 0 3 5 1 2 3 0 2 8 15 22 − 15 11 3 −6 91 28 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á 2.4.2   ? a[i][j] b[i][j] \ !!    i  < i, j > ˜ ( # 20) é < j, i, >ó›  15 0 0 22 0 11 3 0 0 0 0 −6 0 0 0 0 91 0 0 0 0 0 28 0 0 − 15 0 0 0 0 0 0 0 0 0 0 15 0 0 0 11 0 0 30 22 0 − 6 0 00 − 15 0 0 0 91 0 0 0 0 0 0 28 0 0 0 0 0 0 0 0 0 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á     ° W    ª  row col value a[0] [1] [2] [3] [4] [5] [6] [7 ] [8] 6 0 0 0 1 1 2 4 5 6 0 3 5 1 2 3 0 2 8 15 22 − 15 11 3 −6 91 28 b[0] [1] [2] [3] [4] [5] [6] [7 ] [8] row col value 6 0 0 1 2 2 3 3 5 6 0 4 1 1 5 0 2 0 8 15 91 11 3 28 22 −6 − 15 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á      ª  void transpose ( term a, term b) /* a§ b§ */ { int n, i, j, currentb; n = a[0].value; /*  */ b[0].row = a[0].col; b[0].col = a[0].row; b[0].value = n; if (n > 0) { /* 0 ˜ */ currentb = 1; for (i = 0; i < a[0].col; i++) /* a › */ for (j = 1; j <= n; j++) /*  ›  § \ */ if (a[j].col == i) { /*   \  § b  b[currentb].row = a[j].col; b[currentb].col = a[j].row; b[currentb].value = a[j].value; currentb++; } } } */ / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á  − : −  À O(rows × columns) O(elements × columns) O(rows × columns2) ãó› better algorithm − fast_transpose ù \ transpose à  : : / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á      ª  void fast_transpose ( term a, term b) { int row_terms[MAX_COL], starting_pos[MAX_COL]; int i, j, num_cols = a[0].col, num_terms = a[0].value; b[0].row = num_cols; b[0].col = a[0].row; b[0].value = num_terms; if (num_terms > 0) { /* 0 ˜ */ for (i = 0; i < num_cols; i++) row_terms[i] = 0; for (i = 1; i < num_terms; i++) row_terms[a[i].col]++; starting_pos[0] = 1; for (i = 1; i < num_cols; i++) starting_pos[i] = starting_pos[i – 1] + row_terms[i – 1]; for (i = 1; i <= num_terms; i++) { j = starting_pos[a[i].col]++; b[j].row = a[i].col; b[j].col = a[i].row; b[j].value = a[i].value; } } }  − O(elements + columns) O(rows × columns) / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á 2.4.3  ˜  C=A×B ˜ ª , A B   C › ⇒  :A B BT  A B C › / / Data Structures By Hyun-Ju Park / Hanbat National University / N Á  15 0 0 0 91 0 0 0 ˜ ª 22   0 − 15 0 0 0 0 0 0 0 0 0 0 0 − 420 330 0 − 225 225 0 121 33 0 − 18 0 0 0 0 0 0 0 0 0 0 0 0 0 1365 0 0 2002 0 − 1365 0 0 - 168 0 0 0 0 − 15 11 3 0 0 0 0 0 −6 0 0 00 0 0 0 00 0 0 0 0 28 0 0 0 × 15 0 0 22 0 11 3 0 0 0 0 −6 0 0 0 0 91 0 0 0 0 0 28 0 row col value a[0] [1] [ 2] [3] [ 4] [5] [6] [7 ] [8] 6 0 0 0 1 1 2 4 5 6 0 3 5 1 2 3 0 2 8 15 22 − 15 11 3 −6 91 28 b[0] [1] [2] [3] [4] [5] [6] [7 ] [8] row col value 6 0 0 0 1 1 2 4 5 6 0 3 5 1 2 3 0 2 8 15 22 − 15 11 3 −6 91 28 row col c[0] [1] [ 2] [3] [ 4] [5] [ 6] [7 ] [8] [9] [10] [11] 6 0 0 0 0 1 1 1 4 4 4 5 6 0 2 3 5 1 2 3 0 value 11 225 − 420 330 − 225 121 33 − 18 1365 × 3 2002 5 − 1365 3 − 168 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á   _     ˜ ª   void mmult ( term a, term b, term d) /* */ { int i, j, column; int rows_a = a[0].row, cols_a = a[0].col, cols_b = b[0].col; int totala = a[0].value, totalb = b[0].value, totald = 0; int row_begin – 1, row = a[1].row, sum = 0; int new_b[MAX_TERMS][3]; if (cols_a != b[0].row) { fprintf(stderr, “Incompatible matrices\n”); exit(1); } fast_transpose(b, new_b); */ /* a[totala + 1].row = rows_a; new_b[totalb + 1].row = cols_b; new_b[totalb + 1].col = 0; for (i = 1; i <= totala; ) { column = new_b[1].row; for (j = 1; j <= totalb + 1; ) { if (a[i].row != row) { storesum(d, &totald, row, column, &sum); i = row_begin; for (; new_b[j].row == column; j++) ; column = new_b[j].row; } else if (new_b[j].row != column) { storesum(d, &totald, row, column, &sum); i = row_begin; column = new_b[j].row; } else switch (COMPARE(a[i].col, new_b[j].col)) { case –1 : i++; break; case 0 : sum += (a[i++].value * new_b[j++].value); break; case 1 : j++; } } for (; a[i].row == row; i++) ; row_begin = i; row = a[i].row; } d[0].row = rows_a; d[0].col = cols_b; d[0].value = totald; } Data Structures By Hyun-Ju Park / Hanbat National University / 6 ˜ r / / N Á  1  À \  mŠ   › ó  À   › Ã1   n     À     § › (ui – li + 1) %  \ À  é  A(l1 : u1, l2 : u2, ..., ln : un) → ∏ n i =1 A(4:5, 2:4, 1:2, 3:4)  \ 2 × 2 × 2 × 2 = 24[  − A(4,2,1,3), A(4,2,1,4), A(4,2,2,3), A(4,2,2,4), ..., A(4,3,1,3), A(4,3,1,4), A(4,3,2,3), A(4,3,2,3), ..., A(5,4,1,3), A(5,4,1,4), A(5,4,2,3), A(5,4,2,4)  / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á # 1  À A(1 : u1) − − A(1) A(i)  α  \ α + (i – 1) 3  À A(1 : u1, 1 : u2, 1 : u3) − − − A(1, 1, 1) A(i, 1, 1) A(i, j, k)  α  \ α + (i – 1)u2u3  \ α + (i – 1)u2u3 + (j – 1)u3 + (k - 1) n  À A(1 : u1, 1 : u2, ..., 1 : un) − − − A(1, 1, ..., 1) A(i1, 1, ..., 1) A(i1, i2, ..., in)  α  \ α + (i1 – 1)u2u3 ... un \ + (i1 – 1)u2u3 ... un + (i2 – 1)u3u4 ... un + (i3 – 1)u4u5 ... un + … + (in - 1 – 1)un + (in – 1) α =α + Σ (ij – 1)aj, aj = Πuk, 1 ≤ j < n, an = 1 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á  c 2.6.1 }      4USJOH  ª ]  struct String objects : 0[ ø ñ  functions :  s, t ∈ String, i, j, m ∈ ô String Null(m) ::= return $ m ø NULL› °, NULLó “”› o Integer Compare(s, t) ::= if (sé t ó ) return 0 else if (s t ) return –1 else return + 1 Boolean IsNull(s) ::= if (Compare(s, NULL)) return FALSE else return TRUE Integer Length(s) ::= if (Compare(s, NULL)) return s ø else return TRUE String Concat(s, t) ::= if (Compare(s, NULL)) return s  t§ ø else return s String Substr(s, i, j) ::= if ((j > 0) && (i + j – 1) < Length(s)) return s i, i + 1, …, i + j – 1 \ else return NULL ø / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á ?  [ ø › o÷ À ?? C \0ó›  `\  ø À c   ª ]  #define MAX_SIZE 100 char s[MAX_SIZE] = {“dog”}; char t[MAX_SIZE] = {“house”}; s[0] d  char s = {“dog”}; char t = {“house”}; s[1] o   $ s[2] g s[3] \0 t[0] h t[1] o t[2] u t[3] s t[4] e t[5] \0 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á      c —  ª  void strnins ( char *s, char *t, int i ) { /* ø s i   ø t§ */ char string[MAX_SIZE], *temp = string; if (i < 0 && i > strlen(s)) { fprintf(stderr, “Position is out of bounds\n”); exit(1); } if (!strlen(s)) strcpy(s, t); else if (strlen(t)) { strncpy(temp, s, i); strcat(temp, t); strcat(temp, (s + i)); strcpy(s, temp); } }  86,  2.9 : ø !! / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á 2.6.2 - !(pattern matching) ø  ! ø  \ øé ø r  øé ? ø ™›  ø …     675675 – ª  if (t = strstr(string, pat)) printf(“The string from strstr is : %s\n”, t); else printf(“The pattern was not found with strstr\n”); strstr  óà  û ! !! / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á         c ! á– - ª ]  int nfind(char *string, char *pat) { /* ­  ø§   , ô  */ int i, j, start = 0; int lasts = strlen(string) – 1; int lastp = strlen(pat) – 1; int endmatch = lastp; for (i = 0; endmatch <= lasts; endmatch++, start++) { if (string[endmatch] == pat[lastp]) for (j = 0, i = start; j < lastp && start[i] == pat[j]; i++, j++) ; if (j == lastp) return start; } return –1; }  89,  2.10 : nfind  − pat = “aab” string = “ababbaabaa” / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á nfind −  : :  àó , ø  ø  a ã › O(n · m) O(strlen(string) + strlen(pat)) › à  − Knuth-Morris-Pratt algorithm / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 N Á Knuth-Morris-Pratt idea ! ›  øé  \÷   › ó ø §    − −      \ ø ,  ,OVUI .PSSJT 1SBUU Si ≠ a Si+1 }ª  − s= pat = − Si Si+1 = ab : Si+1 = b Si+2 ≠ c Si+1 ≠ a ‘Si+2§ pat à a b ? ? ? . . . . ?’ abcabcacab  øé ó› s= pat = − Si Si+1 Si+2 Si+3 = abca ø  ›  à s ‘- a b c a ? . . . . ?’ abcabcacab Si+4§ pat  \ r øé §à ó› s !  pat Si+4 ≠ b øé  § / / Data Structures By Hyun-Ju Park / Hanbat National University / 66 N Á    ª  f(j) = û÷ -1, − pat = abcabcacab i < j such that p0p1 ... pi = pj-ipj-i+1 ... pj, i ≥ 0 i  \  j pat f − : : \? ø  ø 0 a -1 1 b -1 2 c -1 3 a 0 4 b 1 5 c 2 6 a 3 7 c -1 8 a 0 9 b 1 ó`  ó› › ô  !! ô ø  , ø s   ó øé  § ø \  o ›,  ø − : \? r –1 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66 N Á   _     ,OVUI .PSSJT 1SBUU -  ª  int pmatch(char *string, char *pat) { int i = 0, j = 0; int lens = strlen(string), lenp = strlen(pat); while (i < lens && j < lenp) { if (string[i] == pat[j]) { i++; j++; } else if (j == 0) i ++; else j = failure[j – 1] + 1; } return (j == lenp) ? (i – lenp) : –1 ); } − : pat = abcabcacab string = abcabcabcabcacababcabc / / Data Structures By Hyun-Ju Park / Hanbat National University / 66 N Á      }  } ª ]       − − pmatch : O(m) = O(strlen(string)) fail : O(n) = O(strlen(pat)) O(m + n) = O(strlen(string) + strlen(pat)) / / Data Structures   j=0 − 1, m f ( j ) = f ( j − 1) + 1, m Pf k ( j −1) +1 = Pj − 1, k void fail(char *pat) { int n = strlen(pat); failure[0] = -1; for (j = 1; j < n; j++) { i = failure[j – 1]; while ((pat[j] != pat[i + 1]) && (i >= 0)) i = failue[i]; if (pat[j] = pat[i + 1]) failure[j] = i + 1; else failure[j] = -1; } } By Hyun-Ju Park / Hanbat National University / 66 \ N Á ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online