[AC自動機]HDOJ3695 Computer Virus on Planet Pandora

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

題意:給t、n,t個案例,n個字串

  下面給n+1個字串,n個互不相同的小串,最後一個是模式串

  模式串會出現[qx]的形式,q為數字,x為一個字母

問n個小串在模式串中出現的個數,正著出現、反著出現都算。

 

蠻裸的ac自動機,本來想著在query裡面把找到過的end清零,把模式串展開正著反著找兩遍,那即使再找到加零也不影響。但是逾時了。。。

於是把找到過的end標為-1,-1了就不再找下去,就可以了~上代碼

 

#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <cctype>#include <cmath>#include <string>#include <sstream>#include <iostream>#include <algorithm>#include <iomanip>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>typedef long long LL;typedef long double LD;const double eps=1e-8;#define pi acos(-1.0)#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1typedef pair<int, int> PI;typedef pair<int, PI> PP;#ifdef _WIN32#define LLD "%I64d"#else#define LLD "%lld"#endif//#pragma comment(linker, "/STACK:1024000000,1024000000")//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}//inline int read(){char ch=‘ ‘;int ans=0;while(ch<‘0‘ || ch>‘9‘)ch=getchar();while(ch<=‘9‘ && ch>=‘0‘){ans=ans*10+ch-‘0‘;ch=getchar();}return ans;}//inline void print(LL x){printf(LLD, x);puts("");}//inline void read(double &x){char c = getchar();while(c < ‘0‘) c = getchar();x = c - ‘0‘; c = getchar();while(c >= ‘0‘){x = x * 10 + (c - ‘0‘); c = getchar();}}//inline void sc(LL &x){scanf(LLD, &x);}struct Trie{    int next[5000100][26], fail[5000100], end[5000100];    int root, L;    int newnode()    {        for(int i=0;i<26;i++)            next[L][i]=-1;        end[L++]=0;        return L-1;    }    void init()    {        L=0;        root=newnode();    }    void insert(char buf[])    {        int len=strlen(buf);        int now=root;        for(int i=0;i<len;i++)        {            if(next[now][buf[i]-‘A‘]==-1)                next[now][buf[i]-‘A‘]=newnode();            now=next[now][buf[i]-‘A‘];        }        end[now]++;    }    void build()    {        queue<int> Q;        fail[root]=root;        for(int i=0;i<26;i++)        if(next[root][i]==-1)            next[root][i]=root;        else        {            fail[next[root][i]]=root;            Q.push(next[root][i]);        }        while(!Q.empty())        {            int now=Q.front();            Q.pop();            for(int i=0;i<26;i++)                if(next[now][i]==-1)                    next[now][i]=next[fail[now]][i];                else                {                    fail[next[now][i]]=next[fail[now]][i];                    Q.push(next[now][i]);                }        }    }    int query(char buf[])    {        int len=strlen(buf);        int now=root;        int res=0;        for(int i=0;i<len;i++)        {            now=next[now][buf[i]-‘A‘];            int tmp=now;            while(tmp!=root && end[tmp]!=-1)            {                res+=end[tmp];         //printf("%d %d\n", tmp, end[tmp]);                end[tmp]=-1;                tmp=fail[tmp];            }        }        return res;    }}ac;char buf[5200010], tmp[5200010];int main(){#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);    freopen("out.txt", "w", stdout);#endif    int T;    scanf("%d", &T);    while(T--)    {        int n;        scanf("%d", &n);        ac.init();        for(int i=0;i<n;i++)        {            scanf("%s", buf);            ac.insert(buf);//            int LEN=strlen(buf);//            reverse(buf, buf+LEN);//            ac.insert(buf);        }        ac.build();        scanf("%s", tmp);        int LEN=strlen(tmp), d=0;        for(int i=0;i<LEN;i++)            if(tmp[i]==‘[‘)            {                int cur=i+1, num=0;                while(isdigit(tmp[cur]))                    num=num*10+tmp[cur++]-‘0‘;                fill(buf+d, buf+d+num, tmp[cur]);                i=cur+1, d+=num;            }            else                buf[d++]=tmp[i];        buf[d]=‘\0‘;//        puts(buf);//        for(int i=0;i<ac.L;i++)//            printf("%d %d\n", i, ac.end[i]);        int ans=ac.query(buf);        reverse(buf, buf+d);        printf("%d\n", ans+ac.query(buf));    }    return 0;}
HDOJ 3695

 

[AC自動機]HDOJ3695 Computer Virus on Planet Pandora

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.