| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
晕了 TRIE树超了,模拟过了#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator