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

#include <iostream>
#include <vector>
#define N 36
#define M 1000
using namespace std;

int main() {
  int n, m, q, ans[N][M], a;
  string s[M], b[M], sw, li;
  vector<int> on, off, k, koff;
  while (1) {
    cin >> n >> m >> q;
    if (!n && !m && !q)
      break;
    for (int i = 0; i < q; i++)
      cin >> s[i] >> b[i];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        ans[i][j] = 1;
    for (int i = 0; i < n; i++)
      sw += '0';
    for (int i = 0; i < m; i++)
      li += '0';
    for (int i = 0; i < q; i++) {
      for (int j = 0; j < n; j++) {
        if (sw[j] == '1' && s[i][j] == '0')
          k.push_back(j);
        if (sw[j] == '0' && s[i][j] == '1')
          on.push_back(j);
        if (sw[j] == '1' && s[i][j] == '1')
          off.push_back(j);
        if (sw[j] == '0' && s[i][j] == '0')
          koff.push_back(j);
      }
      for (int j = 0; j < m; j++) {
        if (li[j] == '1' && b[i][j] == '1') {
          for (int l = 0; l < on.size(); l++)
            ans[on[l]][j] = 0;
          for (int l = 0; l < off.size(); l++)
            ans[off[l]][j] = 0;
          for (int l = 0; l < koff.size(); l++)
            ans[koff[l]][j] = 0;
        }
        if (li[j] == '1' && b[i][j] == '0') {
          for (int l = 0; l < on.size(); l++)
            ans[on[l]][j] = 0;
          for (int l = 0; l < k.size(); l++)
            ans[k[l]][j] = 0;
          for (int l = 0; l < koff.size(); l++)
            ans[koff[l]][j] = 0;
        }
        if (li[j] == '0' && b[i][j] == '1') {
          for (int l = 0; l < k.size(); l++)
            ans[k[l]][j] = 0;
          for (int l = 0; l < koff.size(); l++)
            ans[koff[l]][j] = 0;
          for (int l = 0; l < off.size(); l++)
            ans[off[l]][j] = 0;
        }
        if (li[j] == '0' && b[i][j] == '0') {
          for (int l = 0; l < on.size(); l++)
            ans[on[l]][j] = 0;
          for (int l = 0; l < k.size(); l++)
            ans[k[l]][j] = 0;
          for (int l = 0; l < off.size(); l++)
            ans[off[l]][j] = 0;
        }
      }
      k.clear();
      koff.clear();
      on.clear();
      off.clear();
      for (int j = 0; j < n; j++)
        if (s[i][j] == '1')
          sw[j] == '0' ? sw[j] = '1' : sw[j] = '0';
      for (int j = 0; j < m; j++)
        li[j] = b[i][j];
    }
    for (int i = 0; i < m; i++) {
      a = -1;
      for (int j = 0; j < n; j++)
        if (ans[j][i] == 1) {
          if (a == -1)
            a = j;
          else {
            a = -1;
            break;
          }
        }
      if (a == -1)
        cout << '?';
      else {
        if (a <= 9)
          cout << a;
        else
          cout << (char)('A' + a - 10);
      }
    }
    cout << endl;
    sw = "";
    li = "";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define Q 1010
#define N 40
using namespace std;

int main() {
  int n, m, q;
  char ans[Q];
  bool sw[N], swb[N], li[Q], fl[Q], fln[N], fle[Q];
  string s[Q], b[Q];
  vector<int> on, off, r;
  while (1) {
    cin >> n >> m >> q;
    if (!n && !m && !q)
      break;
    for (int i = 0; i < q; i++)
      cin >> s[i] >> b[i];
    for (int i = 0; i < n; i++)
      sw[i] = swb[i] = fln[i] = false;
    for (int i = 0; i < m; i++)
      ans[i] = '?', li[i] = fl[i] = fle[i] = false;
    for (int i = 0; i < q; i++) {
      for (int j = 0; j < n; j++)
        swb[j] = sw[j];
      for (int j = 0; j < n; j++)
        if (s[i][j] == '1')
          sw[j] ? sw[j] = false : sw[j] = true;
      for (int j = 0; j < n; j++) {
        if (swb[j] && sw[j])
          r.push_back(j);
        else if (swb[j] && !sw[j])
          off.push_back(j);
        else if (sw[j])
          on.push_back(j);
      }
      for (int j = 0; j < n; j++)
        if (fln[j]) {
          for (int k = 0; k < on.size(); k++)
            if (j == on[k])
              on.erase(on.begin() + k);
          for (int k = 0; k < off.size(); k++)
            if (j == off[k])
              off.erase(off.begin() + k);
          for (int k = 0; k < r.size(); k++)
            if (j == r[k])
              r.erase(r.begin() + k);
        }
      if (on.size() == 1)
        for (int j = 0; j < m; j++)
          if (!li[j] && b[i][j] == '1') {
            if (!fl[j]) {
              if (on[0] <= 9)
                ans[j] = on[0] + '0';
              else
                ans[j] = on[0] + 'A' - 10;
              fl[j] = fln[j] = true;
            } else
              ans[j] = '?', fle[j] = true;
          }
      if (off.size() == 1)
        for (int j = 0; j < m; j++)
          if (li[j] && b[i][j] == '0') {
            if (!fl[j]) {
              if (off[0] <= 9)
                ans[j] = off[0] + '0';
              else
                ans[j] = off[0] + 'A' - 10;
              fl[j] = fln[j] = true;
            } else
              ans[j] = '?', fle[j] = true;
          }
      if (r.size() == 1)
        for (int j = 0; j < m; j++)
          if (li[j] && b[i][j] == '1') {
            if (!fl[j]) {
              if (r[0] <= 9)
                ans[j] = r[0] + '0';
              else
                ans[j] = r[0] + 'A' - 10;
              fl[j] = fln[j] = true;
            } else
              ans[j] = '?', fle[j] = true;
          }
      for (int j = 0; j < m; j++)
        b[i][j] == '1' ? li[j] = true : li[j] = false;
      on.clear();
      off.clear();
      r.clear();
    }
    int cnt = 0, a;
    for (int i = 0; i < n; i++)
      if (!fln[i])
        cnt++, a = i;
    if (cnt == 1)
      for (int i = 0; i < m; i++)
        if (ans[i] == '?' && !fle[i]) {
          if (a <= 9)
            ans[i] = a + '0';
          else
            ans[i] = a + 'A' - 10;
        }
    for (int i = 0; i < m; i++)
      cout << ans[i];
    cout << endl;
  }
  return 0;
}