# String Matching algorithm ![](https://upload-images.jianshu.io/upload_images/7130568-e10dc137e9083a0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ## Rabin-Karp We can view a string of k characters (digits) as a length-k decimal number. E.g., the string “31425” corresponds to the decimal number 31,425. - Given a pattern P [1..m], let p denote the corresponding decimal value. - Given a text T [1..n], let $t_s$ denote the decimal value of the length-m substring T [(s+1)..(s+m)] for s=0,1,…,(n-m). - let `d` be the radix of num, thus $d = len(set(s))$ - $t_s$ = p iff T [(s+1)..(s+m)] = P [1..m]. - p can be computed in O(m) time. p = P[m] + d\*(P[m-1] + d\*(P[m-2]+…)). - t0 can similarly be computed in O(m) time. - Other $t_1,\ldots,t_{n-m}$ can be computed in O(n-m) time since $t_{s+1} can be computed from ts in constant time. Namely, $$ t_{s+1} = d*(t_s-d^{m-1} * T[s+1])+T[s+m+1] $$ However, it's no need to calculate $t_{s+1}$ directly. We can use modulus operation to reduce the work of caculation. We choose a small prime number. Eg 13 for radix( noted as d) 10. Generally, d\*q should fit within one computer word. We firstly caculate t0 mod q. Then, for every $t_i (i>1)$ assume $$ t_{i-1} = T[i+m-1] + 10*T[i+m-2]+\ldots+10^{m-1}*T[i-1] $$ denote $ d' = d^{m-1}\ mod\ q$ thus, $$ \begin{aligned} t_i &= (t_{i-1} - d^{m-1}*T[i-1]) * d + T[i+m]\\ &\equiv (t_{i-1} - d^{m-1}*T[i-1]) * d + T[i+m] (mod\ q)\\ &\equiv (t_{i-1}- ( d^{m-1} mod \ q) *T[i-1]) * d + T[i+m] (mod\ q)\\ &\equiv (t_{i-1}- d'*T[i-1]) * d + T[i+m] (mod\ q) \end{aligned} $$ So we can compare the modular value of each ti with p's. Only if they are the same, then we compare the origin chracter, namely $T[i],T[i+1],\ldots,T[i+m-1]$ and the pattern. Gernerally, this algorithm's time approximation is O(n+m), and the worst case is O((n-m+1)\*m) **Problem: this is assuming p and ts are small numbers. They may be too large to work with easily.** ## FSM A FSM can be represented as (Q,q0,A,S,C), where - Q is the set of all states - q0 is the start state - $A\in Q$ is a set of accepting states. - S is a finite input alphabet. - C is the set of transition functions: namely $q_j = c(s,q_i)$. Given a pattern string S, we can build a FSM for string matching. Assume S has m chars, and there should be m+1 states. One is for the begin state, and the others are for matching state of each position of S. Once we have built the FSM, we can run it on any input string. ## KMP >Knuth-Morris-Pratt method The idea is inspired by FSM. We can avoid computing the transition functions. Instead, we compute a prefix functi`Next` on P in O(m) time, and Next has only m entries. > Prefix funtion stores info about how the pattern matches against shifts of itself. - String w is a prefix of string x, if x=wy for some string y - String w is a suffix of string x, if x=yw for some string y - The k-character prefix of the pattern P [1..m] denoted by Pk. - Given that pattern prefix P [1..q] matches text characters T [(s+1)..(s+q)], what is the least shift s'> s such that P [1..k] = T [(s'+1)..(s'+k)] where s'+k=s+q? - At the new shift s', no need to compare the first k characters of P with corresponding characters of T. Method: For prefix pi, find the longest proper prefix of pi that is also a suffix of pi. next[q] = max{k|k\