forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisubstr.cpp
62 lines (62 loc) · 802 Bytes
/
disubstr.cpp
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
// 2009-05-05
#include <iostream>
using namespace std;
struct trienode
{
trienode* next[26];
trienode()
{
memset(next,0,sizeof(next));
}
/*~trienode()
{
for (int i=0; i<26; i++)
if (next[i])
delete next[i];
}*/
};
struct trie
{
trienode* root;
int size;
trie()
{
root=new trienode;
size=0;
}
/* ~trie()
{
delete root;
}*/
void insert(char* s)
{
trienode *p=root, *q;
int i;
for (i=0; s[i]; i++)
{
if (!p->next[s[i]-'A']) //doesn't exist
{
size++;
p->next[s[i]-'A']=new trienode;
}
p=p->next[s[i]-'A'];
}
}
};
int main()
{
int T,i;
char s[1010];
scanf("%d",&T);
trie* t;
while (T--)
{
t=new trie;
scanf("%s",s);
for (i=0; s[i]; i++)
t->insert(s+i);
printf("%d\n",t->size);
}
//insert all non-empty suffixes
return 0;
}