title: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted
body: <p>I am getting a <code>bad_alloc</code> exception in my program. </p>
  
  <p>These are the constraints:</p>
  
  <ul>
  <li>1 &lt;= T &lt;= 10</li>
  <li>The length of each string is at most 100000 and contains only lower case characters.</li>
  </ul>
  
  <p>With those constraints, I am unable to figure out why my program is getting <code>bad_alloc</code>.</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class SuffixArray {
      std::vector&lt;std::string&gt; suffixes;
      size_t N;
  public:
      SuffixArray(std::string&amp; s) {
          N = s.length();
          suffixes.resize(N);
          for (size_t i = 0; i &lt; N; i++)
              suffixes[i] = s.substr(i);
          std::sort(suffixes.begin() , suffixes.end());
      }
      ~SuffixArray() {
      }
      size_t lcp(std::string&amp; s, std::string&amp; t) {
          size_t N = std::min(s.length(), t.length());
          for (size_t i = 0; i &lt; N; i++)
              if (s[i] != t[i]) 
                  return i;
          return N;
      }    
  };
  
  int main ( int argc, char **argv) {
      int T;
      std::cin &gt;&gt; T;
      std::vector&lt;size_t&gt; results;
  
      for ( int i = 0; i &lt; T; i++) { 
          std::string str;
          std::cin &gt;&gt; str;
          SuffixArray sa(str);
          size_t sol = 0;
  
          for ( std::string::iterator it = str.begin(); it != str.end(); ++it) {
              std::string target = std::string(it, str.end());
              sol += sa.lcp(str, target);            
          }
          results.push_back(sol);
      }
      for ( std::vector&lt;size_t&gt;::iterator it = results.begin(); it != results.end(); ++it) 
          std::cout &lt;&lt; *it &lt;&lt; std::endl;
      results.clear();
  
      return 0;
  }
  </code></pre>