views:

120

answers:

1

The input is string[] like below.

"CSE111: CSE110 MATH101"

"CSE110:"

I need to order the strings based on some logic. For example my output should be a string[] like

"CSE110","MATH122","CSE111"

My question is

  1. While scanning through the input array, if one string is picked to be the first string of the output array, then how do I skip all occurrences of that particular string, while I continue to process the string[] for the second output string etc..

Eg:

Input:

"CSE111: CSE110 MATH101"

"CSE110:"

If CSE110 is picked to be the first string in the output, then when I scan through the input string[] for the second string to be a part of output, I should not consider CSE110.

How can I achieve this? The answer I am looking forward to is something like:

  1. Store the input in a string[]
  2. loop through the strings one by one using strtok or stringstream >> operator.
  3. Once the first string is found ...blah blah blah ....

Hope my question is clear enough. I will be glad to provide more details.

Edit1:More Explanation

The strings represent the order in which the classes need to taken . If a class has pre-requisite , the pre-requisite has to be taken first. ie. if Input is

"CSE111: CSE110 MATH101"
"CSE110:"

The class CSE111 has a pre-requisite of CSE110 MATH101 . So I need to consider first CSE1110(No Pre-requisite) - MATH101((No Pre-requisite) and then CSE111 . (Further Ties can broken in alphabetical order. ) I hope this helps..

+6  A: 

I hopefully got it now: For a string of the form A: B C D, the course A has B, C, and D as prerequisites.

In that case you want a mapping from a course to its prerequisites, e.g.:

typedef std::set<std::string> CourseSet;
typedef std::map<std::string, CourseSet> Prerequisites;

Now you can fill a Prerequisites by tokenizing, using the first part as the key and a CourseSet as the value.

As it seems that you just want one of the possible orders for all courses in the input, you could then do the following:

  • complete the prerequisites for the courses (i.e. include the courses they indirectly depend on)
  • now a>b if a has b as a prerequisite
  • if b doesn't have a as a prerequisite use e.g. the lexicographical order
Georg Fritzsche
I think he's also trying to specify the first value, with the rest of the array sorted normally.
Justin Johnson
Well I cannot use std:sort() as It is not a simple sorting that I need to do . The strings represent the order in which the classes need to taken . If a class has pre-requisite , the pre-requisite has to be taken first. ie. if Input is "CSE111: CSE110 MATH101" "CSE110:"The class CSE111 has a pre-requisite of CSE110 MATH101 . So I need to consider first CSE1110(No Pre-requisite) - MATH101((No Pre-requisite) and then CSE111 . (Further Ties can broken in alphabetical order. )I hope this helps..
Eternal Learner
@Eternal: I get your prerequisites now, but for now i only see a list of dependencies - what is the actual input for which you want to generate the course-list?
Georg Fritzsche
@Georg:Input:classSchedule={"CSE121: CSE110","CSE110:","MATH122:",}Output:{"CSE110","CSE121","MATH122"}
Eternal Learner
@Eternal: Updated under the assumption that every course with no prerequisites also appears as `"FOO:"` in the input - at least your first example in the question doesn't have that form.
Georg Fritzsche