HDU 2222 Keywords Search ac automaton template problem

Source: Internet
Author: User

Keywords SearchTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 40414 Accepted Submission (s): 12885


Problem DescriptionIn The modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring the feature to his image retrieval system.
Every image has a long description, when users type some keywords to find the image, the system would match the keywords W ITH 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 should tell me how many keywords would b E match.

Inputfirst line would contain one integer means how many cases would follow by.
Each case would contain integers n means the number of keywords and n keywords follow. (N <= 10000)
Each keyword would only contains characters ' a '-' Z ', and the length would be is not longer than 50.
The last line is the description, and the length would be a not longer than 1000000.

Outputprint How many keywords is contained in the description.
Sample Input
15shehesayshrheryasherhs

Sample Output
3
Hey, look at the AC automata theory for a long time, and then look at the template code, and finally a little thorough understanding of AC automata.
AC Automata theory: http://blog.csdn.net/niushuai666/article/details/7002823
Code:
#include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #define SIZE 1000100using namespace std; struct trie{int count; Trie *fail; Trie *next[26]; Trie () {for (int i = 0; i <; ++i) next[i] = null; fail = NULL; count = 0;}} *root; char des[size]; void Insert (char ch[]) {int len = strlen (CH);  Trie *t = root; for (int i = 0; i < len; ++i) {if (t->next[ch[i]-' a '] = = NULL) {t->next[ch[i]-' a '] = new Trie ();} t = t->next[ch[i]-' a '];} t->count++;}  void BFs () {queue<trie*> que; Que.push (root), while (!que.empty ()) {Trie *t = Que.front (), Que.pop (); for (int i = 0; i < 26; ++i) {if (T->next[i]! = NULL) {if (t = = root) T->next[i]->fail = root; Else{trie *p = T->fail; while (p) {if (p-> Next[i]) {t->next[i]->fail = p->next[i]; break;} p = p->fail;} if (p = = NULL) T->next[i]->fail = root;} Que.push (T->next[i]);}}} int query (char str[]) {int sum = 0, i = 0; Trie *now = root, while (Str[i]) {int pos = str[i]-' A ' ; while (now->next[pos] = = NULL && now! = root) {now = Now->fail;} now = Now->next[pos]; if (!now) now = root; Trie *temp = Now, while (temp! = root) {if (temp->count>=0) {sum + = Temp->count; temp->count =-1;} else break; temp = Temp->fail; }++i;} return sum; }void del (Trie *t) {for (int i = 0; i <; ++i) {if (T->next[i]) del (t->next[i]);} Free (t);}  int main () {int t; scanf ("%d", &t), while (t--) {root = new Trie (); int n; scanf ("%d", &n); GetChar (); for (int i = 0; i < n; ++i) {char str[100]; gets (str); insert (str);} BFS (), gets (des), int ans = query (des);p rintf ("%d\n", ans);d El (root);} return 0;}

with June

HDU 2222 Keywords Search ac automaton template problem

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.