Problem p02266 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <deque>
#include <iostream>
#include <string>

#define INPUT_FROM_FILE (0)
#if INPUT_FROM_FILE
#include <fstream>
#endif

int main(int argc, char **argv) {
#if INPUT_FROM_FILE
  std::ifstream ifs("test.txt");
#endif
  std::string str;

#if INPUT_FROM_FILE
  ifs >> str;
#else
  std::cin >> str;
#endif

  std::deque<std::pair<int, int>> answer;
  std::deque<int> s;
  int sum_area = 0;

  for (unsigned int i = 0; i < str.size(); i++) {
    if (str.at(i) == '\\\\') {
      s.push_back(i);
    } else if (str.at(i) == '/' && !s.empty()) {
      int sp = s.back();
      int area = i - sp;

      s.pop_back();
      while (!answer.empty() && sp <= answer.back().first) {
        area += answer.back().second;
        answer.pop_back();
      }
      answer.push_back(std::pair<int, int>(sp, area));
    }
  }

  for (unsigned int i = 0; i < answer.size(); i++) {
    sum_area += answer[i].second;
  }

  std::cout << sum_area << std::endl;
  std::cout << answer.size();
  for (unsigned int i = 0; i < answer.size(); i++) {
    std::cout << " " << answer[i].second;
  }
  std::cout << std::endl;

  return 0;
}


Error: MLE

Fixed submission:

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

#define INPUT_FROM_FILE (0)
#if INPUT_FROM_FILE
#include <fstream>
#endif

void print(int w, int h, const std::vector<char> &diagram) {
  for (int y = 0; y < h; y++) {
    int offset = y * w;
    for (int x = 0; x < w; x++) {
      int cidx = offset + x;
      std::cout << diagram[cidx];
    }
    std::cout << std::endl;
  }
  return;
}

class Flood {
private:
  int x_;
  int y_;
  int w_;
  int h_;
  int area_;

public:
  Flood(int x, int y, int w, int h) : x_(x), y_(y), w_(w), h_(h), area_(0) {}

  virtual ~Flood() {}

  void search(int x, int y, const std::vector<char> &diagram,
              std::vector<bool> &is_checked) {
    if (x >= w_ || y >= h_ || y < y_) {
      return;
    }
    int cidx = y * w_ + x;
    if (is_checked[cidx]) {
      return;
    }

    is_checked[cidx] = true;
    if (diagram[cidx] == '\\\\') {
      area_++;
      search(x + 1, y, diagram, is_checked);
    } else if (diagram[cidx] == '_') {
      area_ += 2;
      search(x + 1, y, diagram, is_checked);
    } else if (diagram[cidx] == '/') {
      area_++;
    } else {
      area_ += 2;
      search(x + 1, y, diagram, is_checked);
      search(x, y + 1, diagram, is_checked);
    }
    return;
  }

  void create_flood(const std::vector<char> &diagram,
                    std::vector<bool> &is_checked) {
    search(x_, y_, diagram, is_checked);
  }

  int get_area() { return area_ / 2; }

  bool operator<(Flood &r) { return this->x_ < r.x_; }
};

int main(int argc, char **argv) {
#if INPUT_FROM_FILE
  std::ifstream ifs("test.txt");
#endif
  std::string str;
  int y = 0;
  int min_y = 0;
  int max_y = 0;
  int w = 0;
  int h = 0;
  std::vector<char> diagram;
  std::vector<bool> is_cheked;
  std::vector<Flood> flood_vec;
  int total_area = 0;

#if INPUT_FROM_FILE
  ifs >> str;
#else
  std::cin >> str;
#endif

  for (unsigned int x = 0; x < str.size(); x++) {
    if (x > 0 && (str.at(x - 1) == '\\\\' || str.at(x - 1) == '_') &&
        str.at(x) == '\\\\') {
      y++;
      max_y = std::max(max_y, y);
    } else if (x > 0 && str.at(x - 1) == '/' &&
               (str.at(x) == '/' || str.at(x) == '_')) {
      y--;
      min_y = std::min(min_y, y);
    }
    w++;
  }

  h = max_y - min_y + 1;

  diagram.resize(w * h, ' ');
  y = -min_y;
  for (unsigned int x = 0; x < str.size(); x++) {
    if (x > 0 && (str.at(x - 1) == '\\\\' || str.at(x - 1) == '_') &&
        str.at(x) == '\\\\') {
      y++;
    }
    if (x > 0 && str.at(x - 1) == '/' &&
        (str.at(x) == '/' || str.at(x) == '_')) {
      y--;
    }
    int cidx = y * w + x;
    if (str.at(x) == '\\\\') {

      diagram[cidx] = '\\\\';
    } else if (str.at(x) == '/') {

      diagram[cidx] = '/';
    } else {
      diagram[cidx] = '_';
    }
  }

  // print(w, h, diagram);

  is_cheked.resize(diagram.size(), false);

  for (int y = 0; y < h; y++) {
    int offset = y * w;
    for (int x = 0; x < w; x++) {
      int cidx = offset + x;

      if (diagram[cidx] == '\\\\' && !is_cheked[cidx]) {
        for (int xx = x + 1; xx < w; xx++) {
          if (diagram[offset + xx] == '/') {
            Flood fl(x, y, w, h);

            fl.create_flood(diagram, is_cheked);
            flood_vec.push_back(fl);
            break;
          }
        }
      }
    }
  }

  std::sort(flood_vec.begin(), flood_vec.end());

  for (unsigned int i = 0; i < flood_vec.size(); i++) {
    total_area += flood_vec[i].get_area();
  }

  std::cout << total_area << std::endl;
  std::cout << flood_vec.size();
  for (unsigned int i = 0; i < flood_vec.size(); i++) {
    std::cout << " " << flood_vec[i].get_area();
  }
  std::cout << std::endl;
  return 0;
}