-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.cpp
More file actions
146 lines (108 loc) · 3.65 KB
/
Copy path25.cpp
File metadata and controls
146 lines (108 loc) · 3.65 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
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
140
141
142
143
144
145
146
#include <bits/stdc++.h>
using namespace std;
void print(map<int,string> &m){
cout<<m.size()<<endl;
for (auto &pr : m)
{
// accessing value of map is also O(logN)
// as we are accessing each value in map in one iteration of loop it is O(logN) and there are N iterations
cout<<pr.first<<" "<<pr.second<<"\n";
}
// thus this loop is N(logN)
}
int main() {
// MAP
// map is not continous
// thus we can't do it+1, but instead we can do it++ where it is iterator
// map stores key in sorted fashion and only unique keys exists
// map internally works using red black trees
// map<int, string> m;
// m[1] = "abc"; // this operation is O(logN) time where N is the current size of map
// m[5] = "cdc";
// m[3] = "acd";
// m.insert({4, "afg"}); //map stores a pair
// m.insert(make_pair(4, "afg"));
// // map<int, string>::iterator it;
// // for (it = m.begin(); it != m.end(); it++)
// // {
// // cout<<(*it).first<<" "<<(*it).second<<"\n";
// // }
// print(m);
// map<int, int> m;
// m[1] = 2; //this operation is O(logN)
// m[6]; // this is same as above and its also O(logN)
// // writing this above line only will insert value 0 or empty string
// // accessing each value of map is also logN
// print(m);
// // 2
// // 1 2
// // 6 0
// m[1] = 4;
// print(m);
map<int, string> m;
m[1] = "abc";
m[5] = "cdc";
m[3] = "acd";
// auto it = m.find(3); //its time complexity is also O(logN)
// //it returns the address
// // or if the key does not exist in the map it will return m.end()
// if(it == m.end()){
// cout<<"NO value";
// }else{
// cout<<(*it).first<<" "<<(*it).second;
// }
// m.erase(3); // O(logN)
// // it takes either value or the address as input
// print(m);
// // auto it = m.find(3);
// auto it = m.find(7);
// // m.erase(it);
// // if you give some value in erase which does not exist or address is out of map
// // then it will give segmentation fault
// // thus we will use it with a check
// if(it != m.end()){
// m.erase(it);
// }
// print(m);
// m.clear(); // it completely clears the map
// print(m);
// more in insertion
// time of insertion also depends on the key
// map<string, string> mpp;
// mpp["abcd"] = "abb";
// // because keys are stored in sorted fashion, it means key comparision also takes place
// // thus that time will also be accountable
// // s.size() * logN where s is the key
/*
Given N strings, print unique strings in lexographical order with their frequency
N <= 10^5
|S| <= 100
8
asj
gfi
abc
qef
abc
jkl
asj
abc
*/
map<string, int> mpp;
//hame string unique rakhni hai isliy inhe keys liya hai
int n;
cin>>n;
for (int i = 0; i < n; i++)
{
string s;
cin>>s;
// mpp[s] = mpp[s] + 1;
// | here mpp[s] is initialise to zero
// | so here 0 comes initially
// then 1 is added
mpp[s]++;
}
for(auto &pr:mpp){
cout<<pr.first<<" "<<pr.second<<"\n";
}
return 0;
}