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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
#define ll long long
#define ld long double
#define P pair<ll, ll>
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define FORa(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FORr(i, m, n) for (ll i = n; i >= (ll)m; i--)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define FORs(i, st) for (auto i : st)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define VEC2(x, a, b)                                                          \\
  vector<vector<ll>> x = vector<vector<ll>>(a, vector<ll>(b, 0))
#define sortAl(v) sort(v.begin(), v.end())
#define sortAlr(v)                                                             \\
  sort(v.begin(), v.end());                                                    \\
  reverse(v.begin(), v.end())
#define cinv(v, n, m) FOR(i, n, m) cin >> v.at(i)
#define coutv(v, n, m)                                                         \\
  FOR(i, n, m) cout << v.at(i) << " ";                                         \\
  cout << endl
#define cout(n) cout << fixed << setprecision(n)

char int_to_char(ll x) {
  if (x == -1)
    return '?';
  else if (x <= 9)
    return '0' + x;
  else
    return 'A' + x - 10;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m, q;
  cin >> n >> m >> q;
  while (n != 0) {

    vector<string> sw(q + 1);
    vector<string> light(q + 1);

    FOR(i, 0, n) { sw[0].push_back('0'); }
    FOR(i, 0, m) { light[0].push_back('0'); }

    FOR(i, 1, q + 1) { cin >> sw[i] >> light[i]; }
    q++;

    FOR(i, 1, q) {
      FOR(j, 0, n) {
        if (sw[i - 1][j] == '1' && sw[i][j] == '1')
          sw[i][j] = '0';
        else if (sw[i - 1][j] == '0' && sw[i][j] == '1')
          sw[i][j] = '1';
        else if (sw[i - 1][j] == '1' && sw[i][j] == '0')
          sw[i][j] = '1';
      }
    }

    vector<vector<ll>> v(m, vector<ll>(n, 1));

    FOR(i, 0, q) {   // クエリごと
      FOR(j, 0, m) { // あかりごと
        FOR(k, 0, n) {
          if (sw[i][k] == '0' && light[i][j] == '1')
            v[j][k] = 0;
          if (sw[i][k] == '1' && light[i][j] == '0')
            v[j][k] = 0;
        }
      }
    }

    vector<ll> ans;
    FOR(i, 0, m) {
      ll tmp = 0;
      ll po = 0;
      FOR(j, 0, n) {
        tmp += v[i][j];
        if (v[i][j] == 1)
          po = j;
      }
      if (tmp == 1)
        ans.push_back(po);
      else
        ans.push_back(-1);
    }

    FOR(i, 0, m) { cout << int_to_char(ans[i]); }
    cout << endl;

    cin >> n >> m >> q;
  }
}


Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
#define ll long long
#define ld long double
#define P pair<ll, ll>
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define FORa(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FORr(i, m, n) for (ll i = n; i >= (ll)m; i--)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define FORs(i, st) for (auto i : st)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define VEC2(x, a, b)                                                          \\
  vector<vector<ll>> x = vector<vector<ll>>(a, vector<ll>(b, 0))
#define sortAl(v) sort(v.begin(), v.end())
#define sortAlr(v)                                                             \\
  sort(v.begin(), v.end());                                                    \\
  reverse(v.begin(), v.end())
#define cinv(v, n, m) FOR(i, n, m) cin >> v.at(i)
#define coutv(v, n, m)                                                         \\
  FOR(i, n, m) cout << v.at(i) << " ";                                         \\
  cout << endl
#define cout(n) cout << fixed << setprecision(n)

char int_to_char(ll x) {
  if (x == -1)
    return '?';
  else if (x <= 9)
    return '0' + x;
  else
    return 'A' + x - 10;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m, q;
  cin >> n >> m >> q;
  while (n != 0) {

    vector<string> sw(q + 1);
    vector<string> light(q + 1);
    vector<ll> ans(m, -1);

    FOR(i, 0, n) { sw[0].push_back('0'); }
    FOR(i, 0, m) { light[0].push_back('0'); }

    FOR(i, 1, q + 1) { cin >> sw[i] >> light[i]; }
    q++;

    FOR(i, 1, q) {
      FOR(j, 0, n) {
        if (sw[i - 1][j] == '1' && sw[i][j] == '1')
          sw[i][j] = '0';
        else if (sw[i - 1][j] == '0' && sw[i][j] == '1')
          sw[i][j] = '1';
        else if (sw[i - 1][j] == '1' && sw[i][j] == '0')
          sw[i][j] = '1';
      }
    }

    FOR(i, 0, q) {
      FOR(j, i + 1, q) {
        ll xor_1 = 0;
        ll x1_po = 0;
        ll xor_0 = 0;
        ll x0_po = 0;
        ll and_ = 0;
        ll a_po = 0;
        FOR(k, 0, n) {
          if (sw[i][k] == '1' && sw[j][k] == '1') {
            and_++;
            a_po = k;
          }
          if ((sw[i][k] == '0' && sw[j][k] == '1')) {
            xor_1++;
            x1_po = k;
          }
          if ((sw[i][k] == '1' && sw[j][k] == '0')) {
            xor_0++;
            x0_po = k;
          }
        }

        FOR(k, 0, m) {
          if (and_ == 1) {
            if (light[i][k] == '1' && light[j][k] == '1')
              ans[k] = a_po;
          }
          if (xor_1 == 1) {
            if (light[i][k] == '0' && light[j][k] == '1')
              ans[k] = x1_po;
          }
          if (xor_0 == 1) {
            if (light[i][k] == '1' && light[j][k] == '0')
              ans[k] = x0_po;
          }
        }
      }
    }
    vector<ll> used(m);
    ll f = 0;
    FOR(i, 0, m) {
      if (ans[i] != -1) {
        if (used[ans[i]] == 0)
          f++;
        used[ans[i]] = 1;
      }
    }

    if (f == n - 1) {
      FOR(i, 0, m) {
        if (used[i] == 0) {
          FOR(j, 0, m) {
            if (ans[j] == -1) {
              ans[j] = i;
            }
          }
          break;
        }
      }
    }

    FOR(i, 0, m) { cout << int_to_char(ans[i]); }
    cout << endl;

    cin >> n >> m >> q;
  }
}