#include#include#include#include#include#include#include#include#include#include

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

fzu Problem 2005 Computer Virus on Planet Pa

系統 2062 0

http://acm.fzu.edu.cn/problem.php?pid=2005

AC自動機 需要優化

否則超時

代碼:

      #include<iostream>

#include<cmath>

#include<cstdio>

#include<string>

#include<cstring>

#include<vector>

#include<stack>

#include<queue>

#include<set>

#include<map>

#include<algorithm>



#define LL long long



using namespace std;



const int INF=0x3f3f3f3f;

const int N=5500005;

const int M=255000;

const int K=26;

struct nodeTrie

{

    int v;

    int fail;

    int next[K];

    void initialize()

    {

        v=0;

        fail=-1;

        memset(next,-1,sizeof(next));

    }

}trie[M];

int cnt,root;

char s1[N];

char s[1005];

char pro[N];

int getNewNode()

{

    ++cnt;

    trie[cnt].initialize();

    return cnt;

}

void addWord(int p,char *s,int k)

{

    if(s[0]=='\0') return ;

    for(int i=0;s[i]!='\0';++i)

    {

        if(trie[p].next[s[i]-'A']==-1)

        trie[p].next[s[i]-'A']=getNewNode();

        p=trie[p].next[s[i]-'A'];

    }

    (trie[p].v)=k;

}

void init(int n)

{

    cnt=-1;

    root=getNewNode();

    for(int i=1;i<=n;++i)

    {

        gets(s);

        addWord(root,s,i);

    }

}

void bfs(int p)

{

    trie[p].fail=root;

    queue<int>qt;

    qt.push(p);

    while(!qt.empty())

    {

        int y;

        int x=qt.front();qt.pop();

        for(int i=0;i<K;++i)

        if(trie[x].next[i]!=-1)

        {

            qt.push(trie[x].next[i]);

            if(x==root)

            {trie[trie[x].next[i]].fail=root;continue;}

            y=trie[x].fail;

            while(y!=root&&trie[y].next[i]==-1)

            y=trie[y].fail;

            if(trie[y].next[i]!=-1)

            trie[trie[x].next[i]].fail=trie[y].next[i];

            else

            trie[trie[x].next[i]].fail=root;

        }

    }

}

int match(int p,char *s,int n)

{

    int wordCount=0;

    int l=0;

    while(s[l]!='\0')

    {

        while(trie[p].next[s[l]-'A']==-1&&p!=root)

        p=trie[p].fail;

        if(trie[p].next[s[l]-'A']!=-1)

        p=trie[p].next[s[l]-'A'];

        ++l;

        int fp=p;

        while(fp!=root)

        {

            if(trie[fp].v==-1)

            break;

            if(trie[fp].v>0)

            {++wordCount;}

            trie[fp].v=-1;

            fp=trie[fp].fail;

        }

        if(wordCount==n)

        break;

    }

    return wordCount;

}

int main()

{

    //freopen("data.in","r",stdin);

    int T;

    scanf("%d",&T);

    while(T--)

    {

        int n;

        scanf("%d ",&n);

        init(n);

        bfs(root);

        gets(s1);

        int m=0;

        for(int i=0;s1[i]!='\0';++i)

        if(s1[i]=='[')

        {

            int sum=0;

            for(++i;s1[i]>='0'&&s1[i]<='9';++i)

            sum=sum*10+s1[i]-'0';

            while(sum--)

            pro[m++]=s1[i];

            ++i;

        }else

        {

            pro[m++]=s1[i];

        }

        pro[m]='\0';

        //puts(pro);

        int ans=match(root,pro,n);

        reverse(pro,pro+m);

        ans+=match(root,pro,n-ans);

        printf("%d\n",ans);

    }

    return 0;

}


    

fzu Problem 2005 Computer Virus on Planet Pandora


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 县级市| 林西县| 海阳市| 吕梁市| 怀仁县| 兴城市| 尖扎县| 宜良县| 兴化市| 昌黎县| 威远县| 建瓯市| 汶川县| 克什克腾旗| 四川省| 望江县| 武定县| 静海县| 上蔡县| 永胜县| 扎鲁特旗| 福州市| 冷水江市| 蕲春县| 云霄县| 齐河县| 栖霞市| 新昌县| 东乌珠穆沁旗| 道孚县| 乌恰县| 黎川县| 星子县| 扬州市| 南乐县| 山阴县| 临泽县| 观塘区| 鹤壁市| 玛沁县| 长丰县|