Zoj 3430 detect the virus (AC automation)

Source: Internet
Author: User

Question:

The following code is used to encode N 64-base viruses.

Then, we will provide M pattern strings for you to decomcode and find out how many virus sequences are contained in them.


Train of Thought Analysis:

A very bare AC automatic machine. But all kinds of nausea.

Dynamic trie static open queue will re.

MLE will be implemented when all the dynamic development is complete.

After various positions, you can open trie statically and enable the queue dynamically to access the AC.


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <queue>#define maxn 50005using namespace std;const char tab = 0;const int max_next = 256;int tlen=0;int next[maxn][max_next],fail[maxn],num[maxn],mark[maxn],siz;int rev[300];int newnode(){    for(int i=0;i<max_next;i++)        next[siz][i]=0;    fail[siz]=num[siz]=mark[siz]=0;    return siz++;}void Insert(int *s,int len){    int p=0;    for(int i=0;i<len;i++)    {        int &x=next[p][s[i]];        p=x?x:x=newnode();    }    num[p]++;}void acbuild(){    queue<int>Q;    Q.push(0);    while(!Q.empty())    {        int temp=Q.front();        Q.pop();        for(int i=0;i<max_next;i++)        {            int v=next[temp][i];            if(v==0)next[temp][i]=next[fail[temp]][i];            else Q.push(v);            if(temp!=0)fail[v]=next[fail[temp]][i];        }    }}int query(int *s,int len,int id){    int cnt=0;    for(int i=0,p=0;i<len;i++)    {        p=next[p][s[i]];        for(int f=p;f&&mark[f]!=id;f=fail[f]){            mark[f]=id;            if(num[f]>0)cnt++;        }    }    return cnt;}int word[10005];void ReEncode(char *a){    tlen=0;    for(int i=0,len=0,x=0;a[i] && a[i] != '=';++i){        len+=6,x=(x<<6)|rev[a[i]];        if(len>=8){            word[tlen++]=(x>>(len-8))&0xff;            len-=8;        }    }}char tmp[10005];int main(){    for(int i=0;i<26;i++)        rev[i+'A']=i;    for(int i=26;i<52;i++)        rev[i+'a'-26]=i;    for(int i=52;i<62;i++)        rev[i+'0'-52]=i;    rev['+']=62;    rev['/']=63;    int n;    while(scanf("%d",&n)!=EOF)    {        siz=0;        newnode();        for(int i=1;i<=n;i++)        {            scanf("%s",tmp);            ReEncode(tmp);            Insert(word,tlen);        }        acbuild();        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%s",tmp);            ReEncode(tmp);            printf("%d\n",query(word,tlen,i));        }        puts("");    }    return 0;}


Zoj 3430 detect the virus (AC automation)

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.