ZOJ 3228Searching the String ac automaton does not repeat the match

Source: Internet
Author: User

This method of judging really didn't think ...

For the match m in S, if the previous match position of M is the match position of the pre with this time now satisfies Now-pre >= m.length, then add 1.

The judgment was too 233 .

#include <iostream> #include <time.h> #include <stdio.h> #include <string.h> #include < stdlib.h> #include <string> #include <map> #include <vector> #include <algorithm> #include <queue> #include <cmath> #define LL long long#define ULL unsigned long longusing namespace Std;const int maxs =    26,MAXN = 600010;const int matn = 70;struct mat{int row,col;    ULL Mat[matn][matn];        void Init (int r,int c,int val) {row = R,col = C; for (int i = 1;i <= row, ++i) for (int j = 1;j <= col; ++j) mat[i][j] = (i = = J val:    0);        } Mat Multi (Mat c,ll MOD) {mat tmp; Tmp.        Init (this->row,c.col,0);       int i,j,k; for (k = 1;k <= this->col; ++k) for (i = 1;i <= tmp.row; ++i) for (j = 1;j <= tmp.col;        ++J) Tmp.mat[i][j] + = (this->mat[i][k]*c.mat[k][j]);    return TMP; } MAT Quick (LL n,ll MOD) {Mat res,tmp = *this; Res.        Init (row,col,1); while (n) {if (n&1) res = res.            Multi (TMP,MOD); TMP = tmp.            Multi (TMP,MOD);        n >>= 1;    } return res;        } void Output () {cout<< "****************" <<endl;        int i,j;             for (i = 1;i <= row; ++i) {for (j = 1;j <= col; ++j) printf ("%3lld", Mat[i][j]);        Puts ("");    } cout<< "&&&&&&&&&&&&&" <<endl;    }};struct trie{int NEXT[MAXS];    int fail; int flag;}    st[maxn];queue<int> q;int anw[100010];int pre[100010];struct query{int y;    Char S[8]; int Len;}    que[100100];struct acautomaton{int top,root;        int creat () {memset (st[top].next,-1,sizeof (St[top].next));        St[top].flag = 0;        St[top].fail =-1;    return top++; } void inIt () {Top = 0;    root = creat ();    } inline int Calindex (int c) {return C ' a ';        } void Insert (char *s,int ty) {int i = 0,TR = root,tmp;            while (s[i]! = ' + ') {tmp = Calindex (s[i]);            if (st[tr].next[tmp] = =-1) st[tr].next[tmp] = creat ();        tr = st[tr].next[tmp],++i;    } st[tr].flag = Ty;        } void Getfail () {st[root].fail =-1;        Q.push (root);        int f,t;            while (q.empty () = = False) {f = Q.front ();            Q.pop ();  for (int i = 0; i < Maxs; ++i) {if (st[f].next[i]! =-1) {T                    = St[f].fail;                    while (t! =-1 && st[t].next[i] = =-1) t = st[t].fail;                    if (t = =-1) st[st[f].next[i]].fail = root; else St[st[f].next[i]].fail = st[t]. Next[i];                Q.push (St[f].next[i]);        }}}} int Overlapmatch (char *s) {int I, tr = root,tmp;        int ans = 0;            for (i = 0; s[i]! = ' + '; ++i) {tmp = Calindex (s[i]);            while (tr! =-1 && st[tr].next[tmp] = =-1) tr = st[tr].fail;                if (tr = =-1) {tr = root;            Continue            } tr = st[tr].next[tmp];            TMP = TR; while (tmp! = root && St[tmp].flag! =-1) {if (St[tmp].flag) ans++,anw                [st[tmp].flag]++;            TMP = St[tmp].fail;    }} return ans;        } int Nooverlapmatch (char *s) {int I, tr = root,tmp;        int ans = 0;            for (i = 0; s[i]! = ' + '; ++i) {tmp = Calindex (s[i]); while (tr! =-1 && st[tr].next[tmp] = =-1) tr = st[tr].fail;           if (tr = =-1) {tr = root;            Continue            } tr = st[tr].next[tmp];            TMP = TR;                    while (tmp! = root && St[tmp].flag! =-1) {if (St[tmp].flag) {  if (I-pre[st[tmp].flag] >= que[st[tmp].flag].len) Ans++,anw[st[tmp].flag]++,pre[st[tmp].flag]                = i;            } tmp = St[tmp].fail;    }} return ans; }};char s[100010];map<string,int> m;map<string,int>::iterator it;int Main () {//Freopen ("Data1.in", "R",    STDIN);    int n,i;    Acautomaton AC;    int icase = 1;        while (scanf ("%s", s)! = EOF) {scanf ("%d", &n);        for (i = 1;i <= n; ++i) scanf ("%d%s", &AMP;QUE[I].Y,QUE[I].S);        for (i = 1;i <= n; ++i) Que[i].len = strlen (QUE[I].S);        memset (pre,-1,sizeof (pre));        memset (anw,0,sizeof (ANW)); AC.       Init (); for (i = 1;i <= n; ++i) {if (que[i].y = = 0) AC.        Insert (Que[i].s,i); } AC.        Getfail (); AC.        Overlapmatch (s);        M.clear ();                for (i = n;i >= 1;-I.) {if (que[i].y = = 0) {it = M.find (QUE[I].S);                if (it = M.end ()) anw[i] = anw[it->second];            Else M.insert (pair<string,int> (que[i].s,i)); }} AC.        Init (); for (i = 1;i <= n; ++i) {if (que[i].y = = 1) AC.        Insert (Que[i].s,i); } AC.        Getfail (); AC.        Nooverlapmatch (s);        M.clear ();                for (i = n;i >= 1;-I.) {if (que[i].y = = 1) {it = M.find (QUE[I].S);                if (it = M.end ()) anw[i] = anw[it->second];            Else M.insert (pair<string,int> (que[i].s,i));        }} printf ("Case%d\n", icase++);        for (i = 1;i <= n; ++i) printf ("%d\n", Anw[i]);    Puts (""); } return 0;}

ZOJ 3228Searching the String ac automaton does not repeat the match

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.