您的位置:首页 > 其它

poj 3630 Phone List trie树—字典树 静态数组版!!!

2011-10-02 10:47 399 查看
做这道题历经re,wa,tle,最后看了别人的代码写出来的!!!!!!!!!!

#include<iostream>

using namespace std;

#define max 10

int num;

bool ok;

struct node

{

bool isword;

int next[max];

void init()

{

memset(next,0,sizeof(next));

isword=false;

}

}tree[100010];

void insert(char a[])

{

int cou=0;

int index=0;

int len=strlen(a);

for(int i=0;i<len;i++)

{

if(tree[index].next[a[i]-'0']==0)

{

tree[++num].init();//建立新节点

tree[index].next[a[i]-'0']=num;//连接

index=num;

}

else

{

cou++;

index=tree[index].next[a[i]-'0'];

if(tree[index].isword)

{

ok=false;

return;

}

}

}

// cout<<index;

// system("pause");

tree[index].isword=true;

if(cou==len)//关键是这个,防止9112,911的情况!!!

ok=false;

}

int main()

{

char a[14];

int t,n;

cin>>t;

while(t--)

{

cin>>n;

tree[0].init();

ok=true;

num=0;

while(n--)

{

if(ok)

{

scanf("%s",a);

insert(a);

}

else

scanf("%s",a);

}

if(ok)

printf("YES\n");

else

printf("NO\n");

}

// system("pause");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: