抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

trie tree poj 2001 shortest prefixes

POJ 2001 Shortest Prefixes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>  
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <utility>
#include <sstream>
#include <climits>
#include <cassert>
#define BUG puts("here!!!");

using namespace std;
const int N = 1010;
const int kind = 26;
char str[N][25];
struct Node {
int num;
bool tail;
Node* next[kind];
Node() : num(1), tail(false) {
memset(next, 0, sizeof(next));
}
};
void insert(Node* root, char *s) {
Node* p = root;
int i = 0, index;
while(s[i]) {
index = s[i] - 'a';
if(p->next[index] == NULL) {
p->next[index] = new Node();
}
else p->next[index]->num++;
p = p->next[index];
i++;
}
p->tail = true;
}
void solve(Node* root, int count) {
for(int i = 0; i < count; i++) {
Node* p = root;
int len = strlen(str[i]);
char* s = str[i];
cout << s << ' ';
for(int j = 0; j < len; j++) {
cout << s[j];
int index = s[j] - 'a';
if(p->next[index]->num == 1) {
break;
}
p = p->next[index];
}
cout << endl;
}
}
int main() {
Node* root = new Node(); // 根节点不包含任何字符
int i = 0, count = 0;
while(scanf("%s", str[i]) == 1) {
insert(root, str[i]);
i++;
}
count = i;
solve(root, count);
return 0;
}

Comments