Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:12:08+00:00 2026-05-27T21:12:08+00:00

I am getting a bad_alloc exception in my program. These are the constraints: 1

  • 0

I am getting a bad_alloc exception in my program.

These are the constraints:

  • 1 <= T <= 10
  • The length of each string is at most 100000 and contains only lower case characters.

With those constraints, I am unable to figure out why my program is getting bad_alloc.

#include <string>
#include <algorithm>
#include <iostream>
#include <vector>

class SuffixArray {
    std::vector<std::string> suffixes;
    size_t N;
public:
    SuffixArray(std::string& s) {
        N = s.length();
        suffixes.resize(N);
        for (size_t i = 0; i < N; i++)
            suffixes[i] = s.substr(i);
        std::sort(suffixes.begin() , suffixes.end());
    }
    ~SuffixArray() {
    }
    size_t lcp(std::string& s, std::string& t) {
        size_t N = std::min(s.length(), t.length());
        for (size_t i = 0; i < N; i++)
            if (s[i] != t[i]) 
                return i;
        return N;
    }    
};

int main ( int argc, char **argv) {
    int T;
    std::cin >> T;
    std::vector<size_t> results;

    for ( int i = 0; i < T; i++) { 
        std::string str;
        std::cin >> 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<size_t>::iterator it = results.begin(); it != results.end(); ++it) 
        std::cout << *it << std::endl;
    results.clear();

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:12:08+00:00Added an answer on May 27, 2026 at 9:12 pm

    Because what you do here:

      for (size_t i = 0; i < N; i++)
            suffixes[i] = s.substr(i);
    

    is: Create N sub strings of lengths 0, 1, 2, …, N
    The total amount of memory these are going to consume is: 1 + 2 + 3 + ... + N bytes. Having good old Gauss at your hand you will find that the sum of the first N numbers is: N * (N + 1) / 2

    Now if you set N = 100,000 this results in about 5GB of memory consumption – which is larger than the max. 2GB address space your program usually has unless you run it on a 64bit system.

    Edit: I don’t know what the problem is you are trying to solve however your implementation seems strange:

    You never ever use the computed suffixes: The only function of SuffixArray you are using is lcp which makes no reference to the stored suffixes vector. So what you need them for in the first place?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting a -> Program received signal: EXC_BAD_ACCESS. In the following code but
I am trying to catch the bad_alloc exception in order to prove that the
I keep getting bad instruction or bad exception so I enabled NSZombieEnabled and the
I'm getting this error when trying to see the contents of a NSMutableArray: Program
I'm getting a json from server. I can parse only one JSON at time.
I keep getting these line numbers for errors in the external libraries cpp files,
I am getting a EXC_BAD_ACCESS on the the glDrawElements() in this code, but only
I'm getting a bad error. When I call delete on an object at the
I keep getting a exc bad access error and I think it has something
I'm getting weird EXC_BAD_ACCESS errors. I thought that I didn't need to retain objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.