"luogup3808" "Template" ac Automaton (Simple version)

Source: Internet
Author: User

The topic background by the data and directly "hit the table" over the title, is cheating behavior, found that the brown name.

This is a simple template problem for AC automata.

Used to detect correctness and algorithm constants.

In order to prevent card OJ, only two sets of data are guaranteed on the correct basis, please do not commit maliciously.

Administrator tip: There are repeated words in the subject data, and repeated words should be counted many times, please note

Title Description

Given n pattern strings and a text string, how many pattern strings appear in the text string.

Input/output format Input Format:

The first row is an n, which indicates the number of pattern strings;

The following n rows a pattern string per line;

The following line is a text string.

output Format:

A number means an answer.

Input/Output sample Input Sample # #:Copy
2aaaaa
Sample # # of output:Copy
2
Description

Subtask1[50pts]:∑length (Pattern String) <=10^6,length (text string) <=10^6,n=1;

Subtask2[50pts]:∑length (Pattern String) <=10^6,length (text string) <=10^6;

Exercises

In the usual no one really listen to the training, Qwerta just finished a Win7 original minesweeper, comfortable raised his head, just on the teacher said fail tree eyes.

So I know how to throw a releasing push method.

We recommend that you first construct a small data-driven push, like what's on $her,him,he,his,she$ to match $shehisher$.

Online tutorials are pretty much, not drawing. (Lazy

Actually, it's not quite clear that I'm pushing.

And then the textbook Qaq Yyb's blog.

In short, AC self-motive is in the trie tree BFs and then disorderly, can be understood as trie on the KMP ba.

I don't know how to do that, I don't talk about it. Qaq

1#include <queue>2#include <cstdio>3#include <cstring>4#include <iostream>5 using namespacestd;6 #defineR Register7 Const intmaxl=1e6+3;8 CharS[MAXL];9 structemm{Ten     intfail; One     intnxt[ -]; A     inttag; -}AC[MAXL];//tree Structure -queue<int>q;//The queue used when get_fail. the intMain () - { -     //freopen ("a.in", "R", stdin); -Ios::sync_with_stdio (false); +Cin.tie (false); Cout.tie (false); -     intN; +Cin>>N; A     inttot=0; at     //trie -      while(n--) -     { -Cin>>s; -         intlen=strlen (s); -         intnow=0;//now indicates the current node number in          for(RintI=0; i<len;++i) -         { to             if(! ac[now].nxt[s[i]-'a'])//If the current node does not have this son +ac[now].nxt[s[i]-'a']=++tot;//He made a son . -now=ac[now].nxt[s[i]-'a'];//Now jump to this son. the         } *ac[now].tag++;//Now the last place is the end point of the pattern string, labeled $     }Panax Notoginseng     //Get_fail -     { the         //first, take care of the points connected to node No. 0. +          for(RintI=0;i< -;++i) A         { the             if(ac[0].nxt[i])//If this son exists , +             { -ac[ac[0].nxt[i]].fail=0;//point fail to node No. 0 (in fact, the default is 0 Ah Qaq $Q.push (ac[0].nxt[i]);//push this son in . $             }     -         } -          while(!q.empty ()) the         { -             intX=q.front (); Q.pop ();//Pick PointWuyi              for(RintI=0;i< -;++i) the             { -                 if(Ac[x].nxt[i])//If this son exists , Wu                 { -Ac[ac[x].nxt[i]].fail=ac[ac[x].fail].nxt[i];//knock the key!!!  About                     //This son's fail, equal to this node fail's son will push to understand BA Qaq $Q.push (Ac[x].nxt[i]);//Push in -                 } -                 Else -Ac[x].nxt[i]=ac[ac[x].fail].nxt[i];//here, the NXT directly points to the corresponding son of fail. A             } +         } the     } -     //Run $Cin>>s; the     Long Longans=0; the     intlen=strlen (s); the     intnow=0;//now indicates the current node number the      for(RintI=0; i<len;++i) -     { innow=ac[now].nxt[s[i]-'a'];//go down one level (because the empty NXT is pointed directly to the fail, so you don't need the usual while jump fail the //probably the difference between the trie and the trie tree. the          for(Rintt=now;t&&ac[t].tag!=-1; t=ac[t].fail)//starting from this point, violent jump fail, find a match About         { theans+=Ac[t].tag; theac[t].tag=-1;//It's a pruning . the         } +     } -cout<<ans; the     return 0;//Sprinkle FlowersBayi}

And then spit out the cuckoo data, before a large section of $now$ and $i$ mixed, the result is a point (???

"luogup3808" "Template" ac Automaton (Simple version)

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.