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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define ppins(a, b, c, d) PP(P(a, b), P(c, d))
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<bool, bool> BI;

bool judge(string s1, string s2) {

  rep(i, 8) {
    if (s1[i] == '?')
      continue;
    if (s1[i] != s2[i])
      return false;
  }

  for (int i = 8; i < 16; i++) {
    if (s1[i] == '?')
      continue;
    if (s1[i] != s2[i])
      return false;
  }

  return true;
}

vector<char> rules;
vector<string> sourceAnDdestination;
int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0)
      break;
    char Message[1024][52];
    rules.clear(), sourceAnDdestination.clear();
    cin.ignore();

    rep(i, n) {
      string line;
      string so, de, soandde;
      cin >> line >> so >> de;
      soandde = so + de;
      rules.pb(line[0]), sourceAnDdestination.pb(soandde);
    }

    BI legal[m];
    rep(i, m) legal[i] = BI(false, false);

    rep(i, m) {
      string a, b, c;
      cin >> a >> b >> c;
      string aandb = a + b;
      sourceAnDdestination.pb(aandb);
      strcpy(Message[i], c.c_str());
    }

    int cnt = 0;

    for (int i = n - 1; i >= 0; i--) {

      if (rules[i] == 'p') {

        rep(j, m) {

          if (legal[j].S)
            continue;
          if (judge(sourceAnDdestination[i], sourceAnDdestination[n + j])) {
            legal[j] = BI(true, true);
            cnt++;
          }
        }

      } else {
        rep(j, m) {
          if (legal[j].S)
            continue;
          if (judge(sourceAnDdestination[i], sourceAnDdestination[n + j])) {
            legal[j] = BI(false, true);
          }
        }
      }
    }

    cout << cnt << endl;
    rep(i, m) {
      if (legal[i].F) {
        cout << sourceAnDdestination[i + n].substr(0, 8) << " "
             << sourceAnDdestination[i + n].substr(8, 8) << " " << Message[i]
             << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define ppins(a, b, c, d) PP(P(a, b), P(c, d))
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<bool, bool> BI;

void MySplit(string message, char *c, string *so, string *de) {
  stringstream ss;
  ss << message;
  string A;
  ss >> A >> *so >> *de;
  *c = A[0];
}

void MySplit2(string line, string *a, string *b, string *c) {
  stringstream ss;
  ss << line;
  ss >> *a >> *b >> *c;
}

bool judge(string s1, string s2) {

  rep(i, 8) {
    if (s1[i] == '?')
      continue;
    if (s1[i] != s2[i])
      return false;
  }

  for (int i = 8; i < 16; i++) {
    if (s1[i] == '?')
      continue;
    if (s1[i] != s2[i])
      return false;
  }

  return true;
}

vector<char> rules;
vector<string> sourceAnDdestination;
int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0)
      break;
    char Message[52][1024];
    rules.clear(), sourceAnDdestination.clear();
    cin.ignore();

    rep(i, n) {
      string line;
      getline(cin, line);
      char c;
      string so, de;
      MySplit(line, &c, &so, &de);
      string soandde = so + de;
      rules.pb(c), sourceAnDdestination.pb(soandde);
    }

    BI legal[m];
    rep(i, m) legal[i] = BI(false, false);

    rep(i, m) {
      string line;
      getline(cin, line);
      string a, b, c;
      MySplit2(line, &a, &b, &c);
      string aandb = a + b;
      sourceAnDdestination.pb(aandb);
      strcpy(Message[i], c.c_str());
    }

    int cnt = 0;

    for (int i = n - 1; i >= 0; i--) {

      if (rules[i] == 'p') {

        rep(j, m) {

          if (legal[j].S)
            continue;
          if (judge(sourceAnDdestination[i], sourceAnDdestination[n + j])) {
            legal[j] = BI(true, true);
            cnt++;
          }
        }

      } else {
        rep(j, m) {
          if (legal[j].S)
            continue;
          if (judge(sourceAnDdestination[i], sourceAnDdestination[n + j])) {
            legal[j] = BI(false, true);
          }
        }
      }
    }

    cout << cnt << endl;
    rep(i, m) {
      if (legal[i].F) {
        cout << sourceAnDdestination[i + n].substr(0, 8) << " "
             << sourceAnDdestination[i + n].substr(8, 8) << " " << Message[i]
             << endl;
      }
    }
  }
  return 0;
}