LA 4670 dominating Patterns (AC automaton)

Source: Internet
Author: User
Tags cmath

Test instructions: Given an article, and then there are some words that ask these words to appear several times in this article.

Analysis: This is an AC automaton of the bare board, and finally after the match to count the number of good.

The code is as follows:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #define DEBUG puts ("+++++")//#include <tr1/unordered_map> #define Freopenr  Freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "w", stdout) using namespace std;//using namespace std:: Tr1;typedef Long Long ll;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;cons T LL LNF = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 1e6 + 5;const LL mod = 214 7493647;const int N = 1e6 + 5;const int dr[] = {-1, 0, 1, 0, 1, 1,-1, -1};const int dc[] = {0, 1, 0,-1, 1,-1, 1, -1};co NST char *hex[] = {"0000", "0001", "0010", "0011", "0100","0101", "0110", "0111", "$", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};inline ll gcd (ll A, ll b) {return b = = 0? A:GCD (b, a%b); }inline int gcd (int a, int b) {return b = = 0? a:gcd (b, a%b);} inline int LCM (int a, int b) {return a * B/GCD (a, b);} int n, m;const int mon[] = {0, 31, 29, 31, 30, 31, 0, +, +,, +, +, +,, N, H, H, C, h, 31};inline int Min (int a, int b) {return a < b? A:b;} inline int Max (int a, int b) {return a > b a:b;} inline ll Min (ll A, ll b) {return a < b a:b;} inline ll Max (ll A, ll b) {return a > b a:b;} inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;}    const int sigma = 26;const int maxnode = 5;struct + aho{int cnt[155];    int Ch[maxnode][sigma];    int F[maxnode];    int Val[maxnode];    int Last[maxnode];    int sz;        void init () {sz = 1; memset (Ch[0], 0, sizeof Ch[0]);    memset (CNT, 0, sizeof CNT);    } int idx (char ch) {return ch-' a ';}            void print (int j) {if (j) {++cnt[val[j]];        Print (Last[j]);        }} void Insert (char *s, int v) {int u = 0;            while (*s) {int c = idx (*s);                if (!ch[u][c]) {memset (Ch[sz], 0, sizeof Ch[sz]);                Val[sz] = 0;            CH[U][C] = sz++;  } u = Ch[u][c];        ++s;    } Val[u] = V;        } void Find (char *s) {int j = 0;            while (*s) {int c = idx (*s);            while (J &&!ch[j][c]) j = f[j];            j = Ch[j][c];            if (Val[j]) print (j);            else if (Last[j]) print (last[j]);        ++s;        }} void Getfail () {queue<int> q;        F[0] = 0;            for (int c = 0; c < Sigma; ++c) {int u = ch[0][c];  if (u) {f[u] = 0;  Q.push (U); Last[u] = 0;     }} while (!q.empty ()) {       int r = Q.front ();            Q.pop ();                for (int c = 0;c < Sigma; ++c) {int u = ch[r][c];                if (!u) continue;                Q.push (U);                int v = f[r];                while (v &&!ch[v][c]) v = f[v];                F[u] = Ch[v][c]; Last[u] = Val[f[u]]?            F[u]: Last[f[u]; }        }    }};  Aho Aho;char S[155][75];char t[1000005];map<string, int> mp;int main () {while (scanf ("%d", &n) = = 1 &&        N) {aho.init ();        Mp.clear ();            for (int i = 1; I <= n; ++i) {scanf ("%s", S[i]);            Aho.insert (S[i], i);        Mp[s[i]] = i;        } scanf ("%s", T);        Aho.getfail ();        Aho.find (t);        int Mmax =-1;        for (int i = 1; I <= n; ++i) Mmax = Max (Mmax, aho.cnt[i]);        printf ("%d\n", Mmax);    for (int i = 1; I <= n; ++i) if (aho.cnt[mp[s[i]] = = Mmax) printf ("%s\n", S[i]); } return 0;}

LA 4670 dominating Patterns (AC automaton)

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.