Problem p00199 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dX[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
struct XY {
  int x;
  int y;
};
vector<long long> Prime;
void SieveOfEratosthenes(long long e) // setup vector<long long>Prime(~x)
{
  bool *x = new bool[e];
  for (long long i = 0; i <= e; i++)
    x[i] = false;
  for (long long i = 2; i <= e; i++) {
    if (x[i])
      ;
    else {
      Prime.push_back(i);
      for (long long l = i; l <= e; l += i)
        x[l] = true;
    }
  }
  return;
}
int StringToInt(string a) {
  stringstream ss;
  int x;
  ss << a;
  ss >> x;
  return x;
}
string IntToString(int a) {
  stringstream ss;
  string x;
  ss << a;
  ss >> x;
  return x;
}
int GetRandom(int MIN, int MAX) {
  static int flag = 0;
  if (flag == 0) {
    srand((unsigned int)time(NULL));
    flag = 1;
  }
  return MIN + (int)(rand() * (MAX - MIN + 1.0) / (1.0 + RAND_MAX));
}
int in() {
  int x;
  cin >> x;
  return x;
}
void outl(string a) {
  cout << a << endl;
  return;
}
void outl(int a) {
  cout << a << endl;
  return;
}
void out(string a) {
  cout << a;
  return;
}
void out(int a) {
  cout << a;
  return;
}
/*
(char)'1' - (int)48 = (int)1;
???1111181
 (char)A=(int)65
 (char)a=(int)97
 */
////////////////////////////////////////////////////

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<char> c;
    rep(i, n) c.push_back('#');
    rep(i, m) {
      char now;
      cin >> now;
      bool wan = false;
      bool ok = true;
      switch (now) {
      case 'A':
        for (int l = 0; l < c.size(); l++) {
          if (c[l] == '#') {
            c[l] = 'A';
            break;
          }
        }
        break;
      case 'B':
        for (int l = c.size() - 1; l >= 0; l--) {
          if (c[l] == '#') {
            if (l - 1 == -1 || c[l - 1] != 'A') {
              if (l + 1 == c.size() || c[l + 1] != 'A') {
                ok = false;
                c[l] = 'B';
                break;
              }
            }
          }
        }
        if (ok) {
          for (int l = 0; l < c.size(); l++) {
            if (c[l] == '#') {
              c[l] = 'B';
              break;
            }
          }
        }
        break;
      case 'C':
        for (int l = 0; l < c.size(); l++) {
          if (c[l] != '#') {
            if (l + 1 < c.size() && c[l + 1] == '#') {
              c[l + 1] = 'C';
              ok = false;
              break;
            } else if (l - 1 >= 0 && c[l - 1] == '#') {
              c[l - 1] = 'C';
              ok = false;
              break;
            }
          }
        }

        if (ok) {
          int j;
          if (c.size() % 2 == 1) {
            j = (c.size() + 1) / 2;
          } else {
            j = c.size() / 2 + 1;
          }
          j--;
          c[j] = 'C';
        }
        break;
      case 'D':
        if (i == 0) {
          c[0] = 'D';
        } else {
          int ann = 0;
          int hhh = -1;
          for (int l = c.size() - 1; l >= 0; l--) {
            if (c[l] == '#') {
              int pp = 0;
              int PP = 0;
              for (int b = l; b >= 0; b--) {
                pp++;
                if (c[b] != '#') {
                  break;
                }
                if (b == 0)
                  pp = 999;
              }

              for (int b = l; b < c.size(); b++) {
                PP++;
                if (c[b] != '#') {
                  break;
                }
                if (b == c.size() - 1)
                  PP = 999;
              }
              pp = min(pp, PP);
              if (pp >= ann) {
                ann = pp;
                hhh = l;
              }
            }
          }

          c[hhh] = 'D';
        }
        break;
      }
    }

    for (int i = 0; i < c.size(); i++) {
      cout << c[i];
    }
    cout << endl;
  }
}

Error: -6
double free or corruption (out)


Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dX[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
struct XY {
  int x;
  int y;
};
vector<long long> Prime;
void SieveOfEratosthenes(long long e) // setup vector<long long>Prime(~x)
{
  bool *x = new bool[e];
  for (long long i = 0; i <= e; i++)
    x[i] = false;
  for (long long i = 2; i <= e; i++) {
    if (x[i])
      ;
    else {
      Prime.push_back(i);
      for (long long l = i; l <= e; l += i)
        x[l] = true;
    }
  }
  return;
}
int StringToInt(string a) {
  stringstream ss;
  int x;
  ss << a;
  ss >> x;
  return x;
}
string IntToString(int a) {
  stringstream ss;
  string x;
  ss << a;
  ss >> x;
  return x;
}
int GetRandom(int MIN, int MAX) {
  static int flag = 0;
  if (flag == 0) {
    srand((unsigned int)time(NULL));
    flag = 1;
  }
  return MIN + (int)(rand() * (MAX - MIN + 1.0) / (1.0 + RAND_MAX));
}
int in() {
  int x;
  cin >> x;
  return x;
}
void outl(string a) {
  cout << a << endl;
  return;
}
void outl(int a) {
  cout << a << endl;
  return;
}
void out(string a) {
  cout << a;
  return;
}
void out(int a) {
  cout << a;
  return;
}
/*
(char)'1' - (int)48 = (int)1;
???1111181
 (char)A=(int)65
 (char)a=(int)97
 */
////////////////////////////////////////////////////

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<char> c;
    rep(i, n) c.push_back('#');
    rep(i, m) {
      char now;
      cin >> now;
      bool wan = false;
      bool ok = true;
      switch (now) {
      case 'A':
        for (int l = 0; l < c.size(); l++) {
          if (c[l] == '#') {
            c[l] = 'A';
            break;
          }
        }
        break;
      case 'B':
        for (int l = c.size() - 1; l >= 0; l--) {
          if (c[l] == '#') {
            if (l - 1 == -1 || c[l - 1] != 'A') {
              if (l + 1 == c.size() || c[l + 1] != 'A') {
                ok = false;
                c[l] = 'B';
              }
            }
          }
        }
        if (ok) {
          for (int l = 0; l < c.size(); l++) {
            if (c[l] == '#') {
              c[l] = 'B';
              break;
            }
          }
        }
        break;
      case 'C':
        for (int l = 0; l < c.size(); l++) {
          if (c[l] != '#') {
            if (l - 1 >= 0 && c[l - 1] == '#') {
              c[l - 1] = 'C';
              ok = false;
              break;
            } else if (l + 1 < c.size() && c[l + 1] == '#') {
              c[l + 1] = 'C';
              ok = false;
              break;
            }
          }
        }

        if (ok) {
          int j;
          if (c.size() % 2 == 1) {
            j = (c.size() + 1) / 2;
          } else {
            j = c.size() / 2 + 1;
          }
          j--;
          c[j] = 'C';
        }
        break;
      case 'D':
        if (i == 0) {
          c[0] = 'D';
        } else {
          int ann = 0;
          int hhh = -1;
          for (int l = c.size() - 1; l >= 0; l--) {
            if (c[l] == '#') {
              int pp = 0;
              int PP = 0;
              for (int b = l; b >= 0; b--) {
                pp++;
                if (c[b] != '#') {
                  break;
                }
                if (b == 0)
                  pp = 999;
              }

              for (int b = l; b < c.size(); b++) {
                PP++;
                if (c[b] != '#') {
                  break;
                }
                if (b == c.size() - 1)
                  PP = 999;
              }
              pp = min(pp, PP);
              if (pp >= ann) {
                ann = pp;
                hhh = l;
              }
            }
          }

          c[hhh] = 'D';
        }
        break;
      }
    }

    for (int i = 0; i < c.size(); i++) {
      cout << c[i];
    }
    cout << endl;
  }
}