0%

Prefixes【字典树】

POJ2001

字典树板子

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
//poj1251
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define gets gets_s
#define scanf scanf_s

char word[100010][51];
int t[1000010][26];
int sz = 0;
int mark[10000010];
void insert(char s[]) {
int k, len = strlen(s+1), now = 0;
for (int p = 1; p <= len; p++) {
k = s[p] - 'a';
if (t[now][k] == 0) t[now][k] = ++sz;
now = t[now][k];
mark[now]++;
}
}

void ask(char ch[]) {
int now = 0, k, len = strlen(ch + 1);
for (int p = 1; p <= len; p++) {
if (mark[now] == 1) break;
k = ch[p] - 'a';
printf("%c", ch[p]);
now = t[now][k];
}
}


int main() {
int tot = 0;
while (cin >> (word[++tot]+1)) {
insert(word[tot]);
}
for (int i = 1; i <= tot; i++) {
printf("%s ", word[i] + 1);
ask(word[i]);
printf("\n");
}

return 0;
}

求大佬赏个饭