hdu1251——统计难题

裸前缀树  只不过  指针和动态内存  我比较陌生  以前没用过啊QAQ

熟悉语法   水一日志  告诉自己  就算noip洗白了  也不要放弃


#include<cstdio>
#include<cstring>
#include<malloc.h>

#define MAXN (30)
#define cle(a,b) memset(a,b,sizeof(a))
#define loop(i,j,k,l) for(int i=j;l>0?i<=k:i>=k;i+=l)

struct trie {
	trie *next[MAXN];
	int v;
}*root;

char s[MAXN];int len;

int id;

void create() {
	trie *p=root,*q;
	
	loop(i,0,len,1) {
		id=s[i]-'a'+1;
		if(p->next[id]==NULL) {
			q=(trie*)malloc(sizeof(trie));
			loop(j,1,26,1) {
				q->next[j]=NULL;
			}
			q->v=1;	
			p->next[id]=q;
				
		}
		else{
			p->next[id]->v++;
		}
		p=p->next[id];			
		
			
	}
	
}

void readdata() {	
	gets(s);
	len=strlen(s)-1;
	while(s[0]!=0) {
		create();
		cle(s,0);
		gets(s);
		len=strlen(s)-1;
	}
}

int find() {
	trie *p=root;
	
	loop(i,0,len,1) {
		id=s[i]-'a'+1;
		if(p->next[id]==NULL){
			return 0;
		}
		p=p->next[id];
		if(i==len) {
			return p->v;
		}
	}
}

void solve() {
	while(scanf("%s",s)!=EOF) {
		len=strlen(s)-1;
		
		printf("%d\n",find());
		
		
		cle(s,0);
	}
}
int main() {
	root=(trie*)malloc(sizeof(trie));
	loop(i,1,26,1) {
		root->next[i]=NULL;
	}
	readdata();
	solve();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值