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;
}
#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;
}
相关文章推荐
- POJ 3630 Phone List(Trie树,静态数组实现)
- POJ 3630 && HDU 1671 Phone list(静态字典树)
- POJ 3630 Phone List 静态字典树Trietree
- POJ 3630 && HDU 1671 Phone list(静态字典树)
- 【POJ】3630 - Phone List(字典树(静态建树))
- Phone List (POJ_3630) 静态字典树 + 模板题
- poj 3630 Phone List (字典树 +静态字典树)
- poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&静态创建】
- POJ 3630 Phone List 静态字典树
- 【POJ 3630】Phone List(静态字典树)
- POJ - 3630 Phone List(字典树建树同时检索)
- Trie树学习小记 Poj 3630 & Hdu1671 Phone List
- poj 3630 Phone List( 简单字典树 )
- hdu 1671&&poj 3630 Phone List 【字典树】
- POJ 3630 Phone List 字典树
- [字典树 ] poj 3630 Phone list
- poj_3630 Phone List(Trie树练习)
- poj3630—Phone List(字典树)
- POJ-3630 Phone List Trie树 | qsort
- POJ 3630/HDU 1671 Phone List(字典树)