hdu 3065

hdu很坑啊,题目没有说多组数据WA死了!!

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
const int kind=26;
using namespace std;
char b[1001][51],a[2000001];    //a模式串,b为匹配串
int mark[1001];
struct trie{
	struct trie * fail,*next[kind];
	int count;           //count表示是第几个模式串
	trie(){
		fail=NULL;
		count=0;
		memset(next,NULL,sizeof(next));
	}
};
struct trie * root;
void insert(char* str,int count){
	int len=strlen(str),i,tem;
	struct trie *p,*q;
	p=root;
	for(i=0;i<len;i++){
		tem=str[i]-'A';
		if(p->next[tem]==NULL){
			q=(struct trie*)calloc(1,sizeof(struct trie));
			q->count=0;
			q->fail=NULL;
			memset(q->next,NULL,sizeof(q->next));
			p->next[tem]=q;
		}
		p=p->next[tem];
	}
	p->count=count;
}
void buildac(){
	int i;
	struct trie *p,*tem;
	queue<struct trie *>q;
	root->fail=NULL;
	q.push(root);
	while(!q.empty()){
		tem=q.front();
		q.pop();
		for(i=0;i<kind;i++){
			if(tem->next[i]!=NULL){
				if(tem==root)
					tem->next[i]->fail=root;
				else{
					p=tem->fail;
					while(p!=NULL){
						if(p->next[i]!=NULL){
							tem->next[i]->fail=p->next[i];
							break;
						}
						p=p->fail;
					}
					if(p==NULL)
						tem->next[i]->fail=root;
				}
				q.push(tem->next[i]);
			}
		}
	}
}
bool query(char *str){
	int len=strlen(str),i,tem,result=0;
	struct trie* tmp=root,*p;
	bool flag=0;
	for(i=0;i<len;i++){
		if(str[i]<'A' || str[i]>'Z'){
			tmp=root;
			continue;
		}
		tem=str[i]-'A';
		while(tmp->next[tem]==NULL && tmp!=root)
			tmp=tmp->fail;
		tmp=tmp->next[tem];
		if(tmp==NULL)
			tmp=root;
		p=tmp;
		while(p!=root){            //每到一个位置就加上p->count,以及以他的后缀为前缀的单词
			if(p->count!=0){
				mark[p->count]+=1;
				flag=1;
			}
			p=p->fail;
		}
	}
	return flag;
}
int main(){
	int t,T,n,i,j,total=0;
	while(scanf("%d",&n)!=EOF){
		root=(struct trie*)calloc(1,sizeof(struct trie));
		root->fail=NULL;
		root->count=0;
		memset(root->next,NULL,sizeof(root->next));
		for(i=1;i<=n;i++){
			scanf("%s",b[i]);
			insert(b[i],i);
		}
		buildac();
		scanf("%s",a);
		memset(mark,0,sizeof(mark));
		if(query(a)){
			for(i=1;i<=n;i++){
				if(mark[i])
					printf("%s: %d\n",b[i],mark[i]);
			}
		}
	}
}


 

你可能感兴趣的:(hdu 3065)