summaryrefslogtreecommitdiff
path: root/Sources/DataStructure.cpp
blob: 07f8b4330a90eeb1fd9a41ba87a94a4c155c7e30 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <vector>
#include <string>

using std::string;
using std::vector;
using std::cin;

struct Query {
    string type, name;
    int number;
};

vector<Query> read_queries() {
    int n;
    cin >> n;
    vector<Query> queries(n);
    for (int i = 0; i < n; ++i) {
        cin >> queries[i].type;
        if (queries[i].type == "add")
            cin >> queries[i].number >> queries[i].name;
        else
            cin >> queries[i].number;
    }
    return queries;
}

void write_responses(const vector<string>& result) {
    for (size_t i = 0; i < result.size(); ++i)
        std::cout << result[i] << "\n";
}

vector<string> process_queries(const vector<Query>& queries) {
    vector<string> result;
    // Keep list of all existing (i.e. not deleted yet) contacts.
    vector<string> contacts(10000000);
    for (size_t i = 0; i < contacts.size(); i++) {
       contacts[i] = "not found";
    }
    for (size_t i = 0; i < queries.size(); ++i)
        if (queries[i].type == "add") {
        	contacts[queries[i].number] = queries[i].name;
        } else if (queries[i].type == "del") {
        	contacts[queries[i].number] = "not found";
        } else {
            result.push_back(contacts[queries[i].number]);
        }
    return result;
}

int main() {
    write_responses(process_queries(read_queries()));
    return 0;
}

//#include <cstdio>
//#include <cstdlib>
//#include <vector>
//#include <algorithm>
//#include <iostream>
////#include <gtest/gtest.h>
//
//using std::cin;
//using std::cout;
//using std::endl;
//using std::max;
//using std::vector;
//
//struct Table {
//	int numberOfRows;
//	int setID;
//
//	Table(int numberOfRows, int setID) :
//			numberOfRows(numberOfRows), setID(setID) {
//	}
//};
//
//int maximumNumberOfRows = -1;
//vector<int> parent;
//vector<int> rank;
//vector<Table> tables;
//
//int find(int i) {
//	if (i != parent[i])
//		parent[i] = find(parent[i]);
//	return parent[i];
//}
//
//void unite(int destination, int source) {
//	int destination_id = find(destination);
//	int source_id = find(source);
//	if (destination_id == source_id)
//		return;
//	if (rank[destination_id] > rank[source_id])
//		parent[source_id] = destination_id;
//	else {
//		parent[destination_id] = source_id;
//		if (rank[destination_id] == rank[source_id])
//			rank[source_id] = rank[source_id] + 1;
//	}
//}
//
//void merge(int destination, int source) {
//	int d = find(destination);
//	int s = find(source);
//	Table* realDestination = &tables[d];
//	Table* realSource = &tables[s];
//	if (d == s) {
//		return;
//	}
//	maximumNumberOfRows = max(maximumNumberOfRows,
//			tables[realDestination->setID].numberOfRows + tables[realSource->setID].numberOfRows);
//	realDestination->numberOfRows = tables[realDestination->setID].numberOfRows
//			+ tables[realSource->setID].numberOfRows;
//	realSource->numberOfRows = 0;
//	realSource->setID = d;
//	realDestination->setID = d;
//	unite(destination, source);
//}
//
//int main() {
//	int n, m;
//	cin >> n >> m;
//
//	parent.resize(n);
//	rank.resize(n);
////	tables.resize(n);
//	for (int i = 0; i < n; i++) {
//		int numberOfRows;
//		cin >> numberOfRows;
//		tables.push_back(Table(numberOfRows, i));
////		tables[i] = Table(numberOfRows, i);
//		parent[i] = i;
//		rank[i] = 0;
//		maximumNumberOfRows = max(maximumNumberOfRows, numberOfRows);
//	}
//
//	for (int i = 0; i < m; i++) {
//		int destination, source;
//		cin >> destination >> source;
//		--destination;
//		--source;
//
//		merge(destination, source);
//		cout << maximumNumberOfRows << endl;
//	}
//
//	return 0;
//}
//
////TEST(MergingTables, t) {
////    maximumNumberOfRows = 1;
////    rank = {0, 0, 0, 0, 0};
////    parent = {0, 1, 2, 3, 4};
////    tables = {Table(1, 0), Table(1, 1), Table(1, 2), Table(1, 3), Table(1, 4)};
////
////    merge(2, 4);
////    ASSERT_EQ(2, maximumNumberOfRows);
////
////    merge(1, 3);
////    ASSERT_EQ(2, maximumNumberOfRows);
////
////    merge(0, 3);
////    ASSERT_EQ(3, maximumNumberOfRows);
////
////    merge(4, 3);
////    ASSERT_EQ(5, maximumNumberOfRows);
////
////    merge(4, 2);
////    ASSERT_EQ(5, maximumNumberOfRows);
////}
////
////TEST(MergingTables, t1) {
////    maximumNumberOfRows = 10;
////    rank = {0, 0, 0, 0, 0, 0};
////    parent = {0, 1, 2, 3, 4, 5};
////    tables = {Table(10, 0), Table(0, 1), Table(5, 2), Table(0, 3), Table(3, 4), Table(3, 5)};
////
////    merge(5, 5);
////    ASSERT_EQ(10, maximumNumberOfRows);
////
////    merge(5, 4);
////    ASSERT_EQ(10, maximumNumberOfRows);
////
////    merge(4, 3);
////    ASSERT_EQ(10, maximumNumberOfRows);
////
////    merge(3, 2);
////    ASSERT_EQ(11, maximumNumberOfRows);
////}