标签:pre size push UI scan begin nbsp oid queue
#include <cstring> #include <vector> #include <cstdio> #include <queue> #include <map> #define N 20005 using namespace std; struct trie { map<int,trie*>next; trie * fail; int cnt,id; }*root; bool vis[N]; int ans[N],n,m,size; vector<int>name[N],Count[N]; trie * create() { trie * now=new trie; now->fail=NULL; now->next.clear(); now->id=++size; now->cnt=0; return now; } void ins(int len,int num) { int k; trie *now=root; for(int i=1;i<=len;++i) { scanf("%d",&k); if(!now->next[k]) now->next[k]=create(); now->id=++size now=now->next[k]; } now->cnt++; Count[now->next[k]].push_back(num); } void build() { queue<trie*>q; q.push(root); for(;!q.empty();) { trie * now=q.front(); q.pop(); trie * tmp=NULL; for(map<int,trie*>::iterator it=now->next.begin();it!=now->next.end();++it) { if(now==root) it->second->fail=root; else { tmp=now->fail; for(;tmp!=NULL;tmp=tmp->fail) { if(tmp->next[it->first]!=NULL) { it->second->fail=tmp->next[it->first]; break; } } if(tmp==NULL) it->second->fail=root; } q.push(it->second); } } } void query() { for(int i=1;i<=n;++i) { memset(vis,0,sizeof(vis)); trie * now=root; for(int i=0;i<name[i].size();++i) { if(!vis[now->id]) { vis[now->id]=true; cal(now,i); for (trie *pos=now->fail;pos!=NULL;pos=pos->Fail) if (!vis[pos->id]) { vis[pos->id] = true; cal(pos,i); } } } } } int main() { root=create(); scanf("%d%d",&n,&m); for(int x,y,i=1;i<=n;++i) { scanf("%d",&x); for(int j=1;j<=x;++j) scanf("%d",&y),name[x].push_back(y); name[x].push_back(-1); scanf("%d",&x); for(int j=1;j<=x;++j) scanf("%d",&y),name[x].push_back(y); name[x].push_back(-1); } for(int x,i=1;i<=m;++i) { scanf("%d",&x); ins(x,i); } build(); query(); return 0; }
标签:pre size push UI scan begin nbsp oid queue
原文地址:http://www.cnblogs.com/ruojisun/p/7544529.html