POJ——3630 Phone List
2011-07-31 20:29
375 查看
这道题有很多种解法,可以用qsort,也可以用Trie树,Trie树做时需要开静态数组开辟空间,动态分配会TLE!在Discuss里看到两组数据,注意一下
下面是Trie树做法,在做题之前被某人提醒静态数组不要开得太大,否则会MLE,结果我就悲剧的RE了一晚上!!
2780K 110MS
View Code
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; }
相关文章推荐
- poj 3630 Phone List(Trie)
- POJ 3630-Phone List【比较许多字符中有没有一个是另一个的前缀,简单方法】
- POJ 3630 Phone List(Trie树,静态数组实现)
- 【原】 POJ 3630 Phone List Trie树 解题报告
- POJ 3630 Phone List
- HDU 1671 POJ 3630 Phone List
- poj 3630 Phone List
- POJ 3630 Phone List
- POJ 3630 Phone List 已被翻译
- POJ 3630 Phone List
- poj 3630(phone list)
- poj 3630 Phone List 贪心
- POJ 3630 Phone List
- POJ-3630 Phone List 字典树
- poj - 3630 - Phone List
- POJ 3630 Phone List
- POJ3630 Phone List 题解&代码
- poj 3630 Phone List trie
- poj 3630 Phone List (字典树 +静态字典树)
- POJ 3630 - Phone List (Trie树)