您的位置:首页 > 其它

POJ——3630 Phone List

2011-07-31 20:29 375 查看
这道题有很多种解法,可以用qsort,也可以用Trie树,Trie树做时需要开静态数组开辟空间,动态分配会TLE!在Discuss里看到两组数据,注意一下

2
2
1
12
2
12
1
NO
NO


下面是Trie树做法,在做题之前被某人提醒静态数组不要开得太大,否则会MLE,结果我就悲剧的RE了一晚上!!

 2780K 110MS

View Code

#include <stdio.h>
#include <string.h>
typedef struct node
{
int flag;
struct node * next[10];
}node;
node num[500000];
int x = 0;
int flg;
node * newnode()
{
node * p = num + x++;
int i;
for(i = 0; i < 10; i++)
{
p->next[i] = NULL;
}
p->flag = 0;
return p;
}
void insert(node * root, char *s)
{
int len = strlen(s);
int i, k;
node * p = root;
for(i = 0; i < len; i++)
{
k = s[i] - '0';
if(i == len - 1 && p->next[k] != NULL)
{
flg = 1;
return;
}
if(p->next[k] != NULL)
{
if(p->next[k]->flag)
{
flg = 1;
return;
}
p = p->next[k];
}
else
{
p->next[k] = newnode();
p = p->next[k];
}
}
p->flag = 1;
}
int main()
{
int t, n;
char s[20];
scanf("%d", &t);
while(t--)
{
x = 0; flg = 0;
node * root = newnode();
scanf("%d", &n);
while(n--)
{
scanf("%s", s);
if(!flg) insert(root, s);
}
if(flg) printf("NO\n");
else     printf("YES\n");
}
return 0;
}


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