Oulipo
To give you a string to find the string containing the number of specified substrings
As long as you know KMP, this problem is not too simple, notice that the STL string will be timed out, or use char bar
1#include <iostream>2#include <algorithm>3#include <functional>4#include <string.h>5 6 using namespacestd;7 8typedefintPosition;9 Static Chartext[1000002], word[10002];Ten Static intnext[10003]; One A voidKmpsearch (Const int,Const int); - voidGetNext (Const int); - the intMainvoid) - { - intcase_sum; -scanf"%d", &case_sum); + GetChar (); - + while(case_sum--) A { atscanf"%s", Word); -scanf"%s", Text); - Kmpsearch (strlen (Word), strlen (Text)); - } - returnexit_success; - } in - voidKmpsearch (Const intW_len,Const intT_len) to { +Position i =0, j =0; - intK_count =0; the GetNext (W_len); * $ while(I <T_len)Panax Notoginseng { - if(j = =-1|| Text[i] = =Word[j]) the { +i++; AJ + +; the if(J = =W_len) + { -k_count++; $j =Next[j]; $ } - } - Elsej =Next[j]; the } -printf"%d\n", k_count);Wuyi } the - voidGetNext (Const intW_len) Wu { -Position i =0, k =-1; Aboutnext[0] = -1; $ - while(I <W_len) - { - if(k = =-1|| Word[i] = =Word[k]) A { +i++; thek++; -Next[i] = word[i]! = word[k]?K:next[k]; $ } the ElseK =Next[k]; the } the}
Match:oulipo (POJ 3461)