AC automata Template (array + pointer) Hdu2222__ac automatic machine

Source: Internet
Author: User
Tags cmath

Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.a.q
Hand in Hand
Online acmers
Forum | Discuss
Statistical charts
Problem Archive
Realtime Judge Status
Authors ranklist
C/c++/java Exams
ACM Steps
Go to Job
Contest Livecast
Icpc@china
Best Coder Beta
VIP | STD Contests
Virtual Contests
DIY | Web-diy Beta
Recent contests
Lee
Mail 0 (0)
Control Panel
Sign out
Keywords Search Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 34852 accepted Submission (s): 11219


Problem Description in the modern time, Search engine came to 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 the users type some keywords to find the image, the system would match the keywords W ITH description of the image which the most keywords be matched.
To simplify the problem, giving your a description of image, and some keywords, you should tell me how many keywords'll b E match.

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

Output Print How many keywords are contained in the description.
Sample Input
1 5 She he say shr her yasherhs
Sample Output
3


I finally moved to AC automaton today.

The original look at the time to remember a bit laborious, have forgotten almost. Today I found some great God's blog, finally understood

This question is basically a template problem, the last array is reference Liu Rugia Training guide, plus this is not faster than one times, he said along the mismatch pointer to the first word node encountered the number

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector > #include <cmath> #include <queue> #include <stack> #include <map> #include <set>
Include<algorithm> using namespace std;
const int maxn=1000010;
const int maxm=50*10010;
const int sigma_size=26;
int n;

Char T[60],S[MAXN];
    struct AC {int ch[maxm][26];
    int VAL[MAXM];
    int FAIL[MAXM],LAST[MAXM];
    int sz;
    void Clear () {memset (ch[0],0,sizeof (ch[0])); sz=1;}
    int idx (char x) {return x ' a ';}
        void Insert (char *s) {int u=0;
        int N=strlen (s);
            for (int i=0;i<n;i++) {int c=idx (s[i]);
                if (!ch[u][c]) {memset (ch[sz],0,sizeof (Ch[sz)));
                val[sz]=0;
            ch[u][c]=sz++;
        } U=ch[u][c];
    } val[u]++;
        } void Getfail () {queue<int> q;
       fail[0]=0; int u=0;
            for (int i=0;i<sigma_size;i++) {u=ch[0][i];
        if (u) {q.push (U); fail[u]=0;last[u]=0}
            while (!q.empty ()) {int R=q.front (); Q.pop ();
                for (int i=0;i<sigma_size;i++) {u=ch[r][i];
                if (!u) {ch[r][i]=ch[fail[r]][i];continue;}
                Q.push (U);
                int V=fail[r];
                while (V&&!ch[v][i]) v=fail[v];
                Fail[u]=ch[v][i];
            Last[u]=val[fail[u]]?fail[u]:last[fail[u]];
        int find (char *s) {int u=0,cnt=0;
        int N=strlen (s);
            for (int i=0;i<n;i++) {int c=idx (s[i]);
            U=CH[U][C];
            The int temp=0;//must have an initial value of 0, which means that while the following two judgments are not valid, while the IF (Val[u]) Temp=u can be performed normally;
            else if (Last[u]) temp=last[u]; while (temp) {Cnt+=val[Temp];
                val[temp]=0;
            TEMP=LAST[TEMP];
    } return CNT;
}}tree;
    int main () {int T;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        Tree.clear ();
            for (int i=1;i<=n;i++) {scanf ("%s", T);
        Tree.insert (t);
        } tree.getfail ();
        scanf ("%s", s);
        int Ans=tree.find (s);
    printf ("%d\n", ans);
return 0;
 }


Pointer version:

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector > #include <cmath> #include <queue> #include <stack> #include <map> #include <set>
Include<algorithm> using namespace std;
const int maxn=1000010;
const int maxm=50*10010;
const int sigma_size=26;
int n;
Char T[60],S[MAXN];
    struct Node {int val;
    Node *next[26];
    Node *fail;
    Node *last;
Node () {memset (next,null,sizeof (next)); fail=null;last=null;val=0;};
    struct AC {node *root;
    void Clear () {root=new node ();}
    int idx (char x) {return x ' a ';}
        void Insert (char *s) {int N=strlen (s);
        Node *p=root;
            for (int i=0;i<n;i++) {int c=idx (s[i]);
            if (!p->next[c]) p->next[c]=new node ();
        p=p->next[c];
    } p->val++;
        } void Getfail () {queue<node *> q;
        Node *p=null; Forint c=0;c<sigma_size;c++) {p=root->next[c];
                if (p!=null) {p->fail=p->last=root;
            Q.push (P);
            } while (!q.empty ()) {node *r=q.front (); Q.pop ();
                for (int c=0;c<sigma_size;c++) {p=r->next[c];
                if (p==null) {r->next[c]=r->fail->next[c];continue;}
                Node *v=r->fail;
                while (v&&v->next[c]==null) {V=v->fail}
                if (v==null) p->fail=root;//add Judge else p->fail=v->next[c];
                if (p->fail->val) p->last=p->fail;
                else p->last=p->fail->last;
            Q.push (P);
        "}" int find (char *s) {int N=strlen (s);
        Node *p=root;
        int cnt=0;
            for (int i=0;i<n;i++) {int c=idx (s[i]); p=p->next[c];
            if (!p) p=root;//here must be judged, go along the mismatch edge to root, indicating that there is no such a match, to start from the root node *temp=null;
            if (p->val) temp=p;
            else temp=p->last;
                while (temp!=root&&temp!=null) {cnt+=temp->val;
                temp->val=0;
            temp=temp->last;
    } return CNT;
}}tree;
    int main () {int T;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        Tree.clear ();
            for (int i=1;i<=n;i++) {scanf ("%s", T);
        Tree.insert (t);
        } tree.getfail ();
        scanf ("%s", s);
        int Ans=tree.find (s);
    printf ("%d\n", ans);
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.