[BZOJ2938] [POI2000] Virus (AC automaton +dfs)

Source: Internet
Author: User
Topics

Portal

This is a more exercise-thinking problem, let me understand the role of mismatch pointers more deeply
General AC automata are as many matches as possible, this question hope infinite long, that is, hope can mismatch on mismatch, we construct the trie tree, in the trie map (note now become a figure) on the search ring can;
Several optimizations: According to the nature of the trie tree. If the suffix of a point is a virus, then this point must not be selected
In DFS, create two bool arrays, one that needs backtracking--for finding loops on this path, and another that doesn't need backtracking--no more searching for code before searching.

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <
Queue> #include <string> using namespace std;
#define LL Long Long const int MAXN=2E6;

const int INF=1E9; void Debug (int *a,int len) {for (int i=1; i<=len; i++) printf ("%d", a[i]);
printf ("\ n");}
int n,m;
string S;
    struct tree{int fail;
    int vis[30];
int end;

}AC[MAXN];
int cnt=0;
    void Build_ac (string s) {int l=s.length (); int now=0; for (int i=0; i<l; i++) {if (!
        ac[now].vis[s[i]-' 0 ']) ac[now].vis[s[i]-' 0 ']=++cnt;
    now=ac[now].vis[s[i]-' 0 '];
} ac[now].end++;
} queue <int> Q;
    void Get_fail () {for (int i=0; i<=1; i++) if (Ac[0].vis[i]) Q.push (ac[0].vis[i]);
        while (!q.empty ()) {int Now=q.front (); Q.pop (); for (int i=0; i<=1; i++) {if (Ac[now].vis[i]) {Ac[ac[now].vis[i]].fai
L=ac[ac[now].fail].vis[i];                if (ac[ac[ac[now].vis[i]].fail].end) ac[ac[now].vis[i]].end++;//based on tire tree Nature optimization Q.push (A
            C[now].vis[i]);
        } else ac[now].vis[i]=ac[ac[now].fail].vis[i];
}}} bool VIS[MAXN],USED[MAXN];
    bool Dfs (int x) {vis[x]=true;
        for (int i=0; i<=1; i++) {int to=ac[x].vis[i];
        if (Vis[to]) return true; if (Used[to] | | |
    Ac[to].end) continue;//before searching or having a virus, no need to search for if (DFS) return true;
    } Vis[x]=false;
return false;
    } int main () {scanf ("%d", &n);
        for (int i=1; i<=n; i++) {cin>>s;
    Build_ac (s);

    } get_fail ();
    if (Dfs (0)) printf ("TAK");
    else printf ("NIE");
return 0; }
Summary

1. The idea of transformation
2, DFS in two bool array, in fact, to find a ring with Topsort can also
3. Speed up the problem

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.