AC automaton LA 4670 dominating Patterns

Source: Internet
Author: User

Topic Portal

Test instructions: South of Training (P216)

Analysis: To find the most occurrences of the string, then each string map id,cnt record the maximum number of times can be.

#include <bits/stdc++.h>using namespace Std;const int N = + 5;const int NODE = N * 70;const int LEN = 1e6 + 5;co    NST int SIZE = 26;struct AC {int ch[node][size], Fail[node], Val[node], cnt[n], sz;    Map<string, int> MS;        void Clear (void) {memset (ch[0], 0, sizeof (ch[0]));        memset (CNT, 0, sizeof (CNT)); SZ = 1;        Val[0] = 0;    Ms.clear ();    } int idx (char c) {return C-' a ';        } void Insert (char *p, int id) {ms[string (P)] = ID;        int u = 0; for (int c, i=0; P[i];            ++i) {c = idx (P[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] = ID;        } void Build (void) {queue<int> que; fail[0] =-1;        int u;            for (int i=0; i<size; ++i) {u = ch[0][i];    if (u) {fail[u] = 0; QuE.push (U);   }} while (!que.empty ()) {u = Que.front ();            Que.pop ();                for (int i=0; i<size; ++i) {int &v = ch[u][i];                if (!v) {v = ch[fail[u]][i]; continue;                } que.push (v);            FAIL[V] = Ch[fail[u]][i];        }}} void query (char *t) {int u = 0, V; for (int c, i=0; T[i];            ++i) {c = idx (T[i]);   U = ch[u][c];            v = u;        cnt[val[v]]++;    }}}ac;char pattern[n][75], text[len];int n;void solve (void) {int best =-1;    for (int i=1, i<=n; ++i) {if (Ac.cnt[i] > Best) best = ac.cnt[i];    } printf ("%d\n", best); for (int i=1; i<=n; ++i) {if (ac.cnt[ac.ms[string (pattern[i])] = = Best) {printf ("%s\n", Patt        Ern[i]);     }}}int Main (void) {while (scanf ("%d", &n) = = 1) {if (!n) break;   Ac.clear ();            for (int i=1; i<=n; ++i) {scanf ("%s", &pattern[i]);        Ac.insert (Pattern[i], i);        } ac.build ();        scanf ("%s", &text);        Ac.query (text);    Solve (); } return 0;}

  

AC automaton LA 4670 dominating Patterns

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.