title: Need help optimizing a program that finds all possible substrings
body: <p>I have to find all possible, <em>unique</em> substrings from a bunch of user-input strings. This group of substrings has to be alphabetically sorted without any duplicate elements, and the group must be <em>queryable</em> by number. Here's some example input and output:</p>
  
  <p>Input:</p>
  
  <pre><code>3 // This is the user's desired number of strings
  abc // So the user inputs 3 strings
  abd
  def
  2 // This is the user's desired number of queries
  7 // So the user inputs 2 queries
  2
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>// From the alphabetically sorted group of unique substrings,
  bd // This is the 7th substring 
  ab // And this is the 2nd substring
  </code></pre>
  
  <p>Here's my implementation:</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      int number_of_strings;
      int number_of_queries;
      int counter;
      string current_string;
      string current_substr;
      map&lt;string, string&gt; substrings;
      map&lt;int, string&gt; numbered_substrings;
      int i;
      int j;
      int k;
  
      // input step
      cin &gt;&gt; number_of_strings;
      string strings[number_of_strings];
      for (i = 0; i &lt; number_of_strings; ++i)
              cin &gt;&gt; strings[i];
      cin &gt;&gt; number_of_queries;
      int queries[number_of_queries];
      for (i = 0; i &lt; number_of_queries; ++i)
              cin &gt;&gt; queries[i];
  
      // for each string in 'strings', I want to insert every possible
      // substring from that string into my 'substrings' map.
      for (i = 0; i &lt; number_of_strings; ++i) {
              current_string = strings[i];
              for (j = 1; j &lt;= current_string.length(); ++j) {
                      for (k = 0; k &lt;= current_string.length()-j; ++k) {
                              current_substr = current_string.substr(k, j);
                              substrings[current_substr] = current_substr;
                      }
              }
      }
  
      // my 'substrings' container is now sorted alphabetically and does
      // not contain duplicate elements, because the container is a map.
      // but I want to make the map queryable by number, so I'm iterating
      // through 'substrings' and assigning each value to an int key.
      counter = 1;
      for (map&lt;string,string&gt;::iterator it = substrings.begin();
                      it != substrings.end(); ++it) {
              numbered_substrings[counter] = it-&gt;second;
              ++counter;
      }
  
      // output step
      for (i = 0; i &lt; number_of_queries; ++i) {
              if (queries[i] &gt; 0 &amp;&amp; queries[i] &lt;= numbered_substrings.size()) {
                      cout &lt;&lt; numbered_substrings[queries[i]] &lt;&lt; endl;
              } else {
                      cout &lt;&lt; "INVALID" &lt;&lt; endl;
              }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I need to optimize my algorithm, but I'm not sure how to do it. Maybe it's the fact that I have a second for loop for assigning new int keys to each substring. Help?</p>