### James S. Plank

Mon Mar 24 14:30:29 EDT 2014
Problem Statement.
This is a really nice problem to practice sets (and maps if you want) and stringstreams.

There are quite a few components to this problem, but let's outline a solution.

• First, you want to keep a set of the dangerous words. You'll start by inserting every element of dangerous. It's easy now to see if a word is dangerous -- simply see if you can find it in the set.

• Second, you want to keep track of the web sites that you know are dangerous. To do that, I'd use one of two data structures:
• A set of integers - this is the index of the web site in address.
• A map whose key is integers (indices of address) and whose val is the address. (In other words, if i is a key in the map, then its val is address[i].
The set is probably easier.

• Finally, you need a vector of vectors of strings, KV, such that KV[i] is a vector of all of the keywords in keywords[i]. Use a stringstream to create KV at the beginning of your program.

Then, your algorithm should be as follows:

• For each web site that is not currently dangerous, see if it is dangerous by checking all of its keywords (from KV) against the set of dangerous words.

• If a web site is determined to be dangerous, put it into the set/map of dangerous web sites, and then insert all of its keywords into the set of dangerous keywords. Because this is a set, it will throw away duplicates, which is fine.

• When you're done, check to see if you added any dangerous keywords. If you have, then you need to repeat the algorithm. If you haven't, you're done -- use the set/map of dangerous web sites to create the return value.

Let's do an example -- example 0 works. First, we create our set of dangerous keywords:

{ "decryption" "encryption" "hack" "interference" "internet" "signal" }

We'll also use a set of dangerous web sites. That starts as empty. And finally, we'll create KV:

• KV[0] = { "hack", "encryption", "decryption", "internet", "algorithm" }
• KV[1] = { "signal", "interference", "evil", "snake", "poison", "algorithm" }
• KV[2] = { "flower", "baloon", "topcoder", "blue", "sky", "sea" }
Now, we do pass one -- we start with i equals zero and we see that "hack", "encryption", "decryption", and "internet" are all dangerous. Since that is above the threshold, we add all of KV[0]'s words to the dangerous set, which now becomes:

{ "algorithm", "decryption" "encryption" "hack" "interference" "internet" "signal" }

We also add i=0 to the set of dangerous websites. The set is { 0 }.

When i=1 we see that there are three dangerous words in KV[1] ("signal", "interference" and "algorithm"), so we add all of KV[1] to the set of dangerous words, which is now:

{ "algorithm", "decryption" "encryption" "evil" "hack" "interference" "internet" "poison" "signal" "snake" }

The set of dangerous web sites is now { 0, 1 }.

We continue with i=2, and we see that there are no dangerous words in KV[2]. We are at the end of web sites, so we ask "Did we add to the number of dangerous words on this pass through the web sites?" The answer is yes, so we do it again.

Now, when i equals 0 and 1, we ignore the web sites because they are already dangerous. When i equals 2, we see again that no words in KV[2] are dangerous. We are done with the web site, and since we didn't add any new dangerous words, we can exit -- we run through the set of dangerous web sites and add their addresses to the return value: { "www.topcoder.com", "www.sindicate_of_evil.com" }.