HDU 2222 -- keywords search (AC automatic machine)

Source: Internet
Author: User

Keywords search Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 34020 accepted submission (s): 11009


Problem descriptionin the modern time, search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you shoshould tell me how many keywords will be match.
 
Inputfirst line will contain one integer means how many cases will follow.
Each case will contain two integers n means the number of keywords and N keywords follow. (n <= 10000)
Each keyword will only contains characters 'a'-'Z', And the length will not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
 
Outputprint how many keywords are contained in the description.
Sample Input
15shehesayshrheryasherhs
 
Sample output
3
 
------------------------------------------------


# Include <iostream> # include <cstring> # include <cstdio> # include <cstdlib> # include <queue> # define sigma_size 26 using namespace STD; char STR [55], s [1000001]; struct node {int count; node * Next [sigma_size]; node * fail;}; node * root = new node; void Init (node * P) {memset (p-> next, 0, sizeof p-> next); P-> COUNT = 0; P-> fail = NULL;} void insert (char * s) {int L = strlen (s); node * P = root, * q; For (INT I = 0; I <L; ++ I) {int c = s [I]-'A'; If (p-> next [c] = NULL) {q = new node; Init (Q); P-> next [c] = Q ;} P = p-> next [c];} p-> count ++;} void get_fail () {node * P; queue <node *> q; q. push (Root); While (! Q. empty () {P = Q. front (); q. pop (); For (INT I = 0; I <sigma_size; ++ I) {If (p-> next [I] = NULL) continue; q. push (p-> next [I]); If (P = root) {P-> next [I]-> fail = root ;} else {node * u = P; while (u-> fail! = NULL & U-> fail-> next [I] = NULL) u = u-> fail; If (u-> fail = NULL) p-> next [I]-> fail = root; else p-> next [I]-> fail = u-> fail-> next [I] ;}} int find (char * s) {node * P = root, * q; int L = strlen (s), Res = 0; For (INT I = 0; I <L; ++ I) {int c = s [I]-'A'; while (P! = Root & P-> next [c] = NULL) P = p-> fail; If (p-> next [c] = NULL) continue; P = p-> next [c]; q = P; while (Q! = Root & Q-> count! =-1) {res + = Q-> count; q-> COUNT =-1; q = Q-> fail ;}} return res ;} void freedom (node * P) {for (INT I = 0; I <26; ++ I) {If (p-> next [I]! = NULL) {freedom (p-> next [I]) ;}} Delete P ;}int main () {int t, n; scanf ("% d ", & T); While (t --) {Init (Root); scanf ("% d", & N); getchar (); While (n --) {gets (STR); insert (STR);} get_fail (); gets (s); printf ("% d \ n", find (s )); for (INT I = 0; I <26; I ++) {// note that root cannot delete if (root-> next [I]! = NULL) Freedom (root-> next [I]) ;}} return 0 ;}



# Include <iostream> # include <cstring> # include <cstdlib> # include <queue> # include <cstdio> # define maxnode 500000 + 10 # define sigma_size 26 # define M 1000000 + 10 using namespace STD; char STR [m]; struct tree {int f [maxnode]; int ch [maxnode] [sigma_size]; int Val [maxnode]; int SZ; void reset () {memset (CH [0], 0, sizeof ch [0]); memset (F, 0, sizeof F); memset (Val, 0, sizeof Val ); SZ = 1 ;}int idx (char c) {return C-'A';} void inser T (char * s) {int L = strlen (s); int u = 0; For (INT I = 0; I <L; ++ I) {int c = idx (s [I]); If (! Ch [u] [c]) {memset (CH [SZ], 0, sizeof ch [SZ]); ch [u] [c] = SZ ++ ;} U = CH [u] [c];} Val [u] ++;} void get_fail () {queue <int> q; For (int c = 0; c <sigma_size; ++ c) {int u = CH [0] [c]; If (u) {f [u] = 0; q. push (u) ;}} while (! Q. empty () {int r = Q. front (); q. pop (); For (INT I = 0; I <sigma_size; ++ I) {int u = CH [r] [I]; If (! U) continue; q. Push (U); int v = f [R]; // pointer to the father's failure while (V &&! Ch [v] [I]) V = f [v]; // the parent's failure pointer's son does not exist C, continue following the failure pointer f [u] = CH [v] [I] ;}} int find (char * s) {int L = strlen (s ); int u = 0, Res = 0; For (INT I = 0; I <L; ++ I) {int c = idx (s [I]); while (U &&! Ch [u] [c]) u = f [u]; If (! Ch [u] [c]) continue; // skip u = CH [u] [c] if it does not match; // match, then, the node continues to match the int TMP = u; // temp pointer to find a shorter prefix while (TMP & Val [TMP]! =-1) {res + = Val [TMP]; Val [TMP] =-1; TMP = f [TMP] ;}} return res ;}} trie; int main () {int t; CIN> T; while (t --) {trie. reset (); int N; scanf ("% d", & N); While (n --) {scanf ("% s", STR); trie. insert (STR);} trie. get_fail (); scanf ("% s", STR); printf ("% d \ n", trie. find (STR);} return 0 ;}















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.