Problem p03248 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)

using namespace std;
typedef pair<int, int> P;
string s;
vector<P> ans;

int main() {
  cin >> s;
  int n = (int)s.size();
  bool flg = true;
  if (s[n - 1] != '0') {
    flg = false;
  }
  if (s[0] == '0') {
    flg = false;
  }
  REP(i, n / 2) {
    if (s[i] != s[n - i - 2]) {
      flg = false;
    }
  }
  if (flg) {
    ans.push_back(P(1, 2));
    int v = 2;
    if (n == 2) {
      goto COR;
    }
    FOR(i, 1, (n - 1) / 2) {
      if (s[i] == '0') {
        ans.push_back(P(v, i + 2));
      } else {
        ans.push_back(P(v, i + 2));
        v = i + 2;
      }
    }
    if (n & 1) {
      for (auto &ite : ans) {
        int &f = ite.first, &s = ite.second;
        if (f == v) {
          f = n;
        } else if (f > v) {
          f--;
        }
        if (s == v) {
          s = n;
        } else if (s > v) {
          s--;
        }
      }
      REP(i, n / 2) {
        P p = ans[i];
        int f = p.first, s = p.second;
        if (f < n) {
          f = n - f;
        }
        if (s < n) {
          s = n - s;
        }
        ans.push_back(P(f, s));
      }
    } else {
      if (s[n / 2 - 1] == '1') {
        for (auto ite : ans) {
          ans.push_back(P(n + 1 - ite.first, n + 1 - ite.second));
        }
        ans.push_back(P(v, n + 1 - v));
      } else {
        for (auto &ite : ans) {
          int &f = ite.first, &s = ite.second;
          if (f == v) {
            f = n / 2;
          } else if (f > v) {
            f--;
          }
          if (s == v) {
            s = n / 2;
          } else if (s > v) {
            s--;
          }
        }
        REP(i, n / 2 - 1) {
          P p = ans[i];
          int f = p.first, s = p.second;
          if (f < n / 2) {
            f = n - f;
          }
          if (s < n / 2) {
            s = n - s;
          }
          ans.push_back(P(f, s));
        }
        ans.push_back(P(n / 2, n));
      }
    }
  COR:
    for (auto ite : ans) {
      cout << ite.first << " " << ite.second << endl;
    }
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)

using namespace std;
typedef pair<int, int> P;
string s;
vector<P> ans;

int main() {
  cin >> s;
  int n = (int)s.size();
  bool flg = true;
  if (s[n - 1] != '0') {
    flg = false;
  }
  if (s[0] == '0') {
    flg = false;
  }
  REP(i, n / 2) {
    if (s[i] != s[n - i - 2]) {
      flg = false;
    }
  }
  if (flg) {
    ans.push_back(P(1, 2));
    int v = 2;
    if (n == 2) {
      goto COR;
    }
    FOR(i, 1, (n - 1) / 2) {
      if (s[i] == '0') {
        ans.push_back(P(v, i + 2));
      } else {
        ans.push_back(P(v, i + 2));
        v = i + 2;
      }
    }
    if (n & 1) {
      for (auto &ite : ans) {
        int &f = ite.first, &s = ite.second;
        if (f == v) {
          f = n;
        } else if (f > v) {
          f--;
        }
        if (s == v) {
          s = n;
        } else if (s > v) {
          s--;
        }
      }
      REP(i, n / 2) {
        P p = ans[i];
        int f = p.first, s = p.second;
        if (f < n) {
          f = n - f;
        }
        if (s < n) {
          s = n - s;
        }
        ans.push_back(P(f, s));
      }
    } else {
      if (s[n / 2 - 1] == '1') {
        REP(i, n / 2 - 1) {
          P p = ans[i];
          ans.push_back(P(n + 1 - p.first, n + 1 - p.second));
        }
        ans.push_back(P(v, n + 1 - v));
      } else {
        for (auto &ite : ans) {
          int &f = ite.first, &s = ite.second;
          if (f == v) {
            f = n / 2;
          } else if (f > v) {
            f--;
          }
          if (s == v) {
            s = n / 2;
          } else if (s > v) {
            s--;
          }
        }
        REP(i, n / 2 - 1) {
          P p = ans[i];
          int f = p.first, s = p.second;
          if (f < n / 2) {
            f = n - f;
          }
          if (s < n / 2) {
            s = n - s;
          }
          ans.push_back(P(f, s));
        }
        ans.push_back(P(n / 2, n));
      }
    }
  COR:
    for (auto ite : ans) {
      cout << ite.first << " " << ite.second << endl;
    }
  } else {
    cout << -1 << endl;
  }
  return 0;
}