[HDU2222] Question A is the most correct choice when it is boring to reach the template question 0.6%, because you will be bored.

Source: Internet
Author: User
# Include <iostream> # include <cstring> # include <cstdio> # include <cstdlib> # include <queue> using namespace std;/** Reference: http://www.cnblogs.com/procedure2012/archive/2012/01/29/2331460.html Thanks: the templates and handouts provided by the lpp senior Pre-Knowledge: http://blog.csdn.net/c0de4fun/article/details/7879597 Tought: Nothing to say is the AC automatic machine ah, Pre-knowledge to the array implementation, dynamic Allocation is implemented here. This mainly refers to the template Query function. */Struct node {node * fail; int count; node * next [26]; node () {fail = NULL; memset (next, 0, sizeof (next )); count = 0 ;}}; const int MAX_SIZE = 1000010; const int MAX_WORD = 62; char dest [MAX_SIZE]; char word [MAX_WORD]; queue <node *> q; void AC_ins (char * word, node * root) {int len = strlen (word); node * tmp = root; for (int I = 0; I <len; I ++) {int k = word [I]-'A'; if (! Tmp-> next [k]) tmp-> next [k] = new node (); tmp = tmp-> next [k];} tmp-> count ++ ;} void AC_build (node * root) {root-> fail = NULL; q. push (root); while (! Q. empty () {node * tmp = q. front (); node * p = NULL; q. pop (); for (int I = 0; I <26; I ++) {if (tmp-> next [I]) {if (tmp = root) {tmp-> next [I]-> fail = root;} else {p = tmp-> fail; while (p) {if (p-> next [I]) {tmp-> next [I]-> fail = p-> next [I]; break;} p = p-> fail;} if (! P) tmp-> next [I]-> fail = root;} q. push (tmp-> next [I]) ;}}} int AC_query (node * root, char * str) {int cnt = 0; node * p = root; int len = strlen (str); for (int I = 0; I <len; I ++) {int ind = str [I]-'A'; while (! P-> next [ind] & p! = Root) p = p-> fail; p = p-> next [ind]; if (p = NULL) p = root; node * tmp = p; while (tmp! = Root & tmp-> count! =-1) {cnt = cnt + tmp-> count; // instead of cnt ++? Let's try; tmp-> count =-1; tmp = tmp-> fail;} return cnt;} int main () {# ifndef ONLINE_JUDGE freopen ("B: \ acm \ SummerVacation \ String-I \ B. in "," r ", stdin); freopen (" B: \ acm \ SummerVacation \ String-I \ B. out "," w ", stdout); # endif int T, N; while (scanf (" % d ", & T )! = EOF) {while (T --) {node * root = new node (); scanf ("% d \ n", & N); for (int I = 0; I <N; I ++) {gets (word); AC_ins (word, root);} AC_build (root); gets (dest); int ans = AC_query (root, dest); printf ("% d \ n", ans) ;}# ifndef ONLINE_JUDGE fclose (stdin); fclose (stdout); # endif 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.