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

晕了 TRIE树超了,模拟过了

Posted by dynamic_study at 2009-08-04 17:24:33 on Problem 3630
#include<iostream>
using namespace std;
const int kind =10;
bool loop;
struct tree
{
	int num;
	bool flag;             
	tree *next[kind];     
	tree()
	{
		int i;num=0;flag=false;  
		for(i=0;i<kind;i++)
		   next[i]=NULL;
	}
};
void insert(tree *&root,char word[])
{
	tree *location=root;
	int i=0,branch=0;
	if(location==NULL)
	{
		location=new tree();	root=location;
	}
	while(word[i])
	{
		branch=word[i]-'0';
		if(location->next[branch]==NULL)
	    	location->next[branch]=new tree();
		i++;
		location=location->next[branch];
		if(location->flag==true)
		{	
			location->num++; 
			if(location->num>=2)
				loop=false;
		}
	}
		location->flag=true;
		location->num++;
		for(i=0;i<kind;i++)
		{	if(location->next[i]!=NULL)
			{
				location->flag=true;
				location->num++;	
			    if(location->num>=2)
			     loop=false;
			}
		}
}
int main()
{
	int t,n,i;
	char str[15];
	cin>>t;
	while(t--)
	{
		tree  *root=NULL;
		cin>>n;
		loop=true;
		for(i=0;i<n;i++)
		{
		cin>>str;
		insert(root,str);
		}
		if(loop==true)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}

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