#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;
}
trie图
最新推荐文章于 2025-05-09 20:50:19 发布