-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrie_using_2darray.cpp
More file actions
89 lines (82 loc) · 1.7 KB
/
Copy pathtrie_using_2darray.cpp
File metadata and controls
89 lines (82 loc) · 1.7 KB
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
//tries
//strings
//2d array
//codeforces
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<ctype.h>
#include<iostream>
#include<map>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
#define inf 1000000000
#define mod 1000000007
#define ll long long
#define in(x) scanf("%d",&x);
#define rep(i,n) for(i=0;i<n;i++)
struct node
{
int word;
int pre;
int n;
};
node a[10000][26];
int main()
{
int no,i,v=0,z,next=1;
cout<<"1. enter word"<<endl<<"2. find prefix"<<endl<<"3. find word"<<endl;
in(no);
char s[100];
while(no--)
{
v=0;
scanf("%d",&z);
if(z==1)
{
scanf("%s",&s);
for(i=0;i<strlen(s);i++)
{
if(a[v][s[i]-97].n==0)
a[v][s[i]-97].n=next++;
a[v][s[i]-97].pre++;
v=a[v][s[i]-97].n;
}
a[v][s[i]-97].word++;
}
else if(z==2)
{
scanf("%s",&s);
v=0;
for(i=0;i<strlen(s);i++)
{
if(a[v][s[i]-97].n==0)
{
cout<<0<<endl;
break;
}
if(i==strlen(s)-1)
{
cout<<a[v][s[i]-97].pre<<endl;;
break;
}
v=a[v][s[i]-97].n;
}
}
else
{
scanf("%s",&s);
v=0;
for(i=0;i<strlen(s);i++)
{
if(a[v][s[i]-97].n==0)
break;
v=a[v][s[i]-97].n;
}
cout<<a[v][s[i]-97].word<<endl;
}
}
return 0;
}