Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

AC了,提醒一下后来人

Posted by abcd1236386 at 2012-06-21 17:38:26 on Problem 3630
使用Trie树的第一,不要动态分配内存,直接分配一块大内存,使用,但是记得使用完后清空。
第二,输入的数据有需要排序,先短后长的输入,如果不排序也可以,但是必须加一个属性判断要在当前落脚的位置是否是被别人走过的,如果是,说明你是别人的前缀,也得输出NO
最后贴上自己的AC代码
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std;
int mx(int a,int b,int c)
{
	a=a>b?a:b;
	a=a>c?a:c;
	return a;
}
int mx(int a,int b)
{
	return a>b?a:b;
}
struct TrieNode{
	bool terminal;
	bool visited;
	TrieNode * son[10];
	TrieNode(){
		terminal=false;
		visited=false;
		memset(son,0,sizeof(son));
	}
};
class TrieTree{
public:
	TrieNode root;
	TrieNode mempool[1000000];
	int midx;
	bool Insert(char * num)
	{
		TrieNode *tmp=&root;
		while (*num)
		{
			if(tmp->son[*num-'0']==0)
			{
				mempool[midx].terminal=false;
				mempool[midx].visited=false;
				memset(mempool[midx].son,0,40);
				tmp->son[*num-'0']=(mempool+midx);
				midx++;
			}
			tmp=tmp->son[*num-'0'];
			if(tmp->terminal)//发现前缀
			{
				return true;
			}
			num++;
			if(*num==0)//定位
			{
				if(tmp->visited)
					return true;
				tmp->terminal=true;
				tmp->visited=true;
				break;
			}
			tmp->visited=true;
		}
		return false;
	}
	void clear()
	{
		memset(root.son,0,sizeof(root.son));
		midx=0;
	}
	TrieTree()
	{
		//root=new TrieNode;
		midx=0;
	}
	~TrieTree()
	{
		//delete root;
	}
};
char buf[11];
TrieTree ttree;
int main()
{
	int n,m;
	scanf("%d",&n);
	while(n--)
	{
		ttree.clear();
		scanf("%d",&m);
		bool hasprefix=false;
		while (m--)
		{
			scanf("%s",buf);
			if(hasprefix)
				continue;
			if(ttree.Insert(buf))
			{
				hasprefix=true;
			}
		}
		if(hasprefix)
			printf("NO\n");
		else
			printf("YES\n");

	}

}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator