Knuth-Morris-Pratt algo(prefix function calculation).