-2

I am new in c++ and I am trying to solve educational exercise in quiz platform, but in this platform I should use no more than 64 MB of memory. My code use more than 130 MB.

#include <sstream>
#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include <map>

using namespace std;

template<class Container>
void splitString(const std::string &basicString, Container &cont, char delim = ' ') {
   std::stringstream ss(basicString);
   std::string token;
   while (std::getline(ss, token, delim)) {
       cont.push_back(token);
   }
}

int main() {
   int target = 0;
   int count = 0;

   std::map<int, int> set;

   string line;
   ifstream fileR("input.txt");

   std::vector<string> c;

   if (fileR.is_open()) {
       while (getline(fileR, line)) {
           if (count == 0) {
               target = std::stoi(line);
               count++;
               continue;
           }

           splitString(line, c);

           for (auto &d : c) {
               int key = std::stoi(d);

               if (set.count(key)) {
                   set[key] += 1;
               } else {
                   set[key] = 1;
               }
           }

           c.clear();
       }

       fileR.clear();
       fileR.close();
   }

   ofstream fileW;
   fileW.open("output.txt");

   bool found = false;

   for (const auto &p : set) {
       int d = target - p.first;

       if (set.count(d)) {
           if (p.first != d || set[d] > 1) {
               fileW << 1;
               found = true;
               break;
           }
       }
   }

   if (!found) {
       fileW << 0;
   }
   fileW.close();
   return 0;
}

What I can add, remove or change for keep within the coveted 64 MB? I tried free memory manually but no effects. I am not sure that is possible to write more effective algorithm.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • This reads like it's from some contest/challenge/competitive coding/hacking site. Is it? If your goal is to learn C++, you won't learn anything there. In nearly all cases, like this one, the correct solution is based on a mathematical or a programming trick. If you don't know what the trick is and attempt to code a brute-force approach, the program runs slow and fails for that reason. If you're trying to learn C++, you won't learn anything from meaningless online contest sites [but only from a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik May 25 '20 at 23:35
  • 3
    Careful with the tags. Many folks monitoring the C tag are also skilled with C++, but if they wanted to answer C++ questions, they would be monitoring the C++ tag. – user4581301 May 25 '20 at 23:45
  • We can't really help you without knowing exactly what is the problem this code is trying to solve. Regarding this line, `if (p.first != d || set[d] > 1)`, though, are you aware of the behaviour of [`std::map::operator[]`](https://en.cppreference.com/w/cpp/container/map/operator_at) and is this the intended one? – Bob__ May 26 '20 at 08:52

1 Answers1

2

Your vector (c) is declared outside the loop and is not cleared every time you call split string. This means every time you pass in a string to split, your vector contains stuff from the previous run. Is this intentional? If it is not, then move your vector into the loop, before you call split string, or clear it in your split string function. If it is intentional please provide more info about what your code is supposed to do.

castro
  • 409
  • 3
  • 13