These days I turned up the algorithm book by accident. When I saw the KMP algorithm, I suddenly found that I had not fully understood it for many years, so I had to make up for it. I wrote a program and understood the fail function and KMP ~~~~
For specific procedures, see 1 package test;
2 /***//**
3 * @ author Jia Yu
4 * @ date 2010-9-28
5 */
6 public class StringMatch {
7
8 private int [] f;
9 /***//**
10 * KMP fail function to calculate f []
11 * f [j] = k <j where k is the maximum of pat [0 .. k] = pat [j-k.. j]
12*=-1 otherwise
13 * @ param pat
14 */
15 public void fail (String pat ){
16 int lenP = pat. length ();
17 f = new int [lenP];
18 f [0] =-1;
19 for (int j = 1; j <lenP; j ++ ){
20 int I = f [J-1];
21 while (pat. charAt (j) = pat. charAt (I + 1) & (I> = 0) I = f [I];
22 if (pat. charAt (j )! = Pat. charAt (I + 1) f [j] = I + 1;
23 else f [j] =-1;
24}
25}
26
27 /***//**
28 * Implementation of KMP algorithm.
29 * @ param s string which is source string
30 * @ param pat string pattern
31 * @ return
32 */
33 public int kmp_find (String s, String pat ){
34 int lenS, lenP;
35 lenS = s. length ();
36 lenP = pat. length ();
37 int I, j;
38 I = j = 0;
39 while (I <lenS & j <lenP ){
40 if (s. charAt (I) = pat. charAt (j )){
41 I ++;
42 j ++;
43}
44 else {
45 if (j = 0) I ++;
46 else j = f [J-1] + 1;
47}
48}
49 if (j <lenP | lenP = 0) return-1;
50 else return I-lenP;
51}
52
53 /***//**
54 * @ param args
55 */
56 public static void main (String [] args ){
57 // TODO Auto-generated method stub
58 StringMatch ss = new StringMatch ();
59 String s = "abcdedabc ";
60 String pat = "dab ";
61 ss. fail (pat );
62 System. out. println (ss. kmp_find (s, pat ));
63}
64
65}
66
After completion, I couldn't help but want to compare the performance with the String indexOf. I always thought that the indexOf String in jdk src was implemented using Na into ve string search algorithm. I don't have any skills. Of course, there are also many people who talk about this, but I have visited some forums and remember that people just implement KMP on their own and then say how good KMP is, however, it is rare to compare data. So today, I am still taking some time to look at some items related to String match in my resume. I wrote some code and found some stuff ~~~~
Not much gossip. First, we made a comparison between KMP and String indexOf. Let's take a look at the results.
Preprocess using 7549ns
========================================================== ==================
Cycles: 30000
String find pat in pos-1
Used Time is 87134ns
KMP find pat in pos-1
Used Time is invalid 1829ns
========================================================== ==================
Cycles: 90000
String find pat in pos-1
Used Time is 150480ns
KMP find pat in pos-1
Used Time is 2277475ns
========================================================== ==================
Cycles: 270000
String find pat in pos-1
Used Time is running 815ns
KMP find pat in pos-1
Used Time is 848257ns
========================================================== ==================
Cycles: 810000
String find pat in pos 119457
Used Time is 509997ns
KMP finds pat in pos 119457
Used Time is 1141992ns
========================================================== ==================
Cycles: 2430000
String find pat in pos 459895
Used Time is 1845130ns
KMP finds pat in pos 459895
Used Time is 4180643ns
The test code is as follows:
1 /***//**
2 *
3 */
4 package test;
5
6 import java. util. Random;
7
8 /***//**
9 * @ author Jia Yu
10 * @ date 2010-9-28
11 */
12 public class StringMatchTest2 {
13
14 private static String src;
15 private static String pat;
16 private static long cycles = CMDL;
17 private static Random rand = new Random (47 );
18
19 public static void generateSource (){
20 StringBuilder sb = new StringBuilder ();
21 int iter = (int) cycles;
22 for (int I = 0; I <iter; I ++ ){
23 sb. append (char) (a + (rand. nextInt (6 ))));
24}
25 src = sb. toString ();
26}
27
28 public static void generatePattern (){
29 StringBuilder sb = new StringBuilder ();
30 for (int I = 0; I <7; I ++ ){
31 sb. append (char) (a + (rand. nextInt (6 ))));
32}
33 pat = sb. toString ();
34}
35
36 /***//**
37 * @ param args
38 */
39 public static void main (String [] args ){
40 // TODO Auto-generated method stub
41 generatePattern ();
42 StringMatch sm = new StringMatch ();
43 long start, pre, dur;
44 start = System. nanoTime ();
45 sm. fail (pat );
46 pre = System. nanoTime ()-start;
47 System. out. println ("