prithivMLmods/Coder-Stat huggingface dataset:
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
typedef struct Node {
  int key;
  struct Node *prev;
  struct Node *next;
} Node;

Node *head;

void initNode() {
  head = (Node *)malloc(sizeof(Node));
  head->prev = head;
  head->next = head;
}

void insertFirst(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = head->next;
  head->next->prev = x;
  x->prev = head;
  head->next = x;
}

Node *listSearch(int key) {
  Node *current = head->next;
  while (current != head && current->key != key) {
    current = current->next;
  }
  return current;
}

void deleteFirst() {
  Node *t = head->next;
  head->next = t->next;
  t->next->prev = head;
  free(t);
}

void deleteLast() {
  Node *t = head->prev;
  head->prev = t->prev;
  t->prev->next = head;
  free(t);
}

void deleteNode(int key) {
  Node *t = listSearch(key);
  t->prev->next = t->next;
  t->next->prev = t->prev;
  free(t);
}

int main() {
  initNode();
  int n;
  scanf("%d", &n);
  char com[20];
  int x;
  for (int i = 0; i < n; i++) {
    scanf("%s", com);
    if (com[6] == '\0') {
      scanf("%d", &x);
      if (com[0] == 'i') {
        insertFirst(x);
      } else if (com[0] == 'd') {
        deleteNode(x);
      }
    } else {
      if (com[6] == 'F') {
        deleteFirst();
      } else if (com[6] == 'L') {
        deleteLast();
      }
    }
  }
  Node *temp = head->next;
  int isf = 0;
  while (temp != head) {
    if (isf++ > 0)
      printf(" ");
    printf("%d", temp->key);
    temp = temp->next;
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
typedef struct Node {
  int key;
  struct Node *prev;
  struct Node *next;
} Node;

Node *head;

void initNode() {
  head = (Node *)malloc(sizeof(Node));
  head->prev = head;
  head->next = head;
}

void insertFirst(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = head->next;
  head->next->prev = x;
  x->prev = head;
  head->next = x;
}

Node *listSearch(int key) {
  Node *current = head->next;
  while (current != head && current->key != key) {
    current = current->next;
  }
  return current;
}

void deleteFirst() {
  Node *t = head->next;
  head->next = t->next;
  t->next->prev = head;
  free(t);
}

void deleteLast() {
  Node *t = head->prev;
  head->prev = t->prev;
  t->prev->next = head;
  free(t);
}

void deleteNode(int key) {
  Node *t = listSearch(key);
  if (t == head)
    return;
  t->prev->next = t->next;
  t->next->prev = t->prev;
  free(t);
}

int main() {
  initNode();
  int n;
  scanf("%d", &n);
  char com[20];
  int x;
  for (int i = 0; i < n; i++) {
    scanf("%s", com);
    if (com[6] == '\0') {
      scanf("%d", &x);
      if (com[0] == 'i') {
        insertFirst(x);
      } else if (com[0] == 'd') {
        deleteNode(x);
      }
    } else {
      if (com[6] == 'F') {
        deleteFirst();
      } else if (com[6] == 'L') {
        deleteLast();
      }
    }
  }
  Node *temp = head->next;
  int isf = 0;
  while (temp != head) {
    if (isf++ > 0)
      printf(" ");
    printf("%d", temp->key);
    temp = temp->next;
  }
  printf("\n");
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  deque<int> d;
  int n;
  string cmd;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      int x;
      cin >> x;
      d.push_front(x);
    } else if (cmd == "delete") {
      int x;
      cin >> x;
      for (deque<int>::iterator i = d.begin(); i != d.end(); i++) {
        if (*i == x) {
          d.erase(i);
          break;
        }
      }
    } else if (cmd == "deleteFirst") {
      d.pop_front();
    } else {
      d.pop_back();
    }
  }
  for (deque<int>::iterator i = d.begin(); i != d.end() - 1; i++) {
    cout << *i << " ";
  }
  cout << d.back() << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

  deque<int> d;
  int n;
  string cmd;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      int x;
      cin >> x;
      d.push_front(x);
    } else if (cmd == "delete") {
      int x;
      cin >> x;
      for (deque<int>::iterator i = d.begin(); i != d.end(); i++) {
        if (*i == x) {
          d.erase(i);
          break;
        }
      }
    } else if (cmd == "deleteFirst") {
      d.pop_front();
    } else {
      d.pop_back();
    }
  }
  for (deque<int>::iterator i = d.begin(); i != d.end() - 1; i++) {
    cout << *i << " ";
  }
  cout << d.back() << endl;

  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  stack<int> S1;
  stack<pair<int, int>> S2;
  char ch;
  int sum = 0;
  for (int i = 0; cin >> ch; ++i) {
    if (ch == '\\')
      S1.push(i);
    else if (ch == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      sum += i - j;
      int a = i - j;
      while (S2.size() > 0 && S2.top().first > j) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(j, a));
    }
  }

  vector<int> ans;
  int t = 0;
  while (S2.size() > 0) {
    ans.push_back(S2.top().second);
    S2.top();
  }
  reverse(ans.begin(), ans.end());
  cout << sum << endl;
  cout << ans.size();
  for (int i = 0; i < ans.size(); i++) {
    cout << " ";
    t += ans[i];
    cout << ans[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  stack<int> S1;
  stack<pair<int, int>> S2;
  char ch;
  int sum = 0;
  for (int i = 0; cin >> ch; ++i) {
    if (ch == '\\')
      S1.push(i);
    else if (ch == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      sum += i - j;
      int a = i - j;
      while (S2.size() > 0 && S2.top().first > j) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(j, a));
    }
  }

  vector<int> ans;
  int t = 0;
  while (S2.size() > 0) {
    ans.push_back(S2.top().second);
    S2.pop();
  }
  reverse(ans.begin(), ans.end());
  cout << sum << endl;
  cout << ans.size();
  for (int i = 0; i < ans.size(); i++) {
    cout << " ";
    t += ans[i];
    cout << ans[i];
  }
  cout << endl;
  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <stack>
using namespace std;

int main() {
  int total_area = 0;

  stack<int> s1;
  stack<pair<int, int>> s2;

  char c;
  int i = 0;
  int ip, area;
  for (scanf("%c", &c); c != '\n'; scanf("%c", &c), ++i) {
    switch (c) {
    case '\\':
      s1.push(i);
      break;
    case '_':
      break;
    case '/':
      ip = s1.top();
      s1.pop();
      area = i - ip;
      total_area += area;
      while (s2.empty() == false && s2.top().first > ip) {
        area += s2.top().second;
        s2.pop();
      }
      s2.push(make_pair(ip, area));
      break;
    }
  }

  int s = s2.size();
  int a[s];
  for (int i = s - 1; i >= 0; --i) {
    a[i] = s2.top().second;
    s2.pop();
  }

  printf("%d\n", total_area);
  printf("%d", s);
  for (int i = 0; i < s; ++i)
    printf(" %d", a[i]);
  printf("\n");

  return 0;
}

Fixed submission:
#include <cstdio>
#include <stack>
using namespace std;

int main() {
  int total_area = 0;

  stack<int> s1;
  stack<pair<int, int>> s2;

  char c;
  int i = 0;
  int ip, area;
  for (scanf("%c", &c); c != '\n'; scanf("%c", &c), ++i) {
    switch (c) {
    case '\\':
      s1.push(i);
      break;
    case '_':
      break;
    case '/':
      if (s1.empty())
        break;
      ip = s1.top();
      s1.pop();
      area = i - ip;
      total_area += area;
      while (s2.empty() == false && s2.top().first > ip) {
        area += s2.top().second;
        s2.pop();
      }
      s2.push(make_pair(ip, area));
      break;
    }
  }

  int s = s2.size();
  int a[s];
  for (int i = s - 1; i >= 0; --i) {
    a[i] = s2.top().second;
    s2.pop();
  }

  printf("%d\n", total_area);
  printf("%d", s);
  for (int i = 0; i < s; ++i)
    printf(" %d", a[i]);
  printf("\n");

  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define UP '/'
#define DOWN '\\'
#define FLAT '_'
#define END 20001

int calc_area(int i, int j, const string *input) {
  int area = 0, d = 0;
  while (i < j) {
    if ((*input)[i] == UP)
      area += d--;
    else if ((*input)[i] == DOWN)
      area += d++;
    else
      area += d;
    i++;
  }
  return area;
}

int main(void) {
  vector<int> areas, heights;
  string input;
  cin >> input;

  int h = 0;
  heights.push_back(0);
  for (auto x : input) {
    if (x == DOWN)
      h--;
    else if (x == UP)
      h++;
    heights.push_back(h);
  }
  heights.push_back(END);

  int i = 0, k = 0, total = 0;
  while (heights[i] != END) {
    int j = i + 1;
    while (heights[j] != END && heights[j] < heights[i])
      j++;
    if (heights[i] == heights[j]) {
      int area = calc_area(i, j, &input);
      if (area > 0) {
        total += area;
        areas.push_back(area);
        k++;
      }
      i = j;
    } else
      i++;
  }

  printf("%d\n%d ", total, k);
  for (int i = 0; i < k - 1; i++)
    printf("%d ", areas[i]);
  printf("%d\n", areas[k - 1]);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define UP '/'
#define DOWN '\\'
#define FLAT '_'
#define END 20001

int calc_area(int i, int j, const string *input) {
  int area = 0, d = 0;
  while (i < j) {
    if ((*input)[i] == UP)
      area += d--;
    else if ((*input)[i] == DOWN)
      area += d++;
    else
      area += d;
    i++;
  }
  return area;
}

int main(void) {
  vector<int> areas, heights;
  string input;
  cin >> input;

  int h = 0;
  heights.push_back(0);
  for (auto x : input) {
    if (x == DOWN)
      h--;
    else if (x == UP)
      h++;
    heights.push_back(h);
  }
  heights.push_back(END);

  int i = 0, k = 0, total = 0;
  while (heights[i] != END) {
    int j = i + 1;
    while (heights[j] != END && heights[j] < heights[i])
      j++;
    if (heights[i] == heights[j]) {
      int area = calc_area(i, j, &input);
      if (area > 0) {
        total += area;
        areas.push_back(area);
        k++;
      }
      i = j;
    } else
      i++;
  }

  printf("%d\n%d", total, k);
  if (k > 0)
    for (int i = 0; i < k; i++)
      printf(" %d", areas[i]);
  puts("");

  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
using namespace std;

int main() {
  stack<char> sc;
  int lim, m = 0, ds = 0, i = 0, k = 0, SS = 0;
  int s[1000];
  int rs[1000];
  char c;
  while (cin >> c) {
    sc.push(c);
    if (m == 0) {
      if (c == '\\')
        m++;
    } else {
      if (c == '/')
        m--;
      else if (c == '\\')
        m++;
    }
    ds = ds + m;
    if ((ds != 0) && (m == 0)) {
      s[i++] = ds;
      ds = 0;
      lim = sc.size();
      for (int j = 0; j < lim; j++)
        sc.pop();
    }
  }
  m = 0;
  ds = 0;
  lim = sc.size();
  for (int j = 0; j < lim; j++) {
    c = sc.top();
    sc.pop();
    if (m == 0) {
      if (c == '/')
        m++;
    } else {
      if (c == '\\')
        m--;
      else if (c == '/')
        m++;
    }
    ds = ds + m;
    if ((ds != 0) && (m == 0)) {
      rs[k++] = ds;
      ds = 0;
    }
  }
  for (int j = 0; j < i; j++)
    SS = SS + s[j];
  for (int j = 0; j < k; j++)
    SS = SS + rs[j];
  cout << SS << endl;
  cout << i + k;
  for (int j = 0; j < i; j++)
    cout << ' ' << s[j];
  for (int j = k - 1; j >= 0; j--)
    cout << ' ' << rs[j];
  cout << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
using namespace std;

int main() {
  stack<char> sc;
  int lim, m = 0, ds = 0, i = 0, k = 0, SS = 0;
  int s[10000];
  int rs[10000];
  char c;
  while (cin >> c) {
    sc.push(c);
    if (m == 0) {
      if (c == '\\')
        m++;
    } else {
      if (c == '/')
        m--;
      else if (c == '\\')
        m++;
    }
    ds = ds + m;
    if ((ds != 0) && (m == 0)) {
      s[i++] = ds;
      ds = 0;
      lim = sc.size();
      for (int j = 0; j < lim; j++)
        sc.pop();
    }
  }
  m = 0;
  ds = 0;
  lim = sc.size();
  for (int j = 0; j < lim; j++) {
    c = sc.top();
    sc.pop();
    if (m == 0) {
      if (c == '/')
        m++;
    } else {
      if (c == '\\')
        m--;
      else if (c == '/')
        m++;
    }
    ds = ds + m;
    if ((ds != 0) && (m == 0)) {
      rs[k++] = ds;
      ds = 0;
    }
  }
  for (int j = 0; j < i; j++)
    SS = SS + s[j];
  for (int j = 0; j < k; j++)
    SS = SS + rs[j];
  cout << SS << endl;
  cout << i + k;
  for (int j = 0; j < i; j++)
    cout << ' ' << s[j];
  for (int j = k - 1; j >= 0; j--)
    cout << ' ' << rs[j];
  cout << endl;
  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<cmath>
using namespace std;

int main(void) {
  int i = 0, sum = 0, k = 0, a, b;
  string s;
  stack<int> S;
  stack<pair<int, int>> A;
  cin >> s;
  while (i != s.size()) {
    switch (s[i]) {
    case '\\':
      S.push(i);
      break;
    case '_':
      i++;
      continue;
    case '/':
      if (!S.empty()) {
        sum += i - S.top();
        A.push(make_pair(S.top(), i - S.top()));
        S.pop();
      }
    }
    i++;
  }
  cout << sum << endl;
  vector<pair<int, int>> V;
  for (; !A.empty();) {
    if (V.empty()) {
      V.push_back(make_pair(A.top().first, A.top().second));
      A.pop();
    } else {
      if (A.top().first > V[k].first &&
          A.top().first + A.top().second < V[k].second + V[k].first) {
        V[k].second += A.top().second;
        A.pop();
      } else {
        k++;
        V.push_back(make_pair(A.top().first, A.top().second));
        A.pop();
      }
    }
  }
  cout << k + 1;
  i = 0;
  for (int j = k; j >= 0; j--) {
    cout << " " << V[j].second;
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<cmath>
using namespace std;

int main(void) {
  int i = 0, sum = 0, k = 0, a, b;
  string s;
  stack<int> S;
  stack<pair<int, int>> A;
  cin >> s;
  while (i != s.size()) {
    switch (s[i]) {
    case '\\':
      S.push(i);
      break;
    case '_':
      i++;
      continue;
    case '/':
      if (!S.empty()) {
        sum += i - S.top();
        A.push(make_pair(S.top(), i - S.top()));
        S.pop();
      }
    }
    i++;
  }
  cout << sum << endl;
  vector<pair<int, int>> V;
  for (; !A.empty();) {
    if (V.empty()) {
      V.push_back(make_pair(A.top().first, A.top().second));
      A.pop();
    } else {
      if (A.top().first > V[k].first &&
          A.top().first + A.top().second < V[k].second + V[k].first) {
        V[k].second += A.top().second;
        A.pop();
      } else {
        k++;
        V.push_back(make_pair(A.top().first, A.top().second));
        A.pop();
      }
    }
  }
  if (!V.empty()) {
    cout << k + 1;
    i = 0;
    for (int j = k; j >= 0; j--) {
      cout << " " << V[j].second;
    }
    cout << endl;
  } else
    cout << k << endl;
  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int top = 0, c[10000], area[10000][2] = {};

void push(int x) {
  top++;
  c[top] = x;
}

int pop() {
  top--;
  return c[top + 1];
}

int main() {

  int sum = 0, cnt = 0;
  char ch;

  for (int i = 0; cin >> ch; i++) {
    if (ch == '\\') {
      push(i);
    } else if (ch == '/' && top > 0) {
      int j = pop(), tmp;
      sum += i - j;
      tmp = i - j;
      while (cnt > 0 && area[cnt][1] > j) {
        tmp += area[cnt][0];
        cnt--;
      }
      cnt++;
      area[cnt][0] = tmp;
      area[cnt][1] = j;
    }
  }

  cout << sum << endl;
  cout << cnt;
  for (int i = 1; i <= cnt; i++)
    cout << " " << area[i][0];
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int top = 0, c[20000], area[20000][2] = {};

void push(int x) {
  top++;
  c[top] = x;
}

int pop() {
  top--;
  return c[top + 1];
}

int main() {

  int sum = 0, cnt = 0;
  char ch;

  for (int i = 0; cin >> ch; i++) {
    if (ch == '\\') {
      push(i);
    } else if (ch == '/' && top > 0) {
      int j = pop(), tmp;
      sum += i - j;
      tmp = i - j;
      while (cnt > 0 && area[cnt][1] > j) {
        tmp += area[cnt][0];
        cnt--;
      }
      cnt++;
      area[cnt][0] = tmp;
      area[cnt][1] = j;
    }
  }

  cout << sum << endl;
  cout << cnt;
  for (int i = 1; i <= cnt; i++)
    cout << " " << area[i][0];
  cout << endl;

  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>

using namespace std;

int main() {
  char s;
  int N;
  stack<int> S1;
  stack<pair<int, int>> S2;
  int sum = 0;
  int ans[1000];
  for (int i = 0; cin >> s; i++) {
    if (s == '\\') {
      S1.push(i);
    } else if (s == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      int a = i - j;
      sum += a;
      while (S2.size() > 0 && j < S2.top().first) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(i, a));
      //	cout << S2.top().first <<"    " << S2.top().second << "    "
      //<<sum<< endl;
    } else if (s == '\n') {
      break;
    }
  }

  N = S2.size();

  for (int i = 0; i < N; i++) {
    ans[i] = S2.top().second;
    S2.pop();
  }

  cout << sum << endl;
  cout << N;
  for (int i = N - 1; i >= 0; i--) {
    cout << ' ' << ans[i];
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>

using namespace std;

int main() {
  char s;
  int N;
  stack<int> S1;
  stack<pair<int, int>> S2;
  int sum = 0;
  int ans[20000];
  for (int i = 0; cin >> s; i++) {
    if (s == '\\') {
      S1.push(i);
    } else if (s == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      int a = i - j;
      sum += a;
      while (S2.size() > 0 && j < S2.top().first) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(i, a));
      //	cout << S2.top().first <<"    " << S2.top().second << "    "
      //<<sum<< endl;
    } else if (s == '\n') {
      break;
    }
  }

  N = S2.size();

  for (int i = 0; i < N; i++) {
    ans[i] = S2.top().second;
    S2.pop();
  }

  cout << sum << endl;
  cout << N;
  for (int i = N - 1; i >= 0; i--) {
    cout << ' ' << ans[i];
  }
  cout << endl;

  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int out(vector<int> &A, int n) {
  int i;
  for (i = 0; i <= n - 2; i++) {
    cout << A[i] << " ";
  }
  cout << A[n - 1] << endl;
}

int main() {
  ios::sync_with_stdio(false);
  vector<char> C_list;
  char c;
  while (cin >> c) {
    C_list.push_back(c);
  }

  vector<int> A_list;
  int n = C_list.size();
  int i = 0;
  while (i < n) {
    char s = C_list[i];
    if (s == '\\') {
      int j = i;
      int A2 = 1;
      int height = -1;
      while (height < 0) {
        j++;
        if (j == n) {
          break;
        }
        char s2 = C_list[j];
        if (s2 == '/') {
          A2 += (-2 * height - 1);
          height += 1;
        } else if (s2 == '\\') {
          A2 += (-2 * height + 1);
          height -= 1;
        } else {
          A2 += (-2 * height);
        }
      }
      if (height == 0) {
        A_list.push_back(A2 / 2);
        i = j;
      }
    }
    i++;
  }
  int A = accumulate(A_list.begin(), A_list.end(), 0);
  cout << A << endl;
  int m = A_list.size();
  cout << m << " ";
  out(A_list, m);

  return 0;
}

Fixed submission:
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int out(vector<int> &A, int n) {
  int i;
  for (i = 0; i <= n - 2; i++) {
    cout << A[i] << " ";
  }
  cout << A[n - 1] << endl;
}

int main() {
  ios::sync_with_stdio(false);
  vector<char> C_list;
  char c;
  while (cin >> c) {
    C_list.push_back(c);
  }

  vector<int> A_list;
  int n = C_list.size();
  int i = 0;
  while (i < n) {
    char s = C_list[i];
    if (s == '\\') {
      int j = i;
      int A2 = 1;
      int height = -1;
      while (height < 0) {
        j++;
        if (j == n) {
          break;
        }
        char s2 = C_list[j];
        if (s2 == '/') {
          A2 += (-2 * height - 1);
          height += 1;
        } else if (s2 == '\\') {
          A2 += (-2 * height + 1);
          height -= 1;
        } else {
          A2 += (-2 * height);
        }
      }
      if (height == 0) {
        A_list.push_back(A2 / 2);
        i = j;
      }
    }
    i++;
  }
  int A = accumulate(A_list.begin(), A_list.end(), 0);
  cout << A << endl;
  if (A == 0) {
    cout << 0 << endl;
  } else {
    int m = A_list.size();
    cout << m << " ";
    out(A_list, m);
  }
  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

int main() {
  stack<int> S1;
  stack<pair<int, int>> S2;
  char ch;
  int sum = 0;
  for (int i = 0; cin >> ch; ++i) {
    if (ch == '\\')
      S1.push(i);
    else if (ch == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      sum += i - j;
      int a = i - j;
      while (S2.size() > 0 && S2.top().first > j) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(j, a));
    }
  }
  vector<int> ans;
  int t = 0;
  while (S2.size() > 0) {
    ans.push_back(S2.top().second);
    S2.top();
  }
  reverse(ans.begin(), ans.end());
  cout << sum << endl;
  cout << ans.size();
  for (int i = 0; i < ans.size(); ++i) {
    cout << " ";
    t += ans[i];
    cout << ans[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  stack<int> S1;
  stack<pair<int, int>> S2;
  char ch;
  int sum = 0;
  for (int i = 0; cin >> ch; ++i) {
    if (ch == '\\')
      S1.push(i);
    else if (ch == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      sum += i - j;
      int a = i - j;
      while (S2.size() > 0 && S2.top().first > j) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(j, a));
    }
  }
  vector<int> ans;
  int t = 0;
  while (S2.size() > 0) {
    ans.push_back(S2.top().second);
    S2.pop();
  }
  reverse(ans.begin(), ans.end());
  cout << sum << endl;
  cout << ans.size();
  for (int i = 0; i < ans.size(); ++i) {
    cout << " ";
    t += ans[i];
    cout << ans[i];
  }
  cout << endl;
  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
/*
** 本题使用两个stack，一个用来统计总积水，一个用来统计单独的积水面积。
*/

#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

static const int maxn = 20000 + 10;

char buff[maxn];

int main() {
  // freopen("data.in", "r", stdin);
  // freopen("data.out", "w", stdout);
  while (scanf("%s", buff) != EOF) {
    /*
    ** ts用来存储每一个'\'字符的位置，然后当出现'/'字符的时候，通过距离计算积水面积
    ** ps用来存储每一个'\'所对应的积水面积，默认是0，当出现'/'后，再把面积加进去
    */
    stack<int> ts, ps;
    int sum = 0;
    for (int i = 0; buff[i] != '\0'; i++) {
      if (buff[i] == 92) { // 92 是 '\'的 ascii码值
        ts.push(i);
        ps.push(0);
      } else if (buff[i] == 47 && !ts.empty()) { // 47 是 '/'的 ascii码值
        int s = i - ts.top();
        ts.pop();
        sum += s;
        while (ps.top() != 0) {
          s += ps.top();
          ps.pop();
        }
        ps.pop();
        ps.push(s);
      }
    }

    printf("%d\n", sum);
    vector<int> v;
    while (!ps.empty()) {
      if (ps.top() != 0)
        v.push_back(ps.top());
      ps.pop();
    }
    printf("%d ", v.size());
    for (int i = v.size() - 1; i > 0; i--)
      printf("%d ", v[i]);
    printf("%d\n", v[0]);
  }
  return 0;
}


Fixed submission:
/*
** 本题使用两个stack，一个用来统计总积水，一个用来统计单独的积水面积。
*/

#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

static const int maxn = 20000 + 10;

char buff[maxn];

int main() {
  // freopen("data.in", "r", stdin);
  // freopen("data.out", "w", stdout);
  while (scanf("%s", buff) != EOF) {
    /*
    ** ts用来存储每一个'\'字符的位置，然后当出现'/'字符的时候，通过距离计算积水面积
    ** ps用来存储每一个'\'所对应的积水面积，默认是0，当出现'/'后，再把面积加进去
    */
    stack<int> ts, ps;
    int sum = 0;
    for (int i = 0; buff[i] != '\0'; i++) {
      if (buff[i] == 92) { // 92 是 '\'的 ascii码值
        ts.push(i);
        ps.push(0);
      } else if (buff[i] == 47 && !ts.empty()) { // 47 是 '/'的 ascii码值
        int s = i - ts.top();
        ts.pop();
        sum += s;
        while (ps.top() != 0) {
          s += ps.top();
          ps.pop();
        }
        ps.pop();
        ps.push(s);
      }
    }

    printf("%d\n", sum);
    vector<int> v;
    while (!ps.empty()) {
      if (ps.top() != 0)
        v.push_back(ps.top());
      ps.pop();
    }
    if (v.size() == 0)
      printf("0\n");
    else {
      printf("%d ", v.size());
      for (int i = v.size() - 1; i > 0; i--)
        printf("%d ", v[i]);
      printf("%d\n", v[0]);
    }
  }
  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {

  stack<int> sa;
  string m;
  int cnt = 0, result = 0, high = 0, tmp = 0, h[20000], i;
  vector<int> amo;

  cin >> m;
  for (i = m.size() - 1; i != -1; i--) {
    if (m[i] == '\\')
      tmp++;
    if (m[i] == '/')
      tmp--;
    if (tmp < 0) {
      for (int j = i + 1; j < m.size(); j++)
        h[j]++;
      high++;
      tmp = 0;
    }
    high = max(high, tmp);
    h[i] = high;
  }
  // for(i=0;i<m.size();i++) cout<<h[i]<<" ";

  for (i = 0; i < m.size(); i++) {
    //    if(i>0) if(h[i]-h[i-1]==1) sa.pop();
    if (m[i] == '\\') {
      sa.push(i);
    } else if (m[i] == '/' && !sa.empty()) {
      cnt += i - sa.top();
      sa.pop();
      if (sa.empty()) {
        amo.push_back(cnt);
        result += cnt;
        cnt = 0;
      }
    }
    if (i < m.size() - 1)
      if (h[i + 1] - h[i] != 0)
        sa.pop();
  }
  if (cnt != 0) {
    amo.push_back(cnt);
    result += cnt;
  }
  cout << result << endl << amo.size() << " ";
  for (i = 0; i < amo.size() - 1; i++)
    cout << amo[i] << " ";
  cout << amo[amo.size() - 1] << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {

  stack<int> sa;
  string m;
  int cnt = 0, result = 0, high = 0, tmp = 0, h[20000], i;
  vector<int> amo;

  cin >> m;
  for (i = m.size() - 1; i != -1; i--) {
    if (m[i] == '\\')
      tmp++;
    if (m[i] == '/')
      tmp--;
    if (tmp < 0) {
      for (int j = i + 1; j < m.size(); j++)
        h[j]++;
      high++;
      tmp = 0;
    }
    high = max(high, tmp);
    h[i] = high;
  }
  // for(i=0;i<m.size();i++) cout<<h[i]<<" ";

  for (i = 0; i < m.size(); i++) {
    //    if(i>0) if(h[i]-h[i-1]==1) sa.pop();
    if (m[i] == '\\') {
      sa.push(i);
    } else if (m[i] == '/' && !sa.empty()) {
      cnt += i - sa.top();
      sa.pop();
      if (sa.empty()) {
        amo.push_back(cnt);
        result += cnt;
        cnt = 0;
      }
    }
    if (i < m.size() - 1)
      if (h[i + 1] - h[i] != 0)
        sa.pop();
  }
  if (cnt != 0) {
    amo.push_back(cnt);
    result += cnt;
  }
  if (amo.empty())
    cout << "0" << endl << "0" << endl;
  else {
    cout << result << endl << amo.size() << " ";
    for (i = 0; i < amo.size() - 1; i++)
      cout << amo[i] << " ";
    cout << amo[amo.size() - 1] << endl;
  }
  return 0;
}
Problem p02266 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  stack<int> S1;
  stack<pair<int, int>> S2;
  char slope;
  int i = 0;
  int sum = 0;
  while (scanf("%s", slope) != EOF) {
    if (slope == '\\') {
      S1.push(i);
    } else if (slope == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      sum += i - j;
      int a = i - j;
      while (S2.size() > 0 && S2.top().first > j) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(j, a));
    }
    i++;
  }
  vector<int> ans;
  int t = 0;
  while (S2.size() > 0) {
    ans.push_back(S2.top().second);
    S2.pop();
  }
  reverse(ans.begin(), ans.end());
  cout << sum << endl;
  cout << ans.size();
  for (int i = 0; i < ans.size(); i++) {
    cout << " ";
    t += ans[i];
    cout << ans[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  stack<int> S1;
  stack<pair<int, int>> S2;
  char slope;
  int i = 0;
  int sum = 0;
  while (cin >> slope) {
    if (slope == '\\') {
      S1.push(i);
    } else if (slope == '/' && S1.size() > 0) {
      int j = S1.top();
      S1.pop();
      sum += i - j;
      int a = i - j;
      while (S2.size() > 0 && S2.top().first > j) {
        a += S2.top().second;
        S2.pop();
      }
      S2.push(make_pair(j, a));
    }
    i++;
  }
  vector<int> ans;
  int t = 0;
  while (S2.size() > 0) {
    ans.push_back(S2.top().second);
    S2.pop();
  }
  reverse(ans.begin(), ans.end());
  cout << sum << endl;
  cout << ans.size();
  for (int i = 0; i < ans.size(); i++) {
    cout << " ";
    t += ans[i];
    cout << ans[i];
  }
  cout << endl;
  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

bool t[10005];

signed main() {
  int n, q;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    t[tmp] = 1;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int tmp;
    cin >> tmp;
    if (t[tmp] == 1) {
      ans++;
      t[tmp] = 0;
    }
  }

  cout << ans << endl;

  return (0);
}

Fixed submission:
#include <iostream>
using namespace std;

const int MAX = 1e9 + 5;
bool t[MAX];

signed main() {
  int n, q;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    t[tmp] = 1;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int tmp;
    cin >> tmp;
    if (t[tmp] == 1) {
      ans++;
      t[tmp] = 0;
    }
  }

  cout << ans << endl;

  return (0);
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

bool linearSearch(int A[], int key, int size) {
  int i = 0;
  A[size] = key;
  while (A[i] != key)
    i++;
  return i != size;
}

int main() {
  int size, query, queryNum, count = 0;

  cin >> size;
  int *A = new int[size];
  for (int i = 0; i < size; i++)
    scanf("%d", &A[i]);

  cin >> queryNum;
  for (int i = 0; i < queryNum; i++) {
    scanf("%d", &query);
    if (linearSearch(A, query, size))
      count++;
  }

  cout << count << endl;

  delete[] A;
}

Fixed submission:
#include <iostream>
using namespace std;

bool linearSearch(int A[], int key, int size) {
  int i = 0;
  A[size] = key;
  while (A[i] != key)
    i++;
  return i != size;
}

int main() {
  int size, query, queryNum, count = 0;

  cin >> size;
  int *A = new int[size + 1];
  for (int i = 0; i < size; i++)
    scanf("%d", &A[i]);

  cin >> queryNum;
  for (int i = 0; i < queryNum; i++) {
    scanf("%d", &query);
    if (linearSearch(A, query, size))
      count++;
  }

  cout << count << endl;

  delete[] A;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int i, j, n, q, S[10001], T[100], C;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (i = 0; i < q; i++)
    cin >> T[i];

  C = 0;
  for (i = 0; i < q; i++) {
    j = 0;
    S[n] = T[i];
    while (S[j] != S[n])
      j++;
    if (j < n)
      C++;
  }
  cout << C << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int i, j, n, q, S[10001], T[500], C;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (i = 0; i < q; i++)
    cin >> T[i];

  C = 0;
  for (i = 0; i < q; i++) {
    j = 0;
    S[n] = T[i];
    while (S[j] != S[n])
      j++;
    if (j < n)
      C++;
  }
  cout << C << endl;

  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  long long n, q, s[1000000001], t, k;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  k = 0;
  for (long long i = 0; i < q; i++) {
    cin >> t;
    for (long long j = 0; j < n; j++) {
      if (s[j] == t) {
        k++;
        break;
      }
    }
  }
  cout << k << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  long long n, q, s[10001], t, k;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  k = 0;
  for (long long i = 0; i < q; i++) {
    cin >> t;
    for (long long j = 0; j < n; j++) {
      if (s[j] == t) {
        k++;
        break;
      }
    }
  }
  cout << k << endl;
  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, p;
  int s[1000];
  int t[1000];
  int i, j;
  int c = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> p;
  for (i = 0; i < p; i++) {
    cin >> t[i];
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < p; j++) {
      if (s[i] == t[j]) {
        t[j] = -1;
        c++;
      }
    }
  }
  cout << c << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, p;
  int s[1000000];
  int t[1000000];
  int i, j;
  int c = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> p;
  for (i = 0; i < p; i++) {
    cin >> t[i];
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < p; j++) {
      if (s[i] == t[j]) {
        t[j] = -1;
        c++;
      }
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  int q;
  cin >> q;
  vector<int> t(q);
  for (int i = 0; i < q; i++) {
    cin >> t[i];
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  int ans = 0, p = 0;
  for (int i = 0; i < n; i++) {
    if (t[p] == s[i]) {
      ans++;
      p++;
    } else if (t[p] < s[i]) {
      p++;
      i--;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  int q;
  cin >> q;
  vector<int> t(q);
  for (int i = 0; i < q; i++) {
    cin >> t[i];
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  int ans = 0;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < n; j++) {
      if (t[i] == s[j]) {
        ans++;
        i++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int linear(int a[], int key, int n);
int main() {
  int i, j, n, q, c = 0;
  int s[1000], t[1000];

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> s[i];

  cin >> q;
  for (i = 0; i < q; i++)
    cin >> t[i];

  for (i = 0; i < q; i++) {
    c = c + linear(s, t[i], n);
  }
  cout << c << endl;
}
int linear(int a[], int key, int n) {
  int i = 0;
  a[n] = key;
  while (a[i] != key) {
    i++;
  }
  if (i >= n) {
    return 0;
  } else {
    return 1;
  }
}

Fixed submission:
#include <iostream>
using namespace std;

int linear(int a[], int key, int n);
int main() {
  int i, j, n, q, c = 0;
  int s[100000], t[100000];

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> s[i];

  cin >> q;
  for (i = 0; i < q; i++)
    cin >> t[i];

  for (i = 0; i < q; i++) {
    c = c + linear(s, t[i], n);
  }
  cout << c << endl;
}
int linear(int a[], int key, int n) {
  int i = 0;
  a[n] = key;
  while (a[i] != key) {
    i++;
  }
  if (i >= n) {
    return 0;
  } else {
    return 1;
  }
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, N) for (int i = 0; i < N; ++i)

using namespace std;

bool linerSearch(vector<int> vec, int key) {
  for (int &v : vec)
    if (v == key)
      return true;
  return false;
}

int main() {
  int n, q, key;
  scanf("%d", &n);
  vector<int> S(n);
  rep(i, n) scanf("%d", &S[i]);

  int ans = 0;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &key);
    if (linerSearch(S, key))
      ++ans;
  }

  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, N) for (int i = 0; i < N; ++i)

using namespace std;

bool linerSearch(vector<int> vec, int key) {
  auto itr = std::find(vec.begin(), vec.end(), key);
  size_t index = std::distance(vec.begin(), itr);
  return index != vec.size();
}

int main() {
  int n, q, key;
  scanf("%d", &n);
  vector<int> S(n);
  rep(i, n) scanf("%d", &S[i]);

  int ans = 0;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &key);
    if (linerSearch(S, key))
      ++ans;
  }

  printf("%d\n", ans);
  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int LSearch(int A[], unsigned int q, int x) {
  A[q] = x;
  for (int i = 0;; i++) {
    if (A[i] == x)
      if (i != q)
        return 0;
      else
        return -1;
  }
}
int main() {
  unsigned int n, q;
  unsigned int counter = 0;
  int S[1000000000], T;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T;
    if (LSearch(S, n, T) == 0)
      counter++;
  }
  cout << counter << endl;
}


Fixed submission:
#include <iostream>
using namespace std;

int LSearch(int A[], unsigned int q, int x) {
  A[q] = x;
  for (int i = 0;; i++) {
    if (A[i] == x)
      if (i != q)
        return 0;
      else
        return -1;
  }
}
int main() {
  unsigned int n, q;
  unsigned int counter = 0;
  int S[10000000], T;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T;
    if (LSearch(S, n, T) == 0)
      counter++;
  }
  cout << counter << endl;
}
Problem p02267 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int search(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return (i != n);
}

int main() {
  int A[10000 + 1], n, i, q, sum = 0, key;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    while (search(A, n, key))
      sum++;
  }

  printf("%d\n", sum);

  return 0;
}

Fixed submission:
#include <stdio.h>

int search(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return (i != n);
}

int main() {
  int A[10000 + 1], n, i, q, sum = 0, key;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (search(A, n, key))
      sum++;
  }

  printf("%d\n", sum);

  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int Linear_Search(int S[], int T[], int n, int q) {
  int result = 0;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < n; j++) {
      if (T[i] == S[j]) {
        result++;
        break;
      }
    }
  }

  return result;
}

int main() {
  int S[1000], T[1000];
  int n, q, result;

  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> S[i];

  cin >> q;

  for (int i = 0; i < q; i++)
    cin >> T[i];

  result = Linear_Search(S, T, n, q);

  cout << result << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int Linear_Search(int S[], int T[], int n, int q) {
  int result = 0;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < n; j++) {
      if (T[i] == S[j]) {
        result++;
        break;
      }
    }
  }

  return result;
}

int main() {
  int S[10000], T[1000];
  int n, q, result;

  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> S[i];

  cin >> q;

  for (int i = 0; i < q; i++)
    cin >> T[i];

  result = Linear_Search(S, T, n, q);

  cout << result << endl;

  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int search(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return i != n;
}

int main() {
  int i, n, A[1000 + 1], q, key, sum = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (search(A, n, key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}

Fixed submission:
#include <stdio.h>

int search(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return i != n;
}

int main() {
  int i, n, A[10000 + 1], q, key, sum = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (search(A, n, key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  ALDS1_4_A
//
//  Created by ptyuan on 2018/10/22.
//  Copyright © 2018 YY. All rights reserved.
//

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

int linearSearch(int *array, int n, int key) {
  *(array + n) = key;
  int i = 0;
  while (array[i] != key) {
    i++;
  }
  return i == n ? -1 : i;
}

int main(int argc, const char *argv[]) {
  int n = 0;
  int S[10 ^ 5] = {0};
  int q = 0;
  int sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", S + i);
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int key = 0;
    scanf("%d", &key);
    if (-1 != linearSearch(S, n, key)) {
      sum++;
    }
  }
  printf("%d\n", sum);

  return 0;
}


Fixed submission:
//
//  main.cpp
//  ALDS1_4_A
//
//  Created by ptyuan on 2018/10/22.
//  Copyright © 2018 YY. All rights reserved.
//

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

int linearSearch(int *array, int n, int key) {
  *(array + n) = key;
  int i = 0;
  while (array[i] != key) {
    i++;
  }
  return i == n ? -1 : i;
}

int main(int argc, const char *argv[]) {
  int n = 0;
  int S[10000 + 1] = {0};
  int q = 0;
  int sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", S + i);
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int key = 0;
    scanf("%d", &key);
    if (-1 != linearSearch(S, n, key)) {
      sum++;
    }
  }
  printf("%d\n", sum);

  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

void fnInput(vector<int> &a_rvoTarget, vector<int> &a_rvoSearch) {
  int iTargetCnt;
  cin >> iTargetCnt;

  a_rvoTarget.resize(iTargetCnt + 1);
  for (int i = 0; i < iTargetCnt; i++)
    cin >> a_rvoTarget[i];

  int iSearchCnt;
  cin >> iSearchCnt;

  a_rvoSearch.resize(iSearchCnt);
  for (int i = 0; i < iSearchCnt; i++)
    cin >> a_rvoSearch[i];
}

void fnLinearSearch(vector<int> &a_rvoTarget, const vector<int> &a_cnrvoSearch,
                    int &a_riFound) {
  size_t zMaxSize = a_rvoTarget.size() - 1;
  for (size_t zi = 0; zi < a_cnrvoSearch.size(); zi++) {
    a_rvoTarget[zMaxSize] = a_cnrvoSearch[zi];
    size_t zj = 0;
    while (a_rvoTarget[zj] = a_cnrvoSearch[zi])
      zj++;
    if (zj != zMaxSize)
      a_riFound++;
  }
}

void fnResultOut(int a_iFound) { cout << a_iFound << endl; }

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

  vector<int> voTarget, voSearch;
  int iFound = 0;

  fnInput(voTarget, voSearch);
  fnLinearSearch(voTarget, voSearch, iFound);
  fnResultOut(iFound);

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

void fnInput(vector<int> &a_rvoTarget, vector<int> &a_rvoSearch) {
  int iTargetCnt;
  cin >> iTargetCnt;

  a_rvoTarget.resize(iTargetCnt + 1);
  for (int i = 0; i < iTargetCnt; i++)
    cin >> a_rvoTarget[i];

  int iSearchCnt;
  cin >> iSearchCnt;

  a_rvoSearch.resize(iSearchCnt);
  for (int i = 0; i < iSearchCnt; i++)
    cin >> a_rvoSearch[i];
}

void fnLinearSearch(vector<int> &a_rvoTarget, const vector<int> &a_cnrvoSearch,
                    int &a_riFound) {
  size_t zMaxSize = a_rvoTarget.size() - 1;
  for (size_t zi = 0; zi < a_cnrvoSearch.size(); zi++) {
    a_rvoTarget[zMaxSize] = a_cnrvoSearch[zi];
    size_t zj = 0;
    while (a_rvoTarget[zj] != a_cnrvoSearch[zi])
      zj++;
    if (zj != zMaxSize)
      a_riFound++;
  }
}

void fnResultOut(int a_iFound) { cout << a_iFound << endl; }

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

  vector<int> voTarget, voSearch;
  int iFound = 0;

  fnInput(voTarget, voSearch);
  fnLinearSearch(voTarget, voSearch, iFound);
  fnResultOut(iFound);

  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int searcher(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return (i != n);
}

int main() {
  int A[1000 + 1], key, q, n;
  int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (searcher(A, n, key))
      sum++;
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int searcher(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return (i != n);
}

int main() {
  int A[10000 + 10], key, q, n;
  int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (searcher(A, n, key))
      sum++;
  }
  cout << sum << endl;
  return 0;
}
Problem p02267 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
bool flag[10000];
int main() {
  int n, m;
  cin >> n;
  int s[10000], t[10000];
  rep(i, n) cin >> s[i];
  cin >> m;
  int cnt = 0;
  rep(i, m) cin >> t[i];
  rep(i, n) {
    rep(j, m) {
      if (s[i] == t[j] && flag[s[i]] == false) {
        cnt++;
        flag[s[i]] = true;
        break;
      }
    }
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
bool flag[1000000000];
signed main() {
  int n, m;
  cin >> n;
  int s[10000], t[10000];
  rep(i, n) cin >> s[i];
  cin >> m;
  int cnt = 0;
  rep(i, m) cin >> t[i];
  rep(i, n) {
    rep(j, m) {
      if (s[i] == t[j] && flag[s[i]] == false) {
        cnt++;
        flag[s[i]] = true;
        break;
      }
    }
  }
  cout << cnt << endl;
}