-
Notifications
You must be signed in to change notification settings - Fork 0
/
DICT.cpp
139 lines (134 loc) · 2.46 KB
/
DICT.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//be careful of the input output format
#include<bits/stdc++.h>
using namespace std;
#define CHAR_TO_INDEX(c) ((int)c-(int)'a')
struct Trie_node
{
int value;
Trie_node *children[26];
};
class Trie
{
private:Trie_node* root;int count;
public:
Trie()
{
root= getnode();
count=0;
}
Trie_node *getnode()
{
Trie_node *node=new Trie_node;
if(node)
{
node->value=0;
for(int i=0;i<26;i++)
node->children[i]=NULL;
}
return node;
}
void insert(char *key)
{
Trie_node *pCrawl=root;
count++;
int level;
int index;
int len=strlen(key);
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(key[level]);
if(!pCrawl->children[index])
{
pCrawl->children[index]=getnode();
}
pCrawl=pCrawl->children[index];
}
pCrawl->value=count;
}
int search(char *key)
{
Trie_node *pCrawl=root;
int level;
int index;
int flag=0;
int len=strlen(key);
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(key[level]);
if(!pCrawl->children[index])
return 0; //return 0 if null link is encountered
pCrawl=pCrawl->children[index];
}
for(int i=0;i<26;i++)
{
if(pCrawl->children[i]!=NULL)
{
flag=1;
break;
}
}
// cout<<pCrawl->value<<" "<<flag<<endl;
//cout<<level<<endl;
if(flag==1)
{
dfs(key,pCrawl,level,level); //prints all the words that contains the given prefix
if(flag==1 && pCrawl->value==0)
return flag; //if prefix terminates on a intermediate node with value=0
return pCrawl->value; //if prefix terminates on a intermediate node with value!=0
}
else
return flag; //if the prefix occurs in no word print no match
//cout<<level<<endl;
}
void dfs(char *str,Trie_node *curr,int pre,int level)
{
if(curr->value!=0 && level>pre)
{
cout<<str<<"\n";
}
for(int i=0;i<26;i++)
{
if(curr->children[i]!=NULL)
{
char ch='a'+i;
str[level]=ch;
//cout<<str[level];
str[level+1]='\0';
//cout<<str<<level<<endl;
//cout<<i<<" "<<str<<endl;
//curr=curr->children[i];
dfs(str,curr->children[i],pre,level+1);
}
}
}
};
bool comp(const string &p,const string &q)
{
return p.length()>q.length();
}
int main()
{
char word[30];
char prefix[30];
int ch,k,t,n,i;
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
Trie tr;
cin>>t;
while(t--)
{
cin>>word;
tr.insert(word);
}
cin>>k;
for(i=1;i<=k;i++)
{
cin>>prefix;
cout<<"Case #"<<i<<":"<<"\n";
if(!tr.search(prefix))
{
cout<<"No match.\n";
}
}
return 0;
}