How to solve the problem: Jump count with next array.
Problem Solving Code:
1 //File name:d.cpp2 //Author:darkdream3 //Created time:2015 April 12 Sunday 19:40 52 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - - using namespacestd; - intN; - intT; in Charstr[300000]; - intLen; to intnext[300000]; + voidGet_next () - { thenext[0] = -1; * intK =-1; $ intj =0 ; Panax Notoginseng while(J <len) - { the if(k = =-1|| STR[J] = =Str[k]) + { A++K; the++J; +NEXT[J] =K; -}Else{ $K =Next[k]; $ } - } - } the intFindintx) - {Wuyi if(x = =0) the return 0; - return 1+find (Next[x]); Wu } - intMain () { Aboutscanf"%d",&t); $ while(t--) - { -scanf"%s", str); -Len =strlen (str); Ascanf"%s",&Str[len]); +Len =strlen (str); the Get_next (); -printf"%d\n", find (len)); $ } the return 0; the}
View Code
Acdream Hut Freshman Training tournament D-Nana in Wonderland Series-the quirks of the villagers KMP