ZOJ2876 POJ3630 HDU1671 Phone List,静态Trie树

105 篇文章 0 订阅
60 篇文章 0 订阅

这题用Trie数来解决。如果用动态的话,malloc,new和delete这些肯定会超时,所以只能用静态数。判断条件优化一下,可以把时间将到100ms以内,ZOJ上70ms刷到第一页。。。记得静态数组开大一点,10000个号码每个号码10位,开100000最保险了。



/*******************************************************************************
 * Author : Neo Fung
 * Email : neosfung@gmail.com
 * Last modified : 2011-09-12 19:14
 * Filename : ZOJ2876 POJ3630 HDU1671 Phone List.cpp
 * Description : 
 * *****************************************************************************/
// ZOJ2876 POJ3630 HDU1671 Phone List.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"
// #define DEBUG


#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <memory.h>

#define KINDNUM 10  //字母种类,直接就
using namespace std;

int arrayIndex;

struct TrieNode//树的结点结构
{
	bool isNum;
	TrieNode *next[KINDNUM];//指向儿子结点,注意此处是 指针数组
}nodeArray[200000];

bool insert(TrieNode *&root,char *word)//向以root为根结点的树中插入串word
{
	TrieNode *location=root;
	int i=0,branch=0;
	if(location==NULL) 
	{
		location=&nodeArray[arrayIndex++];
		location->isNum=false;
		for(int i=0;i<KINDNUM;++i)
			location->next[i]=NULL;
		root=location;
	}

	while(word[i])
	{
		branch=word[i]-'0';
		if(location->next[branch])
		{
			if(location->next[branch]->isNum || !word[i+1])
				return false;
		}
		else {
			location->next[branch]=&nodeArray[arrayIndex++];
			location->next[branch]->isNum=false;
			for(int i=0;i<KINDNUM;++i)
				location->next[branch]->next[i]=NULL;
		}
		i++;
		location=location->next[branch];
	}
	location->isNum=true;
	return true;
}

int main(void)
{
#ifdef DEBUG   
	freopen("data.txt","r",stdin);  
#endif  

	int ncases,n;
	char num[12];
	scanf("%d",&ncases);
	while(ncases--)
	{
		scanf("%d",&n);
		arrayIndex=0;
		TrieNode *head=NULL;
		bool consistent=true;
		getchar();
		while(n--)
		{
			gets(num);
			bool bTemp;
			if(consistent)
				bTemp=insert(head,num);
			if(!bTemp)
				consistent=bTemp;
		}
		if(consistent)
			printf("YES\n");
		else
			printf("NO\n");
	}

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值