1.HDU 2222 Keywords Search
求目标串中出现了几个模式串。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define MSE(a,b) memset(a,b,sizeof(a))
#define REN(x) for(ted*e=fch[x];e;e=e->nxt)
#define REP(i,s,t) for(int i=s,__=t;i<=__;i++)
#define DWN(i,s,t) for(int i=s,__=t;i>=__;i--)
using namespace std;
const int maxn=+,inf=-1u>>,sig=;
struct node{node*tx[sig],*fail;int cnt;}ac[maxn],*nodecnt=ac,*root;
node*get(node*&x){return x?x:root;}node*tran(node*x,int&c){while(x&&!x->tx[c])x=x->fail;return get(get(x)->tx[c]);}
void initac(){
for(node*x=ac;x!=nodecnt;x++){
MSE(x->tx,NULL);x->fail=NULL;x->cnt=;
}nodecnt=ac;root=nodecnt++;return;
}
void insert(char*s){
node*x=root;for(int i=;s[i];i++){int c=s[i]-'a';
if(!x->tx[c])x->tx[c]=nodecnt++;x=x->tx[c];
}x->cnt++;return;
}
void getfail(){
queue<node*>Q;REP(c,,sig-)if(root->tx[c])Q.push(root->tx[c]);
while(!Q.empty()){node*x=Q.front();Q.pop();
REP(c,,sig-)if(x->tx[c])x->tx[c]->fail=tran(x->fail,c),Q.push(x->tx[c]);
}return;
}
int find(char*s){
node*x=root;int ans=;for(int i=;s[i];i++){int c=s[i]-'a';
x=tran(x,c);ans+=x->cnt;x->cnt=;node*p=x->fail;while(p)ans+=p->cnt,p->cnt=,p=p->fail;
}return ans;
}
inline int read(){
int x=;bool sig=true;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false;
for(;isdigit(ch);ch=getchar())x=*x+ch-'';return sig?x:-x;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=;static int buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
char s[maxn];int n,T;
int main(){
T=read();while(T--){
initac();n=read();REP(i,,n)scanf("%s",s),insert(s);
getfail();scanf("%s",s);write(find(s));ENT;
}
return ;
}
2.hiho 1036 Trie图
是否含有模式串
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define MSE(a,b) memset(a,b,sizeof(a))
#define REN(x) for(ted*e=fch[x];e;e=e->nxt)
#define REP(i,s,t) for(int i=s,__=t;i<=__;i++)
#define DWN(i,s,t) for(int i=s,__=t;i>=__;i--)
using namespace std;
const int maxn=+,inf=-1u>>,sig=;
struct node{node*tx[sig],*fail;int cnt;}ac[maxn],*nodecnt=ac,*root;
node*get(node*&x){return x?x:root;}node*tran(node*x,int&c){while(x&&!x->tx[c])x=x->fail;return get(x);}
void initac(){
for(node*x=ac;x!=nodecnt;x++){
MSE(x->tx,NULL);x->fail=NULL;x->cnt=;
}nodecnt=ac;root=nodecnt++;return;
}
void insert(char*s){
node*x=root;for(int i=;s[i];i++){int c=s[i]-'a';
if(!x->tx[c])x->tx[c]=nodecnt++;x=x->tx[c];
}x->cnt++;return;
}
void getfail(){
queue<node*>Q;REP(c,,sig-)if(root->tx[c])Q.push(root->tx[c]);
while(!Q.empty()){node*x=Q.front();Q.pop();
REP(c,,sig-)if(x->tx[c]){
node*p=tran(x->fail,c);x->tx[c]->fail=get(p->tx[c]);
if(p->tx[c]&&p->tx[c]->cnt)x->tx[c]->cnt+=p->tx[c]->cnt;Q.push(x->tx[c]);
}
}return;
}
bool find(char*s){
node*x=root;int ans=;for(int i=;s[i];i++){int c=s[i]-'a';
x=get(tran(x,c)->tx[c]);if(x->cnt)return true;//node*p=x->fail;while(p){if(p->cnt)return true;p=p->fail;}
}return false;
}
inline int read(){
int x=;bool sig=true;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false;
for(;isdigit(ch);ch=getchar())x=*x+ch-'';return sig?x:-x;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=;static int buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
char s[maxn];int n,T;
int main(){
initac();n=read();REP(i,,n)scanf("%s",s),insert(s);
getfail();scanf("%s",s);puts(find(s)?"YES":"NO");
return ;
}