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

//============================================================================
// Name        : BinarySearch.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;

void BubbleSort(vector<int> &);
int BinarySearch(const vector<int> &, const vector<int> &);
void WriteVector(const vector<int> &);

void WriteVector(const vector<int> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v.at(i) << "\\n";
  }
}

void BubbleSort(vector<int> &v) {
  bool changed = true;
  while (changed) {
    changed = false;
    for (int i = v.size() - 1; 1 <= i; i--) {
      if (v.at(i) < v.at(i - 1)) {
        int tmp = v.at(i - 1);
        v.at(i - 1) = v.at(i);
        v.at(i) = tmp;
        changed = true;
      }
    }
  }
  cout << "sorted"
       << "\\n";
  WriteVector(v);
}

int BinarySearch(const vector<int> &unsorted_s, const vector<int> &sorted_t) {

  int n_common = 0;

  for (int i = 0; i < unsorted_s.size(); i++) {
    int begin = 0;
    int end = sorted_t.size();
    // cout << "searching: " << unsorted_s.at(i) << "\\n";
    while (begin < end) {
      int mid = (begin + end) / 2;

      if (sorted_t.at(mid) == unsorted_s.at(i)) {
        n_common = n_common + 1;
        // cout << "found: " << unsorted_s.at(i) << "\\n";
        break;
      } else if (sorted_t.at(mid) < unsorted_s.at(i)) {
        // cout << "case1:  begin: " << begin << ", mid: " << mid << ", end: "
        //	 << end << ", unsorted_s.at(i): " << unsorted_s.at(i) << "\\n";
        begin = mid + 1;
      } else { // sorted_t.at(mid) > unsorted_s.at(i)
        // cout << "case2:  begin: " << begin << ", mid: " << mid << ", end: "
        //	 << end << ", unsorted_s.at(i): " << unsorted_s.at(i) << "\\n";
        end = mid;
      }
    }
  }
  return n_common;
}

int main() {
  int n;
  vector<int> s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int input;
    scanf("%d", &input);
    s.push_back(input);
  }

  int m;
  vector<int> t;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int input;
    scanf("%d", &input);
    t.push_back(input);
  }

  // BubbleSort(t);

  int n_common = 0;
  n_common = BinarySearch(t, s);

  // cout << n_common << "\\n";
  printf("%d\\n", n_common);

  return 0;
}


Error: -11

Fixed submission:

//============================================================================
// Name        : BinarySearch.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;

void BubbleSort(vector<int> &);
int BinarySearch(const vector<int> &, const vector<int> &);
void WriteVector(const vector<int> &);

void WriteVector(const vector<int> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v.at(i) << "\\n";
  }
}

void BubbleSort(vector<int> &v) {
  bool changed = true;
  while (changed) {
    changed = false;
    for (int i = v.size() - 1; 1 <= i; i--) {
      if (v.at(i) < v.at(i - 1)) {
        int tmp = v.at(i - 1);
        v.at(i - 1) = v.at(i);
        v.at(i) = tmp;
        changed = true;
      }
    }
  }
  cout << "sorted"
       << "\\n";
  WriteVector(v);
}

int BinarySearch(const vector<int> &unsorted_s, const vector<int> &sorted_t) {

  int n_common = 0;

  for (int i = 0; i < unsorted_s.size(); i++) {
    int begin = 0;
    int end = sorted_t.size();
    // cout << "searching: " << unsorted_s.at(i) << "\\n";
    while (begin < end) {
      int mid = (begin + end) / 2;

      if (sorted_t.at(mid) == unsorted_s.at(i)) {
        n_common = n_common + 1;
        // cout << "found: " << unsorted_s.at(i) << "\\n";
        break;
      } else if (sorted_t.at(mid) < unsorted_s.at(i)) {
        // cout << "case1:  begin: " << begin << ", mid: " << mid << ", end: "
        //	 << end << ", unsorted_s.at(i): " << unsorted_s.at(i) << "\\n";
        begin = mid + 1;
      } else { // sorted_t.at(mid) > unsorted_s.at(i)
        // cout << "case2:  begin: " << begin << ", mid: " << mid << ", end: "
        //	 << end << ", unsorted_s.at(i): " << unsorted_s.at(i) << "\\n";
        end = mid;
      }
    }
  }
  return n_common;
}

int main() {
  int n, input;
  vector<int> s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", input);
    s.push_back(input);
  }

  int m;
  vector<int> t;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", input);
    t.push_back(input);
  }

  // BubbleSort(t);

  int n_common = 0;
  n_common = BinarySearch(t, s);

  cout << n_common << "\\n";

  return 0;
}