zoukankan      html  css  js  c++  java
  • Rabin-Karp 字符串匹配算法

     1 public class Main  
     2 { 
     3     // d is the number of characters in the input alphabet 
     4     public final static int d = 256; 
     5       
     6     /* pat -> pattern 
     7         txt -> text 
     8         q -> A prime number 
     9     */
    10     static void search(String pat, String txt, int q) 
    11     { 
    12         int M = pat.length(); 
    13         int N = txt.length(); 
    14         int i, j; 
    15         int p = 0; // hash value for pattern 
    16         int t = 0; // hash value for txt 
    17         int h = 1; 
    18       
    19         //  pow(d, M-1)%q 
    20         for (i = 0; i < M-1; i++) 
    21             h = (h*d)%q; 
    22       
    23         // Calculate the hash value of pattern and first 
    24         // window of text 
    25         for (i = 0; i < M; i++) 
    26         { 
    27             p = (d*p + pat.charAt(i))%q; 
    28             t = (d*t + txt.charAt(i))%q; 
    29         } 
    30       
    31         // Slide the pattern over text one by one 
    32         for (i = 0; i <= N - M; i++) 
    33         { 
    34       
    35             // Check the hash values of current window of text 
    36             // and pattern. If the hash values match then only 
    37             // check for characters on by one 
    38             if ( p == t ) 
    39             { 
    40                 /* Check for characters one by one */
    41                 for (j = 0; j < M; j++) 
    42                 { 
    43                     if (txt.charAt(i+j) != pat.charAt(j)) 
    44                         break; 
    45                 } 
    46       
    47                 // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1] 
    48                 if (j == M) 
    49                     System.out.println("Pattern found at index " + i); 
    50             } 
    51       
    52             // Calculate hash value for next window of text: Remove 
    53             // leading digit, add trailing digit 
    54             if ( i < N-M ) 
    55             { 
    56                 t = (d*(t - txt.charAt(i)*h) + txt.charAt(i+M))%q; 
    57       
    58                 // We might get negative value of t, converting it 
    59                 // to positive 
    60                 if (t < 0) 
    61                 t = (t + q); 
    62             } 
    63         } 
    64     } 
    65       
    66     /* Driver program to test above function */
    67     public static void main(String[] args) 
    68     { 
    69         String txt = "GEEKS FOR GEEKS"; 
    70         String pat = "GEEK"; 
    71         int q = 101; // A prime number 
    72         search(pat, txt, q); 
    73     } 
    74 } 
  • 相关阅读:
    前端分布引导插件IntroJs的使用
    分步引导中,Js操作Cookie,实现判断用户是否第一次登陆网站
    android 5.0新特性CardView教程
    Android使用NumberPicker控件实现选择城市,生日
    程控交换机是什么东东!
    sip消息 响应状态码解析大全
    测试人员必看的经典书籍
    mysql创造并使用它
    linux系统备份与还原
    BNF范式(巴科斯范式)简介
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/10495661.html
Copyright © 2011-2022 走看看