hdu 3065

本文讨论了在使用HDOJ平台解决编程问题时遇到的一个陷阱——题目未明确说明存在多组数据输入,导致WA(Wrong Answer)。文章详细介绍了使用树状数组(或线段树)进行多组数据处理的方法,并通过代码实现来解决此类问题。

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]);
			}
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值