1.HDU 2222 Keywords Search
求目标串中出现了几个模式串。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<stack> 6 #include<queue> 7 #include<cstring> 8 #define PAU putchar(' ') 9 #define ENT putchar(' ') 10 #define MSE(a,b) memset(a,b,sizeof(a)) 11 #define REN(x) for(ted*e=fch[x];e;e=e->nxt) 12 #define REP(i,s,t) for(int i=s,__=t;i<=__;i++) 13 #define DWN(i,s,t) for(int i=s,__=t;i>=__;i--) 14 using namespace std; 15 const int maxn=1000000+10,inf=-1u>>1,sig=26; 16 struct node{node*tx[sig],*fail;int cnt;}ac[maxn],*nodecnt=ac,*root; 17 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]);} 18 void initac(){ 19 for(node*x=ac;x!=nodecnt;x++){ 20 MSE(x->tx,NULL);x->fail=NULL;x->cnt=0; 21 }nodecnt=ac;root=nodecnt++;return; 22 } 23 void insert(char*s){ 24 node*x=root;for(int i=0;s[i];i++){int c=s[i]-'a'; 25 if(!x->tx[c])x->tx[c]=nodecnt++;x=x->tx[c]; 26 }x->cnt++;return; 27 } 28 void getfail(){ 29 queue<node*>Q;REP(c,0,sig-1)if(root->tx[c])Q.push(root->tx[c]); 30 while(!Q.empty()){node*x=Q.front();Q.pop(); 31 REP(c,0,sig-1)if(x->tx[c])x->tx[c]->fail=tran(x->fail,c),Q.push(x->tx[c]); 32 }return; 33 } 34 int find(char*s){ 35 node*x=root;int ans=0;for(int i=0;s[i];i++){int c=s[i]-'a'; 36 x=tran(x,c);ans+=x->cnt;x->cnt=0;node*p=x->fail;while(p)ans+=p->cnt,p->cnt=0,p=p->fail; 37 }return ans; 38 } 39 inline int read(){ 40 int x=0;bool sig=true;char ch=getchar(); 41 for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false; 42 for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';return sig?x:-x; 43 } 44 inline void write(int x){ 45 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 46 int len=0;static int buf[20];while(x)buf[len++]=x%10,x/=10; 47 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 48 } 49 char s[maxn];int n,T; 50 int main(){ 51 T=read();while(T--){ 52 initac();n=read();REP(i,1,n)scanf("%s",s),insert(s); 53 getfail();scanf("%s",s);write(find(s));ENT; 54 } 55 return 0; 56 }
2.hiho 1036 Trie图
是否含有模式串
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<stack> 6 #include<queue> 7 #include<cstring> 8 #define PAU putchar(' ') 9 #define ENT putchar(' ') 10 #define MSE(a,b) memset(a,b,sizeof(a)) 11 #define REN(x) for(ted*e=fch[x];e;e=e->nxt) 12 #define REP(i,s,t) for(int i=s,__=t;i<=__;i++) 13 #define DWN(i,s,t) for(int i=s,__=t;i>=__;i--) 14 using namespace std; 15 const int maxn=1000000+10,inf=-1u>>1,sig=26; 16 struct node{node*tx[sig],*fail;int cnt;}ac[maxn],*nodecnt=ac,*root; 17 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);} 18 void initac(){ 19 for(node*x=ac;x!=nodecnt;x++){ 20 MSE(x->tx,NULL);x->fail=NULL;x->cnt=0; 21 }nodecnt=ac;root=nodecnt++;return; 22 } 23 void insert(char*s){ 24 node*x=root;for(int i=0;s[i];i++){int c=s[i]-'a'; 25 if(!x->tx[c])x->tx[c]=nodecnt++;x=x->tx[c]; 26 }x->cnt++;return; 27 } 28 void getfail(){ 29 queue<node*>Q;REP(c,0,sig-1)if(root->tx[c])Q.push(root->tx[c]); 30 while(!Q.empty()){node*x=Q.front();Q.pop(); 31 REP(c,0,sig-1)if(x->tx[c]){ 32 node*p=tran(x->fail,c);x->tx[c]->fail=get(p->tx[c]); 33 if(p->tx[c]&&p->tx[c]->cnt)x->tx[c]->cnt+=p->tx[c]->cnt;Q.push(x->tx[c]); 34 } 35 }return; 36 } 37 bool find(char*s){ 38 node*x=root;int ans=0;for(int i=0;s[i];i++){int c=s[i]-'a'; 39 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;} 40 }return false; 41 } 42 inline int read(){ 43 int x=0;bool sig=true;char ch=getchar(); 44 for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false; 45 for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';return sig?x:-x; 46 } 47 inline void write(int x){ 48 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 49 int len=0;static int buf[20];while(x)buf[len++]=x%10,x/=10; 50 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 51 } 52 char s[maxn];int n,T; 53 int main(){ 54 initac();n=read();REP(i,1,n)scanf("%s",s),insert(s); 55 getfail();scanf("%s",s);puts(find(s)?"YES":"NO"); 56 return 0; 57 }