-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathCount of distinct substrings of a string using Suffix Array
86 lines (59 loc) · 1.32 KB
/
Count of distinct substrings of a string using Suffix Array
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
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
# define hashmap unordered_map<char,node*>
int countr=0;
struct node{
char ch;
hashmap h;
bool terminal;
};
struct node* construct_node(char c)
{
struct node* new_node= new node[1];
new_node->ch=c;
new_node->terminal=false;
countr++;
return new_node;
}
void addword(string s,struct node*root)
{
int n=s.length();
struct node*temp=root;
for(int i=0;i<n;i++)
{
if(temp->h.count(s[i])==0)
{
struct node*new_node = construct_node(s[i]);
temp->h[s[i]]=new_node;
temp=new_node;
}
else
temp=temp->h[s[i]];
}
temp->terminal=true;
return;
}
bool lookup(string s,node*root)
{
struct node* temp=root; int n=s.length();
for(int i=0;i<n;i++)
{
if(temp->h.count(s[i])==0)
return false;
else
temp=temp->h[s[i]];
}
return temp->terminal;
}
int main() {
struct node*root=construct_node('\0');
/* addword("shailendra",root);
addword("shail",root);
cout<<lookup("shailendra",root)<<endl;*/
string s; cin>>s;
int n=s.length();
for(int i=0;i<n;i++)
addword(s.substr(i,n-i),root);
cout<<countr<<endl;
return 0;
}