1 Introduction
① string matching, violent time complexity O (N * M), once a match failure occurs, S series and T series must fall back to the starting point, to start a match.
int ViolentMatch (char * S, char * T) { int sLen = strlen (S); int tLen = strlen (T); for (int I = 0; I <sLen; I ++) {// S strings int j = 0 ; for (; J <tLen; J ++) {// T string if (s [i + j] = t [j]!) break; // match fails } IF (J == tLen) I return; // match completely } return -1; }
Exposure problem: once the failure, S series and T series will need to roll back occurs starting point matching. Is there any way you can just let the string rollback T, S series does not roll back? KMP application was born.
②KMP time complexity of O (N + M)
Idea: Every time the match fails, can not fail to rollback, S does not roll back the string, T falls back to maximum prefix string, the introduction of next array.
Reference: https://blog.csdn.net/wardseptember/article/details/78801491