forked from panthji/cppprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
92 lines (87 loc) · 1.76 KB
/
trie.cpp
File metadata and controls
92 lines (87 loc) · 1.76 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
90
91
92
//https://www.hackerrank.com/challenges/contacts/problem
#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; i < n; i++)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%I64d", &x)
#define ss(s) scanf("%s", s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define clr(x) memset(x, 0, sizeof(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
// int mod(int a,int b){
// return (((a % m) x (b % m)) % m );
// }
// int dp[]
struct node
{
int cnt = 0;
node *next[26];
node()
{
int i;
fo(i, 26)
{
next[i] = NULL;
}
}
};
void addnode(node *head, string s)
{
for (auto i : s)
{
if (!head->next[i - 'a'])
head->next[i - 'a'] = new node();
head = head->next[i - 'a'];
head->cnt++;
}
}
int findstr(node *head, string s)
{
for (auto i : s)
{
if (!head->next[i - 'a'])
return false;
head = head->next[i - 'a'];
}
return head->cnt;
}
int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
int n;
cin >> n;
node root;
node *head = &root;
int i;
string t, s;
fo(i, n)
{
head = &root;
cin >> t >> s;
addnode(head, s);
}
fo(i, n)
{
head = &root;
cin >> t >> s;
cout << findstr(head, s) << "\n";
}
return 0;
}