-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.cpp
109 lines (95 loc) · 2.53 KB
/
Trie.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<bits/stdc++.h>
using namespace std;
#define deb(x) cout<<#x<<" = "<<x<<endl;
struct trie
{
int isend;
trie *child[26];
trie()
{
isend=0;
for(int i=0; i<26; i++)
child[i]=NULL;
}
};
void insert(trie *root,string s)
{
trie * temp=root;
for(int i=0; i<s.length(); i++)
{
int idx=s[i]-'a';
if(temp->child[idx]==NULL)
temp->child[idx]=new trie();
temp=temp->child[idx];
}
temp->isend++;;
}
int query(trie *root,string s)
{
trie* temp=root;
for(int i=0; i<s.length(); i++)
{
int idx=s[i]-'a';
if(temp->child[idx]==NULL)
return 0;
temp=temp->child[idx];
}
return temp->isend;
}
void words(trie *root,string s)
{
if(root==NULL)
return;
if(root->isend!=0)
{
for(int i=0;i<root->isend;i++)cout<<s<<endl;
}
for(int i=0; i<26; i++)
{
if(root->child[i]==NULL)
continue;
else
{
// if(root->child[i]->isend!=0)
// {
// for(int j=0; j<root->child[i]->isend; j++)
// cout<<s+(char)(i+'a')<<endl;
// }
words(root->child[i],s+(char)(i+'a'));
}
}
}
int main()
{
trie *root=new trie();
string str="In computer science about there there is ";
//, a trie, also called digital tree or prefix tree, is a type of search tree, a tree data structure used for locating specific keys from within a set. These keys are most often strings, with links between nodes defined not by the entire key, but by individual characters. In order to access a key (to recover its value, change it, or remove it), the trie is traversed depth-first, following the links between nodes, which represent each character in the key.";
///INSERT in length of string (complexity)
///Search in length of string (complexity)
for(int i=0; i<str.length(); i++)
{
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]-'A'+'a';
else if(!isalpha(str[i]))
str[i]=' ';
}
istringstream os(str);
string token;
vector<string>word;
while(os>>token)
{
word.push_back(token);
}
for(int i=0; i<word.size(); i++)
{
insert(root,word[i]);
}
trie * temp=root;
words(temp,"");
while(1)
{
string ss;
cin>>ss;
cout<<query(root,ss)<<endl;
}
}