How is the time complexity of KMP algorithm calculated?

suppose that the starting position of the N string is found in the M string, and the length is m and n, respectively. Using the KMP algorithm, it is generally believed that the time complexity is O (m), that is, the time complexity of calculating the next array is O (n), and the matching time is O (m),. But in the matching, it is assumed that I is used to identify the position in M, j is used to identify the position in N, and I is understandably increased but not decreased. But j will also backtrack according to the next array, and it may backtrack many times, so why is the time complexity of matching not O (masked n)? Solve

Mar.05,2021

  1. the number of comparisons when calculating Partial_Table (or the list of longest common prefix suffixes of a pattern string) is between [mforce 2m], assuming the length of the pattern string at m.
  2. the number of comparisons between pattern strings and substrings is between [n], and the worst case is T = "aaaabaaaab", P = "aaaaa".

so the time complexity of the algorithm is O (massin).


j backtracking, but forward


The backtracking of

j will only affect the upper and lower bounds of the number of search main cycles ([m, 2m]), not the relationship that makes it m _ n as you said.

you can understand that since m only increases but does not decrease, the worst-case scenario goes like this:

  1. every match fails (m remains the same)
  2. failed to match again (massi1)

in this case, each character in M is actually compared twice, so a total of 2m loops are executed. This is the upper limit of the number of cycles. In any other case, there are at least several cycles that are m direct + 1.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9fa44-1a73.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9fa44-1a73.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?