-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl_ex.cpp
81 lines (71 loc) · 1.05 KB
/
stl_ex.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
/*
STL Exapmle using map and pair
Author Sharique
e.g.
input :
10
ADD abcd
ADD cdef
ADD abcd
ADD abcd
ADD cdef
ADD ghf
ADD fff
SEARCH abcd
SEARCH cdef
SEARCH rrrr
output:
1 4
2 5
-1 -1
*/
#include <iostream>
#include<map>
#include<utility>
using namespace std;
int main() {
int n;
cin>>n;
/*
map to store the string and first and last occurance of string
key of map is string
value of map is pair conting first and last
*/
map<string,pair<int,int>> m;
string value,query;
//query number
int q = 1;
while( n --)
{
cin>>query>>value;
if(query == "ADD")
{
/*
if value is presnt then set last occurance
*/
if(m.find(value) != m.end())
{
m[value].second = q;
q++;
}
else
{
/*
if value is not presnt then make a pair
value of first and last equals to query number
*/
pair<int,int>p(q,q);
m[value] = p;
q++;
}
}
else if(query == "SEARCH")
{
if(m.find(value) != m.end())
cout<<m[value].first<<" "<<m[value].second<<endl;
else
cout<<"-1 -1"<<endl;
}
}
return 0;
}