trie图

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

#define INF 2000000000
#define max(a,b) (a)>(b)?(a):(b)
#define read(a) scanf("%d",&a);
#define reads(s) scanf("%s",s);
#define ouput(a) printf("%d\n",a);
#define outs(s) printf("%s\n",s);
#define Forl(i,s,e) for(int i=s;i<e;i++)
#define Fore(i,s,e) for(int i=s;i<=e;i++)
#define debug outs("here")
#define typec int //type of value
using namespace std;


const int maxn = 1000004;
const int na = -1;

int res;

struct trie{
    struct trie *fail;
    struct trie *next[26];
    int is_word;
    trie(){
        fail = NULL;
        is_word = 0;
        Forl(i,0,26)next[i]=NULL;
    }
};

void build(char *s,trie * root)
{
    int i=0,id;
    trie *p = root;
    while(s[i]){
        id = s[i] - 'a';
        if(p->next[id]==NULL){
            p->next[id] = new trie();
        }
        p=p->next[id];
        i++;
    }
    p->is_word++;
}

void bfs(trie *root)
{
    queue<trie *> que;
    trie *p = root;
    que.push(p);
    while(!que.empty()){
        p = que.front();que.pop();
        Forl(i,0,26){
            if(p->next[i]!=NULL){
                if(p==root)p->next[i]->fail = root;
                else{
                    trie *temp = p;
                    while(temp->fail!=NULL){
                        if(temp->fail->next[i]!=NULL){
                            p->next[i]->fail = temp->fail->next[i];
                            break;
                        }
                        temp = temp->fail;
                    }
                    if(temp->fail==NULL){
                        p->next[i]->fail=root;
                    }
                }
                que.push(p->next[i]);
            }
        }
    }
}

bool search_(char *s,trie *root)
{
    int i=0,id;
    trie *p = root;
    while(s[i]){
        id = s[i++] - 'a';
        while(p!=root&&p->next[id]==NULL){
            p=p->fail;
        }
        p=p->next[id];
        if(p==NULL)p=root;
        trie *temp = p;
        if(temp!=root&&temp->is_word){
            return true;
            res+=temp->is_word;
            temp->is_word = 0;
            temp = temp->fail;
        }
    }
    return false;
}
void del(trie *t)
{
    Forl(i,0,26){
        if(t->next[i]!=NULL)del(t->next[i]);
    }
    delete t;
}
char word[maxn],pa[maxn];
int main()
{
    int n,t;
    while(scanf("%d",&n)==1){
        trie *root = new trie();
        Forl(i,0,n){
            scanf("%s",word);
            build(word,root);
        }
        bfs(root);
        scanf("%s",pa);
        res = 0;
        if(search_(pa,root))outs("YES")
        else outs("NO")
        del(root);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值