【统计难题】 hdu1251

看了这个题目,统计难题,但一读题,原来是个关于Trie字典树的运用,完全是根据Trie算法,是一个模版题。


#include <iostream>
#include <string.h>
using namespace std;

const  int sonsum=26,base='a';
char s1[12],ss[12];

struct Trie
{
	int num;
	bool flag;
	struct Trie *son[sonsum];
	Trie()
	{
		num=1;flag=false;
		memset(son,NULL,sizeof(son));
	}
};

Trie *NewTrie()
{
	Trie *temp=new Trie;
	return temp;
}

void Inset(Trie *root,char *s)
{
	Trie *temp=root;
	while(*s)
	{
		if(temp->son[*s-base]==NULL)
		{
			temp->son[*s-base]=NewTrie();
		}
		else
		    temp->son[*s-base]->num++;
        temp=temp->son[*s-base];
        s++;
	}
	temp->flag=true;
}

int search(Trie *root,char *s)
{
	Trie *temp=root;
	while(*s)
	{
		if(temp->son[*s-base]==NULL) return 0;
		temp=temp->son[*s-base];
		s++;		
	}
	return temp->num;
}


int main()
{
	Trie *root=NewTrie();
	root->num=0;
	//while(cin.get(s1,12))
	while(gets(s1)&&strcmp(s1,"")!=0)  
	{
		//if(strcmp(s1," ")==0)
		//break;
		Inset(root,s1);
	}
	while(cin>>ss)
	{
		int  ans=search(root,ss);
		cout<<ans<<endl;
	}
	
	
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值