際際滷

際際滷Share a Scribd company logo
By油 TheLlama TopCoder Member
 The "Naive" Method    Rabin-Karp Algorithms ( 朱 - 豺危 )     Knuth-Morris-Pratt Algorithms ( 讀 - 覈襴 -  )
function  brute_force  ( text [],   pattern [])  { // n   text  蠍語 // m  谿場朱る 伎 蠍語 for ( i  = 0;  i  <  n ;  i ++) { for ( j  = 0;  j  <  m  &&  i  +  j  <  n ;  j ++) {   if ( text [ i + j ]   !=  pattern [ j ])   break; } //  るジ 蠍螳 朱 襭襯 觜碁 if ( j  ==  m )   //  殊 蟆郁骸襯 谿場 } }
text[]  O(nm) pattern []  A B A B C A A B C C A B C A B C A B C A B C A B C A B C A B C A B C
油
< 朱 - 豺危 螻襴讀 > 覓語伎 企麹 伎螳 襷 譟伎る , 伎螳 螳  覓語伎 殊 !! 轟 螳 企  n 讌覯 螳 襷  譟伎 蟆螻  ex) 40 (10)  = 101000 (2)  = 50 (8)  = 2F (16)
Case :  伎 蠍語願  3  蟆曙 伎螳 H 0  = H s[0]..s[2]  = s[0] * B 2  + s[1] * B + s[2] H 1  = H s[1]..s[3]  = s[1] * B 2  + s[2] * B + s[3] H 2  = H s[2]..s[4]  = s[2] * B 2  + s[3] * B + s[4] H 3  = H s[3]..s[5]  = s[3] * B 2  + s[4] * B + s[5] 鐚 鐚 S: 覓語   H: 伎螳   m: 伎 蠍語 B:  B >=  覓語伎  覓語
豐蠍郁 H 0 = H s[0]s[m-1]  = = s[0]*B m-1  + s[1]*B m-2  +       +   s[m-2]*B + s[m-1] 蟯螻 H n = H s[n]..s[n+m-1]  =(H n-1  - s[n-1]*B m-1  ) * B + s[n+m-1] S: 覓語   H: 伎螳   m: 伎 蠍語 B:  B >=  覓語伎  覓語
// a 螳  蟆曙一 襾語襯 蟲蠍   int_mod ( int  a ,   int  b )   {   return  ( a  %  b  +  b ) %  b ; } function   Rabin_Karp ( text [],   pattern []) { // n: 覓語 蠍語  m:  蠍語  B: 讌覯  // M: 企   ( る襦覦讌 ) if ( n  <  m )   return ;   //  伎 蠍語願  蠍碁 轟壱 覿殊 //  伎 伎螳 蟲 , hp = 0; for ( i  = 0;  i  <  m ;  i ++)   hp  =   int_mod ( hp   *   B  +  pattern [ i ],  M ); //  覓語伎 蠍語  m 襷殊 伎螳 蟲 , ht = 0; for ( i  = 0;  i  <  m ;  i ++)   ht  =   int_mod ( ht   *   B  +  text [ i ],  M ); れ 伎襦 伎伎
if ( ht  ==  hp )   //  ろ語  願骸 殊讌  //  覓語伎 磯手覃伎 伎螳 蟲螻 觜蟲襯 覦覲 // E = ( B m-1 )%M  for ( i  =  m ;  i  <  n ;  i ++) { ht =  int_mod ( ht   -  int_mod ( text [ i  -  m ]   *  E ,  M ),  M ); ht =  int_mod ( ht   *   B ,  M ); ht =  int_mod ( ht   +   text [ i ],  M ); if ( ht   ==   hp )   //  伎螳 殊覃 狩 覓語  } } 伎 伎 伎伎
text[]  pattern []  覓語 :3 A B A B C A A B C C A B C
text[]  ht O( n ) A B A B C A A B C C 3 10 4 15 18 1 4 11 X X
< 讀 - 覈襴 -   > 谿剰  伎 覈 覿 覓語伎  覿蟆 觜蟲 蟆 譴 蟆螳 豢 . 覓語 蟆 ろ 蟆曙 襷 豌朱  螳 蟆  れ 豺襦 企 蟆襯 螻
pattern []   誤 覓語伎 り 螳朱伎 螳 蠍 覓語伎  A B C D A B D
pattern []  STATE0 竜 STATE2 AB STATE1 A STATE3 ABC STATE4 ABCD STATE6 ABCDAB STATE5 ABCDA A A B B D D C ろ ろ  襾語 ろ  STATE0 朱 螳 !! A B C D A B D STATE7 ABCDABD
// F[]   failure function 朱 覿襴覃 螳  豺襯 ロ function  build_failure_function ( pattern []) { // m: 伎 蠍語 F[0] = F[1] = 0;  //   焔渚 for ( i  = 2;  i  <=  m ;  i ++) { j  = F[ i  - 1];  // j:i-1  螳 蠍 る覿 覓語 蠍語 for ( ; ; ) { if ( pattern [ j ] ==  pattern [ i -1]) {F[ i ] =  j  + 1;  break ;} // pattern[j]  覿覿 覓語 れ 襦 覓語 // pattern[i-1]  結覿 覓語 れ 襦 覓語 if ( j  == 0) { F[ i ] = 0;  break ; } //  覓語螳 るゴ覃伎  j=0 企 螻 讌  0 j  = F[ j ];  //  るゴ讌襷  j!=0,  れ朱 蠍 る語 蟆   } }
function  Knuth_Morris_Pratt ( text [],  pattern []) { // n: ろ語 蠍語  m: 伎 蠍語 // F[]  the &quot;failure function build_failure_function( pattern []);  i  = 0;  //  殊 覓語  j  = 0;  //  ろ語 豌 覓語 for ( ; ; ) { if ( j  ==  n )  break ;  //  ろ語  覃 襭 譬襭 if ( text [ j ] ==  pattern [ i ]) { i ++;  //  殊 覓語 讀螳 j ++;  //  れ 覓語 觜蟲 if ( i  ==  m )  //  ろ語 願骸 殊 覓語 覦蟆 !!! } //  覓語螳 るゼ 蟆曙 , i 螳  0  朱 れ 襦 企 else   if ( i  > 0)  i  = F[ i ]; //  覓語螳 るゼ蟆曙 , i 螳  0 企朱 ろ語 れ 覓語襦 企 else   j ++; } }
text[]  ...... ...... Worst Case : O( nm ) Best Case : O(m+n) A B C A B C D A B A B C D A B C D A B D E A B C D A B D A B C D A B D A B C D A B D A B C D A B D A B C D A B D
油

More Related Content

What's hot (20)

9. pointer
9. pointer9. pointer
9. pointer
誤一蠍一 (2) - 誤 蠍1
誤一蠍一 (2) - 誤 蠍1誤一蠍一 (2) - 誤 蠍1
誤一蠍一 (2) - 誤 蠍1
Hoyoung Jung
7蠏碁9 貊
7蠏碁9 貊7蠏碁9 貊
7蠏碁9 貊
herojoon1378
伎一 Project5
伎一 Project5伎一 Project5
伎一 Project5
KoChungWook
2012 Ds B1 01
2012 Ds B1 012012 Ds B1 01
2012 Ds B1 01
seonhyung
4. 誤
4. 誤4. 誤
4. 誤
Hoyoung Jung
Project#7 Group Codes Hwp
Project#7 Group Codes HwpProject#7 Group Codes Hwp
Project#7 Group Codes Hwp
Kimjeongmoo
=求戟求 戟 メ求п.Key
=求戟求 戟 メ求п.Key=求戟求 戟 メ求п.Key
=求戟求 戟 メ求п.Key
Seokju Hong
誤一 覦一
誤一 覦一誤一 覦一
誤一 覦一
Kim YoSep
=求メ 罪,п,覓語
=求メ 罪,п,覓語=求メ 罪,п,覓語
=求メ 罪,п,覓語
HoYong Na
2012 Ds 01
2012 Ds 012012 Ds 01
2012 Ds 01
Jungyerin
Python3 10 覓語伎伎手鍵
Python3 10 覓語伎伎手鍵Python3 10 覓語伎伎手鍵
Python3 10 覓語伎伎手鍵
Jihoon Kong
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10
Nam Hyeonuk
C Language For Arduino
C Language For ArduinoC Language For Arduino
C Language For Arduino
[TAOCP] 1.3.1 MIX る
[TAOCP] 1.3.1 MIX る[TAOCP] 1.3.1 MIX る
[TAOCP] 1.3.1 MIX る
譬觜
03. function in typescript
03. function in typescript03. function in typescript
03. function in typescript
Han JaeYeab
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
譴 蟾
2012 Ds 05
2012 Ds 052012 Ds 05
2012 Ds 05
Jungyerin
9. pointer
9. pointer9. pointer
9. pointer
誤一蠍一 (2) - 誤 蠍1
誤一蠍一 (2) - 誤 蠍1誤一蠍一 (2) - 誤 蠍1
誤一蠍一 (2) - 誤 蠍1
Hoyoung Jung
伎一 Project5
伎一 Project5伎一 Project5
伎一 Project5
KoChungWook
2012 Ds B1 01
2012 Ds B1 012012 Ds B1 01
2012 Ds B1 01
seonhyung
Project#7 Group Codes Hwp
Project#7 Group Codes HwpProject#7 Group Codes Hwp
Project#7 Group Codes Hwp
Kimjeongmoo
=求戟求 戟 メ求п.Key
=求戟求 戟 メ求п.Key=求戟求 戟 メ求п.Key
=求戟求 戟 メ求п.Key
Seokju Hong
誤一 覦一
誤一 覦一誤一 覦一
誤一 覦一
Kim YoSep
=求メ 罪,п,覓語
=求メ 罪,п,覓語=求メ 罪,п,覓語
=求メ 罪,п,覓語
HoYong Na
2012 Ds 01
2012 Ds 012012 Ds 01
2012 Ds 01
Jungyerin
Python3 10 覓語伎伎手鍵
Python3 10 覓語伎伎手鍵Python3 10 覓語伎伎手鍵
Python3 10 覓語伎伎手鍵
Jihoon Kong
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10
Nam Hyeonuk
C Language For Arduino
C Language For ArduinoC Language For Arduino
C Language For Arduino
[TAOCP] 1.3.1 MIX る
[TAOCP] 1.3.1 MIX る[TAOCP] 1.3.1 MIX る
[TAOCP] 1.3.1 MIX る
譬觜
03. function in typescript
03. function in typescript03. function in typescript
03. function in typescript
Han JaeYeab
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
譴 蟾
2012 Ds 05
2012 Ds 052012 Ds 05
2012 Ds 05
Jungyerin

Viewers also liked (20)

Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
Arduino
ArduinoArduino
Arduino
Jerin John
disjoint-set data structures
disjoint-set data structuresdisjoint-set data structures
disjoint-set data structures
skku_npc
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
Samsil Arefin
Java basics
Java basicsJava basics
Java basics
Sardar Alam
Revision c odesagain
Revision c odesagainRevision c odesagain
Revision c odesagain
rex0721
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
introduction to python
introduction to pythonintroduction to python
introduction to python
Sardar Alam
J2ME
J2MEJ2ME
J2ME
Kumar Gaurav
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
OXUS 20
Structure programming Java Programming Theory
Structure programming  Java Programming  TheoryStructure programming  Java Programming  Theory
Structure programming Java Programming Theory
OXUS 20
Makers, IoT, Third Industrial Revolution and (im)Precision Agriculture
Makers, IoT, Third Industrial Revolution and (im)Precision AgricultureMakers, IoT, Third Industrial Revolution and (im)Precision Agriculture
Makers, IoT, Third Industrial Revolution and (im)Precision Agriculture
Davide Gomba
Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)
Adam Mukharil Bachtiar
IoT for indian agriculture
IoT  for indian agricultureIoT  for indian agriculture
IoT for indian agriculture
Ravi Mundada
Smart farming using ARDUINO (Nirma University)
Smart farming using ARDUINO (Nirma University)Smart farming using ARDUINO (Nirma University)
Smart farming using ARDUINO (Nirma University)
Raj Patel
IoT in agri-food
IoT in agri-foodIoT in agri-food
IoT in agri-food
Sjaak Wolfert
IOT based smart security and monitoring devices for agriculture
IOT based smart security and monitoring devices for agriculture IOT based smart security and monitoring devices for agriculture
IOT based smart security and monitoring devices for agriculture
sneha daise paulson
Internet of things for agriculture
Internet of things for agricultureInternet of things for agriculture
Internet of things for agriculture
KG2
Connected Agricultural services and internet of things..
Connected Agricultural services and internet of things..Connected Agricultural services and internet of things..
Connected Agricultural services and internet of things..
Atul Khiste
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
disjoint-set data structures
disjoint-set data structuresdisjoint-set data structures
disjoint-set data structures
skku_npc
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
Samsil Arefin
Revision c odesagain
Revision c odesagainRevision c odesagain
Revision c odesagain
rex0721
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
introduction to python
introduction to pythonintroduction to python
introduction to python
Sardar Alam
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
OXUS 20
Structure programming Java Programming Theory
Structure programming  Java Programming  TheoryStructure programming  Java Programming  Theory
Structure programming Java Programming Theory
OXUS 20
Makers, IoT, Third Industrial Revolution and (im)Precision Agriculture
Makers, IoT, Third Industrial Revolution and (im)Precision AgricultureMakers, IoT, Third Industrial Revolution and (im)Precision Agriculture
Makers, IoT, Third Industrial Revolution and (im)Precision Agriculture
Davide Gomba
Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)
Adam Mukharil Bachtiar
IoT for indian agriculture
IoT  for indian agricultureIoT  for indian agriculture
IoT for indian agriculture
Ravi Mundada
Smart farming using ARDUINO (Nirma University)
Smart farming using ARDUINO (Nirma University)Smart farming using ARDUINO (Nirma University)
Smart farming using ARDUINO (Nirma University)
Raj Patel
IOT based smart security and monitoring devices for agriculture
IOT based smart security and monitoring devices for agriculture IOT based smart security and monitoring devices for agriculture
IOT based smart security and monitoring devices for agriculture
sneha daise paulson
Internet of things for agriculture
Internet of things for agricultureInternet of things for agriculture
Internet of things for agriculture
KG2
Connected Agricultural services and internet of things..
Connected Agricultural services and internet of things..Connected Agricultural services and internet of things..
Connected Agricultural services and internet of things..
Atul Khiste

Similar to String Searching Algorithms (13)

Haskell study 4
Haskell study 4Haskell study 4
Haskell study 4
Nam Hyeonuk
伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶
Han Sung Kim
遺襭
遺襭遺襭
遺襭
koominsu
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
2012 Dm 07
2012 Dm 072012 Dm 07
2012 Dm 07
Jungyerin
襭蟲譟 Project6
襭蟲譟 Project6襭蟲譟 Project6
襭蟲譟 Project6
KoChungWook
襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願
mil23
蠏 Regular expression (regex)
蠏 Regular expression (regex)蠏 Regular expression (regex)
蠏 Regular expression (regex)
Sunyoung Kim
[D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語 [D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語
NAVER D2
貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)
貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)
貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)
旧價拘磯
2012 Ds A1 05
2012 Ds A1 052012 Ds A1 05
2012 Ds A1 05
seonhyung
Haskell study 4
Haskell study 4Haskell study 4
Haskell study 4
Nam Hyeonuk
伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶
Han Sung Kim
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
2012 Dm 07
2012 Dm 072012 Dm 07
2012 Dm 07
Jungyerin
襭蟲譟 Project6
襭蟲譟 Project6襭蟲譟 Project6
襭蟲譟 Project6
KoChungWook
襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願
mil23
蠏 Regular expression (regex)
蠏 Regular expression (regex)蠏 Regular expression (regex)
蠏 Regular expression (regex)
Sunyoung Kim
[D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語 [D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] る SCCC 襦蠏碁覦 蟆曙 覓語
NAVER D2
貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)
貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)
貊語拘 C&JAVA 蠍一螻殊 C襦蠏碁覦(3)
旧價拘磯
2012 Ds A1 05
2012 Ds A1 052012 Ds A1 05
2012 Ds A1 05
seonhyung

More from skku_npc (13)

Maximum Flow
Maximum FlowMaximum Flow
Maximum Flow
skku_npc
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
skku_npc
Line sweep algorithms
Line sweep algorithmsLine sweep algorithms
Line sweep algorithms
skku_npc
Data Structures
Data StructuresData Structures
Data Structures
skku_npc
Prime numbers, factorization
Prime numbers, factorizationPrime numbers, factorization
Prime numbers, factorization
skku_npc
Mathematics
MathematicsMathematics
Mathematics
skku_npc
Greedy is Good
Greedy is GoodGreedy is Good
Greedy is Good
skku_npc
Binary Search
Binary SearchBinary Search
Binary Search
skku_npc
How to find a solution
How to find a solutionHow to find a solution
How to find a solution
skku_npc
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
skku_npc
An introduction to recursion
An introduction to recursionAn introduction to recursion
An introduction to recursion
skku_npc
Algorithm Games
Algorithm GamesAlgorithm Games
Algorithm Games
skku_npc
Introduction to Graphs
Introduction to GraphsIntroduction to Graphs
Introduction to Graphs
skku_npc
Maximum Flow
Maximum FlowMaximum Flow
Maximum Flow
skku_npc
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
skku_npc
Line sweep algorithms
Line sweep algorithmsLine sweep algorithms
Line sweep algorithms
skku_npc
Data Structures
Data StructuresData Structures
Data Structures
skku_npc
Prime numbers, factorization
Prime numbers, factorizationPrime numbers, factorization
Prime numbers, factorization
skku_npc
Mathematics
MathematicsMathematics
Mathematics
skku_npc
Greedy is Good
Greedy is GoodGreedy is Good
Greedy is Good
skku_npc
Binary Search
Binary SearchBinary Search
Binary Search
skku_npc
How to find a solution
How to find a solutionHow to find a solution
How to find a solution
skku_npc
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
skku_npc
An introduction to recursion
An introduction to recursionAn introduction to recursion
An introduction to recursion
skku_npc
Algorithm Games
Algorithm GamesAlgorithm Games
Algorithm Games
skku_npc
Introduction to Graphs
Introduction to GraphsIntroduction to Graphs
Introduction to Graphs
skku_npc

String Searching Algorithms

  • 2. The &quot;Naive&quot; Method Rabin-Karp Algorithms ( 朱 - 豺危 ) Knuth-Morris-Pratt Algorithms ( 讀 - 覈襴 - )
  • 3. function brute_force ( text [], pattern []) { // n text 蠍語 // m 谿場朱る 伎 蠍語 for ( i = 0; i < n ; i ++) { for ( j = 0; j < m && i + j < n ; j ++) { if ( text [ i + j ] != pattern [ j ]) break; } // るジ 蠍螳 朱 襭襯 觜碁 if ( j == m ) // 殊 蟆郁骸襯 谿場 } }
  • 4. text[] O(nm) pattern [] A B A B C A A B C C A B C A B C A B C A B C A B C A B C A B C A B C
  • 5.
  • 6. < 朱 - 豺危 螻襴讀 > 覓語伎 企麹 伎螳 襷 譟伎る , 伎螳 螳 覓語伎 殊 !! 轟 螳 企 n 讌覯 螳 襷 譟伎 蟆螻 ex) 40 (10) = 101000 (2) = 50 (8) = 2F (16)
  • 7. Case : 伎 蠍語願 3 蟆曙 伎螳 H 0 = H s[0]..s[2] = s[0] * B 2 + s[1] * B + s[2] H 1 = H s[1]..s[3] = s[1] * B 2 + s[2] * B + s[3] H 2 = H s[2]..s[4] = s[2] * B 2 + s[3] * B + s[4] H 3 = H s[3]..s[5] = s[3] * B 2 + s[4] * B + s[5] 鐚 鐚 S: 覓語 H: 伎螳 m: 伎 蠍語 B: B >= 覓語伎 覓語
  • 8. 豐蠍郁 H 0 = H s[0]s[m-1] = = s[0]*B m-1 + s[1]*B m-2 + + s[m-2]*B + s[m-1] 蟯螻 H n = H s[n]..s[n+m-1] =(H n-1 - s[n-1]*B m-1 ) * B + s[n+m-1] S: 覓語 H: 伎螳 m: 伎 蠍語 B: B >= 覓語伎 覓語
  • 9. // a 螳 蟆曙一 襾語襯 蟲蠍 int_mod ( int a , int b ) { return ( a % b + b ) % b ; } function Rabin_Karp ( text [], pattern []) { // n: 覓語 蠍語 m: 蠍語 B: 讌覯 // M: 企 ( る襦覦讌 ) if ( n < m ) return ; // 伎 蠍語願 蠍碁 轟壱 覿殊 // 伎 伎螳 蟲 , hp = 0; for ( i = 0; i < m ; i ++) hp = int_mod ( hp * B + pattern [ i ], M ); // 覓語伎 蠍語 m 襷殊 伎螳 蟲 , ht = 0; for ( i = 0; i < m ; i ++) ht = int_mod ( ht * B + text [ i ], M ); れ 伎襦 伎伎
  • 10. if ( ht == hp ) // ろ語 願骸 殊讌 // 覓語伎 磯手覃伎 伎螳 蟲螻 觜蟲襯 覦覲 // E = ( B m-1 )%M for ( i = m ; i < n ; i ++) { ht = int_mod ( ht - int_mod ( text [ i - m ] * E , M ), M ); ht = int_mod ( ht * B , M ); ht = int_mod ( ht + text [ i ], M ); if ( ht == hp ) // 伎螳 殊覃 狩 覓語 } } 伎 伎 伎伎
  • 11. text[] pattern [] 覓語 :3 A B A B C A A B C C A B C
  • 12. text[] ht O( n ) A B A B C A A B C C 3 10 4 15 18 1 4 11 X X
  • 13. < 讀 - 覈襴 - > 谿剰 伎 覈 覿 覓語伎 覿蟆 觜蟲 蟆 譴 蟆螳 豢 . 覓語 蟆 ろ 蟆曙 襷 豌朱 螳 蟆 れ 豺襦 企 蟆襯 螻
  • 14. pattern [] 誤 覓語伎 り 螳朱伎 螳 蠍 覓語伎 A B C D A B D
  • 15. pattern [] STATE0 竜 STATE2 AB STATE1 A STATE3 ABC STATE4 ABCD STATE6 ABCDAB STATE5 ABCDA A A B B D D C ろ ろ 襾語 ろ STATE0 朱 螳 !! A B C D A B D STATE7 ABCDABD
  • 16. // F[] failure function 朱 覿襴覃 螳 豺襯 ロ function build_failure_function ( pattern []) { // m: 伎 蠍語 F[0] = F[1] = 0; // 焔渚 for ( i = 2; i <= m ; i ++) { j = F[ i - 1]; // j:i-1 螳 蠍 る覿 覓語 蠍語 for ( ; ; ) { if ( pattern [ j ] == pattern [ i -1]) {F[ i ] = j + 1; break ;} // pattern[j] 覿覿 覓語 れ 襦 覓語 // pattern[i-1] 結覿 覓語 れ 襦 覓語 if ( j == 0) { F[ i ] = 0; break ; } // 覓語螳 るゴ覃伎 j=0 企 螻 讌 0 j = F[ j ]; // るゴ讌襷 j!=0, れ朱 蠍 る語 蟆 } }
  • 17. function Knuth_Morris_Pratt ( text [], pattern []) { // n: ろ語 蠍語 m: 伎 蠍語 // F[] the &quot;failure function build_failure_function( pattern []); i = 0; // 殊 覓語 j = 0; // ろ語 豌 覓語 for ( ; ; ) { if ( j == n ) break ; // ろ語 覃 襭 譬襭 if ( text [ j ] == pattern [ i ]) { i ++; // 殊 覓語 讀螳 j ++; // れ 覓語 觜蟲 if ( i == m ) // ろ語 願骸 殊 覓語 覦蟆 !!! } // 覓語螳 るゼ 蟆曙 , i 螳 0 朱 れ 襦 企 else if ( i > 0) i = F[ i ]; // 覓語螳 るゼ蟆曙 , i 螳 0 企朱 ろ語 れ 覓語襦 企 else j ++; } }
  • 18. text[] ...... ...... Worst Case : O( nm ) Best Case : O(m+n) A B C A B C D A B A B C D A B C D A B D E A B C D A B D A B C D A B D A B C D A B D A B C D A B D A B C D A B D
  • 19.