HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Implementing a data structure that is a collection of sets

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
collectiondatathatstructuresetsimplementing

Problem

I'm trying to optimize my code (time optimization) based on this problem:


Let S be a set of strings. Each string in S is associated with a
set 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 string
belongs to its own set. In particular, S1 = {'home'}, S2 = {'tree'}.


Allowed operations are:



  • u tree house: Combine the house sets containing (S1) and shaft (S2).



  • m tree house: Move the string in the set of tree house (S2).



  • s house: 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 the
number of operations to be performed. The second line begins with the
character and followed by the list of strings in S. The remaining
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

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 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.