poj 2001 Shortest Prefixes Trie
2015-03-06 13:11
399 查看
原题链接:http://poj.org/problem?id=2001
字典树的简单应用,具体实现如下:
字典树的简单应用,具体实现如下:
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef char State[24]; typedef struct _trie{ int cnt; struct _trie *next[26]; }trie, *Trie; State st[1020]; trie node[200000]; int sz = 0, count = 0; Trie built(){ Trie T = &node[sz++]; memset(T, 0, sizeof(trie)); return T; } void insert(Trie T, char *src){ char *p = src; while (*p != '\0'){ if (T->next[*p - 'a'] == NULL) T->next[*p - 'a'] = built(); T = T->next[*p - 'a']; T->cnt++; p++; } } int search(Trie T, char *src){ char *p = src; while (*p != '\0'){ if (T == NULL || T->next[*p - 'a'] == NULL) return 0; T = T->next[*p - 'a']; p++; } return T->cnt; } int main(){ #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif Trie root = built(); State ret, temp; while (~scanf("%s", ret)){ insert(root, ret); strcpy(st[count++], ret); } int i, t = 0; while (t < count){ int n = strlen(st[t]); for (i = 0; i < n; i++){ strncpy(temp, st[t], (i + 1) * sizeof(char)); temp[i + 1] = '\0'; if (1 == search(root, temp) || n == i + 1){ printf("%s %s\n", st[t], temp); break; } } t++; } return 0; }
相关文章推荐
- [poj 2001]Shortest Prefixes [Trie]
- POJ 2001 Shortest Prefixes 字典树Trie
- poj 2001 Shortest Prefixes trie
- POJ_2001_Shortest_Prefixes_(Trie)
- POJ 2001 Shortest Prefixes 【LCP(Trie)】
- POJ 2001 Shortest Prefixes // Trie
- POJ 2001 Shortest Prefixes【字典树】
- POJ 2001 Shortest Prefixes【字典树】
- POJ 2001 Shortest Prefixes
- Shortest Prefixes POJ - 2001 (字典树模板题~)
- poj 2001 Shortest Prefixes 字典树
- poj 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- [POJ 2001] Shortest Prefixes
- POJ2001 Shortest Prefixes 动态字典树实现
- POJ 2001 Shortest Prefixes 字典树
- POJ 2001 Shortest Prefixes 短的前缀 代码整理
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- Shortest Prefixes POJ - 2001(字典树)
- [复习][poj2001]字典树(trie树)Shortest Prefixes