-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26.cpp
More file actions
103 lines (71 loc) · 1.77 KB
/
Copy path26.cpp
File metadata and controls
103 lines (71 loc) · 1.77 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
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
void print(unordered_map<int,string> &m){
cout<<m.size()<<endl;
for (auto &pr : m)
{
cout<<pr.first<<" "<<pr.second<<"\n";
}
}
int main() {
// UNORDERED MAP
// differences between map and unordered map are :-
// 1. inbuilt implementation - instead of tree unordered_map uses hash table
// 2. Time complexity
// 3. valid keys datatype
// all functions are same
// unordered_map<int, string> m;
// m[1] = "abc"; // O(1)
// // insertion and access of memory both are of O(1)
// m[5] = "cdc";
// m[3] = "acd";
// print(m);
// m.find() and m.erase() --> O(1)
// If there is nothing to take with order in the question, then always use
// unordered_map because it will take less time to do any operation
// In unordered_map, internally hash tables are used which hash the keys to store them
// thus those data structuers that have hash function defined can only be used as key
// thus we can't use pair, vector
/*
Given N strings and Q queries. In each query you are given a string
print frequency of that string
N <= 10^6
|S| <= 100
Q <= 10^6
input
8
asj
gfi
abc
qef
abc
jkl
asj
abc
2
abc
gfi
*/
unordered_map<string, int> m;
int n;
cin>>n;
for (int i = 0; i < n; i++)
{
string s;
cin>>s;
m[s]++;
}
int q;
cin>>q;
while(q--){
string s;
cin>>s;
cout<<m[s]<<"\n";
}
// this while loop is 10^6 * O(1)
// if we have used map then it would be 10^6 * O(logN)
// MULTIMAP
// only difference between map and multimap is you can store duplicate keys
multimap<int, int> mapp;
return 0;
}