patterncppMinor
Implementing a data structure that is a collection of sets
Viewed 0 times
collectiondatathatstructuresetsimplementing
Problem
I'm trying to optimize my code (time optimization) based on this problem:
Let
set of elements. Consider, for example,
Implement a data structure that is a collection of sets and supports
the operations
belongs to its own set. In particular,
Allowed operations are:
The reading must be done by standard inputs. For each test, the first
line contains the character and the two integers
where a represents the number of elements in
number of operations to be performed. The second line begins with the
character and followed by the list of strings in
lines b contain the operations to be performed.
N.B.: The last line contains the string
Here's my attempt:
```
int main() {
map myMap;
map > > mapSet;
std::ios_base::sync_with_stdio(false);
string line;
string line1;
string line2;
int value = 0;
while (line.compare("") != 0) {
cin >> line;
if (line.compare("i") == 0) {
cin >> line1 >> line2;
value = atoi(line1.c_str());
myMap.clear();
mapSet.clear();
}
if (line.compare("e") == 0) {
for (unsigned int i = 0; i > line1;
myMap[line1] = i;
set set;
set.insert(line1);
mapSet[i] = make_pair(line1.length(), set);
}
} else {
if (line.compare("m") == 0) {
cin >> line1 >> line2;
int index
Let
S be a set of strings. Each string in S is associated with aset of elements. Consider, for example,
S = {"home", "tree"}.Implement a data structure that is a collection of sets and supports
the operations
Union, Move and Print. Initially, each stringbelongs to its own set. In particular,
S1 = {'home'}, S2 = {'tree'}.Allowed operations are:
utree house: Combine the house sets containing (S1) and shaft (S2).
mtree house: Move the string in the set of tree house (S2).
shouse: Print the number of items in the home and the amount of length of the strings present.
The reading must be done by standard inputs. For each test, the first
line contains the character and the two integers
a and b (a, b > 0),where a represents the number of elements in
S and b represents thenumber of operations to be performed. The second line begins with the
character and followed by the list of strings in
S. The remaininglines b contain the operations to be performed.
N.B.: The last line contains the string
""Here's my attempt:
```
int main() {
map myMap;
map > > mapSet;
std::ios_base::sync_with_stdio(false);
string line;
string line1;
string line2;
int value = 0;
while (line.compare("") != 0) {
cin >> line;
if (line.compare("i") == 0) {
cin >> line1 >> line2;
value = atoi(line1.c_str());
myMap.clear();
mapSet.clear();
}
if (line.compare("e") == 0) {
for (unsigned int i = 0; i > line1;
myMap[line1] = i;
set set;
set.insert(line1);
mapSet[i] = make_pair(line1.length(), set);
}
} else {
if (line.compare("m") == 0) {
cin >> line1 >> line2;
int index
Solution
Before you attempt to optimize this, I would recommend refactoring it to be easier to follow. It may seem easier to just put everything into
But, luckily you've provided the challenge prompt for this, so it can be refactored.
Essentially, you can have three functions: one for each of the stated operations. They can all just be called in
It should then be much easier to follow the program's flow and determine where possible optimizations may be needed. You'll especially benefit from greater maintainability, for yourself and for others.
main() and not have to worry about functions, but that's actually worse. Just by looking at this, I wouldn't be able to tell what the code is doing nor how many different operations are being performed. There is a lot of nesting here, which can greatly complicate the code and even suggest longer runtime (especially nested loops and conditions in loops, which bring branch prediction into the equation).But, luckily you've provided the challenge prompt for this, so it can be refactored.
Essentially, you can have three functions: one for each of the stated operations. They can all just be called in
main() (unless one needs to call another), after initialing the data structures.It should then be much easier to follow the program's flow and determine where possible optimizations may be needed. You'll especially benefit from greater maintainability, for yourself and for others.
Context
StackExchange Code Review Q#54897, answer score: 3
Revisions (0)
No revisions yet.