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

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

int ans[100][2]
       [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列

int counter = 0, flag = 0;

void solve() {
  int n;
  cin >> n;
  if (n == 0) {
    flag = 1;
    return;
  }
  ans[counter][0][0] = n;

  int S[12]; // 数列
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  bool check = false; // 操作の有無のフラグ
  int time = 0;       // 操作回数
  do {
    map<int, int> marker;
    for (int i = 0; i < n; i++) {
      marker[S[i]];
      marker[S[i]]++;
    }
    check = false;
    for (int i = 0; i < n; i++) {
      if (S[i] != marker[S[i]]) {
        check = true;
        S[i] = marker[S[i]];
      }
    }
    if (check)
      time++;
  } while (check);

  ans[counter][0][1] = time;
  for (int i = 0; i < n; i++) {
    ans[counter][1][i] = S[i];
  }
  counter++;
}

int main() {
  while (flag == 0)
    solve();
  for (int i = 0; i < counter; i++) {
    cout << ans[i][0][1] << endl;
    for (int j = 0; j < ans[i][0][0] - 1; j++) {
      cout << ans[i][1][j] << " ";
    }
    cout << ans[i][1][ans[i][0][0] - 1] << endl;
  }
}

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

int ans[1000][2]
       [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列

int counter = 0, flag = 0;

void solve() {
  int n;
  cin >> n;
  if (n == 0) {
    flag = 1;
    return;
  }
  ans[counter][0][0] = n;

  int S[12]; // 数列
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  bool check = false; // 操作の有無のフラグ
  int time = 0;       // 操作回数
  do {
    map<int, int> marker;
    for (int i = 0; i < n; i++) {
      marker[S[i]];
      marker[S[i]]++;
    }
    check = false;
    for (int i = 0; i < n; i++) {
      if (S[i] != marker[S[i]]) {
        check = true;
        S[i] = marker[S[i]];
      }
    }
    if (check)
      time++;
  } while (check);

  ans[counter][0][1] = time;
  for (int i = 0; i < n; i++) {
    ans[counter][1][i] = S[i];
  }
  counter++;
}

int main() {
  while (flag == 0)
    solve();
  for (int i = 0; i < counter; i++) {
    cout << ans[i][0][1] << endl;
    for (int j = 0; j < ans[i][0][0] - 1; j++) {
      cout << ans[i][1][j] << " ";
    }
    cout << ans[i][1][ans[i][0][0] - 1] << endl;
  }
}
Problem p00108 (C++)

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

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<int> s;
    int b;
    for (int i = 0; i < n; i++) {
      cin >> b;
      s.push_back(b);
    }
    int c = 0;
    while (1) {
      vector<int> freq(13, 0);
      for (int i = 0; i < n; i++)
        freq[s[i]]++;

      vector<int> ss = s;
      for (int i = 0; i < n; i++)
        ss[i] = freq[ss[i]];

      if (ss == s) {
        cout << c << endl;
        for (int i = 0; i < s.size(); i++)
          printf("%d%c", s[i], (i == s.size() - 1) ? '\n' : ' ');
        break;
      }

      s = ss;
      c++;
    }
  }
}

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

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<int> s;
    int b;
    for (int i = 0; i < n; i++) {
      cin >> b;
      s.push_back(b);
    }
    int c = 0;
    while (1) {
      vector<int> freq(1000, 0);
      for (int i = 0; i < n; i++)
        freq[s[i]]++;

      vector<int> ss = s;
      for (int i = 0; i < n; i++)
        ss[i] = freq[ss[i]];

      if (ss == s) {
        cout << c << endl;
        for (int i = 0; i < s.size(); i++)
          printf("%d%c", s[i], (i == s.size() - 1) ? '\n' : ' ');
        break;
      }

      s = ss;
      c++;
    }
  }
}
Problem p00108 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
using namespace std;
int x[100000][500], n;
int main() {
  while (true) {
    memset(x, 0, sizeof(x));
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> x[0][i];
    }
    for (int i = 1; i < 30000; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (x[i - 1][k] == x[i - 1][j]) {
            x[i][j]++;
          }
        }
      }
    }
    for (int i = 1; i < 30000; i++) {
      for (int j = 0; j < n; j++) {
        if (x[i - 1][j] != x[i][j]) {
          goto E;
        }
      }
      cout << i - 1 << endl;
      for (int j = 0; j < n; j++) {
        if (j) {
          cout << ' ';
        }
        cout << x[i][j];
      }
      cout << endl;
      goto owata;
    E:;
    }
  owata:;
  }
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
int x[32000][500], n;
int main() {
  while (true) {
    memset(x, 0, sizeof(x));
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> x[0][i];
    }
    for (int i = 1; i < 30000; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (x[i - 1][k] == x[i - 1][j]) {
            x[i][j]++;
          }
        }
      }
    }
    for (int i = 1; i < 30000; i++) {
      for (int j = 0; j < n; j++) {
        if (x[i - 1][j] != x[i][j]) {
          goto E;
        }
      }
      cout << i - 1 << endl;
      for (int j = 0; j < n; j++) {
        if (j) {
          cout << ' ';
        }
        cout << x[i][j];
      }
      cout << endl;
      goto owata;
    E:;
    }
  owata:;
  }
}
Problem p00108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> vi, vi2;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      int j;
      cin >> j;
      vi.push_back(j);
    }
    int counter = -1;
    do {
      vi2.clear();
      copy(vi.begin(), vi.end(), back_inserter(vi2));
      for (int i = 0; i < n; i++) {
        vi[i] = count(vi2.begin(), vi2.end(), vi[i]);
      }
      counter++;
    } while (!equal(vi.begin(), vi.end(), vi2.begin()));
    cout << counter << endl;
    for (int i = 0; i < vi.size(); i++) {
      if (i != 0)
        cout << " ";
      cout << vi[i];
    }
    cout << endl;
  }
  return 0;
}

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

int main() {
  int n;
  vector<int> vi, vi2;
  while (cin >> n) {
    vi.clear();
    vi2.clear();
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      int j;
      cin >> j;
      vi.push_back(j);
    }
    int counter = -1;
    do {
      vi2.clear();
      copy(vi.begin(), vi.end(), back_inserter(vi2));
      for (int i = 0; i < n; i++) {
        vi[i] = count(vi2.begin(), vi2.end(), vi[i]);
      }
      counter++;
    } while (!equal(vi.begin(), vi.end(), vi2.begin()));
    cout << counter << endl;
    for (int i = 0; i < vi.size(); i++) {
      if (i != 0)
        cout << " ";
      cout << vi[i];
    }
    cout << endl;
  }
  return 0;
}
Problem p00108 (C++)

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

int ans[113][2]
       [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列

int counter = 0, flag = 0;

void solve() {
  int n;
  cin >> n;
  if (n == 0) {
    flag = 1;
    return;
  }
  ans[counter][0][0] = n;

  int S[12]; // 数列
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  bool check = false; // 操作の有無のフラグ
  int time = 0;       // 操作回数
  do {
    map<int, int> marker;
    for (int i = 0; i < n; i++) {
      marker[S[i]];
      marker[S[i]]++;
    }
    check = false;
    for (int i = 0; i < n; i++) {
      if (S[i] != marker[S[i]]) {
        check = true;
        S[i] = marker[S[i]];
      }
    }
    if (check)
      time++;
  } while (check);

  ans[counter][0][1] = time;
  for (int i = 0; i < n; i++) {
    ans[counter][1][i] = S[i];
  }
  counter++;
}

int main() {
  while (flag == 0)
    solve();
  for (int i = 0; i < counter; i++) {
    cout << ans[i][0][1] << endl;
    for (int j = 0; j < ans[i][0][0] - 1; j++) {
      cout << ans[i][1][j] << " ";
    }
    cout << ans[i][1][ans[i][0][0] - 1] << endl;
  }
}


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

int ans[114][2]
       [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列

int counter = 0, flag = 0;

void solve() {
  int n;
  cin >> n;
  if (n == 0) {
    flag = 1;
    return;
  }
  ans[counter][0][0] = n;

  int S[12]; // 数列
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  bool check = false; // 操作の有無のフラグ
  int time = 0;       // 操作回数
  do {
    map<int, int> marker;
    for (int i = 0; i < n; i++) {
      marker[S[i]];
      marker[S[i]]++;
    }
    check = false;
    for (int i = 0; i < n; i++) {
      if (S[i] != marker[S[i]]) {
        check = true;
        S[i] = marker[S[i]];
      }
    }
    if (check)
      time++;
  } while (check);

  ans[counter][0][1] = time;
  for (int i = 0; i < n; i++) {
    ans[counter][1][i] = S[i];
  }
  counter++;
}

int main() {
  while (flag == 0)
    solve();
  for (int i = 0; i < counter; i++) {
    cout << ans[i][0][1] << endl;
    for (int j = 0; j < ans[i][0][0] - 1; j++) {
      cout << ans[i][1][j] << " ";
    }
    cout << ans[i][1][ans[i][0][0] - 1] << endl;
  }
}
Problem p00108 (C++)

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

int ans[110][2]
       [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列

int counter = 0, flag = 0;

void solve() {
  int n;
  cin >> n;
  if (n == 0) {
    flag = 1;
    return;
  }
  ans[counter][0][0] = n;

  int S[12]; // 数列
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  bool check = false; // 操作の有無のフラグ
  int time = 0;       // 操作回数
  do {
    map<int, int> marker;
    for (int i = 0; i < n; i++) {
      marker[S[i]];
      marker[S[i]]++;
    }
    check = false;
    for (int i = 0; i < n; i++) {
      if (S[i] != marker[S[i]]) {
        check = true;
        S[i] = marker[S[i]];
      }
    }
    if (check)
      time++;
  } while (check);

  ans[counter][0][1] = time;
  for (int i = 0; i < n; i++) {
    ans[counter][1][i] = S[i];
  }
  counter++;
}

int main() {
  while (flag == 0)
    solve();
  for (int i = 0; i < counter; i++) {
    cout << ans[i][0][1] << endl;
    for (int j = 0; j < ans[i][0][0] - 1; j++) {
      cout << ans[i][1][j] << " ";
    }
    cout << ans[i][1][ans[i][0][0] - 1] << endl;
  }
}


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

int ans[120][2]
       [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列

int counter = 0, flag = 0;

void solve() {
  int n;
  cin >> n;
  if (n == 0) {
    flag = 1;
    return;
  }
  ans[counter][0][0] = n;

  int S[12]; // 数列
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  bool check = false; // 操作の有無のフラグ
  int time = 0;       // 操作回数
  do {
    map<int, int> marker;
    for (int i = 0; i < n; i++) {
      marker[S[i]];
      marker[S[i]]++;
    }
    check = false;
    for (int i = 0; i < n; i++) {
      if (S[i] != marker[S[i]]) {
        check = true;
        S[i] = marker[S[i]];
      }
    }
    if (check)
      time++;
  } while (check);

  ans[counter][0][1] = time;
  for (int i = 0; i < n; i++) {
    ans[counter][1][i] = S[i];
  }
  counter++;
}

int main() {
  while (flag == 0)
    solve();
  for (int i = 0; i < counter; i++) {
    cout << ans[i][0][1] << endl;
    for (int j = 0; j < ans[i][0][0] - 1; j++) {
      cout << ans[i][1][j] << " ";
    }
    cout << ans[i][1][ans[i][0][0] - 1] << endl;
  }
}
Problem p00108 (C++)

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

int main() {
  while (1) {
    int n, count = 0;
    long before[13], after[13], c[13];

    scanf(" %d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; ++i)
      scanf(" %ld", &before[i]);

    // loop
    while (1) {

      for (int i = 0; i <= 120; ++i)
        c[i] = 0;

      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (before[i] == before[j])
            c[i]++;
        }
      }

      for (int i = 0; i < n; ++i)
        after[i] = c[i];

      bool flag = true;
      for (int i = 0; i < n; ++i) {
        if (before[i] != after[i]) {
          flag = false;
          break;
        }
      }
      if (flag)
        break;

      /*
      for(int i=0; i<n; ++i){
              printf("%ld", after[i]);
              if(i<n-1) printf(" ");
      }
      printf("\n");
      */

      count++;
      for (int i = 0; i < n; ++i)
        before[i] = after[i];
    }

    printf("%d\n", count);
    for (int i = 0; i < n; ++i) {
      printf("%ld", after[i]);
      if (i < n - 1)
        printf(" ");
    }
    printf("\n");
  }
  return 0;
}

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

int main() {
  while (1) {
    int n, count = 0;
    long before[13], after[13], c[13];

    scanf(" %d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; ++i)
      scanf(" %ld", &before[i]);

    // loop
    while (1) {

      for (int i = 0; i <= 12; ++i)
        c[i] = 0;

      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (before[i] == before[j])
            c[i]++;
        }
      }

      for (int i = 0; i < n; ++i)
        after[i] = c[i];

      bool flag = true;
      for (int i = 0; i < n; ++i) {
        if (before[i] != after[i]) {
          flag = false;
          break;
        }
      }
      if (flag)
        break;

      /*
      for(int i=0; i<n; ++i){
              printf("%ld", after[i]);
              if(i<n-1) printf(" ");
      }
      printf("\n");
      */

      count++;
      for (int i = 0; i < n; ++i)
        before[i] = after[i];
    }

    printf("%d\n", count);
    for (int i = 0; i < n; ++i) {
      printf("%ld", after[i]);
      if (i < n - 1)
        printf(" ");
    }
    printf("\n");
  }
  return 0;
}
Problem p00108 (C++)

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

int main() {
  int n, c, i;
  int pre[12], now[12], cnt[12];
  while (cin >> n, n) {
    map<int, int> m;
    for (i = 0; i < n; i++) {
      cin >> pre[i];
      m[pre[i]] = i;
    }
    for (i = 0; i < n; i++)
      now[i] = m[pre[i]];

    for (c = 0;; c++) {
      for (i = 0; i < n; i++)
        cnt[i] = 0;

      for (i = 0; i < n; i++)
        cnt[now[i] - 1]++;

      for (i = 0; i < n; i++)
        now[i] = cnt[now[i] - 1];

      for (i = 0; i < n; i++)
        if (now[i] != pre[i])
          break;

      if (i == n)
        break;

      for (i = 0; i < n; i++)
        pre[i] = now[i];
    }

    cout << c << endl << now[0];
    for (i = 1; i < n; i++)
      cout << " " << now[i];
    cout << endl;
  }
  return 0;
}

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

int main() {
  int n, c, i;
  int pre[12], now[12], cnt[12];
  while (cin >> n, n) {
    map<int, int> m;
    for (i = 0; i < n; i++) {
      cin >> pre[i];
      m[pre[i]] = i + 1;
    }
    for (i = 0; i < n; i++)
      now[i] = m[pre[i]];

    for (c = 0;; c++) {
      for (i = 0; i < n; i++)
        cnt[i] = 0;

      for (i = 0; i < n; i++)
        cnt[now[i] - 1]++;

      for (i = 0; i < n; i++)
        now[i] = cnt[now[i] - 1];

      for (i = 0; i < n; i++)
        if (now[i] != pre[i])
          break;

      if (i == n)
        break;

      for (i = 0; i < n; i++)
        pre[i] = now[i];
    }

    cout << c << endl << now[0];
    for (i = 1; i < n; i++)
      cout << " " << now[i];
    cout << endl;
  }
  return 0;
}
Problem p00109 (C++)

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

using namespace std;

string S;
size_t cur;

int digit() { return S[cur++] - '0'; }

int number() {
  int n = digit();
  while (cur < S.size() && isdigit(S[cur])) {
    n = n * 10 + digit();
  }
  return n;
}

int expression();

int factor() {
  if (isdigit(S[cur]))
    return number();
  cur++;
  int n = expression();
  cur++;
  return n;
}

int term() {
  int t = factor();
  for (; cur < S.size() && (S[cur] == '*' || S[cur] == '/');) {
    if (S[cur++] == '*')
      t *= factor();
    else
      t /= factor();
  }
  return t;
}

int expression() {
  int t = term();
  for (; cur < S.size() && (S[cur] == '+' || S[cur] == '-');) {
    if (S[cur++] == '+')
      t += term();
    else
      t -= term();
  }
  return t;
}

int main() {
  int n;
  cin >> n;
  for (; n--;) {
    cin >> S;
    cout << expression() << endl;
  }
  return 0;
}

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

using namespace std;

string S;
size_t cur;

int digit() { return S[cur++] - '0'; }

int number() {
  int n = digit();
  while (cur < S.size() && isdigit(S[cur])) {
    n = n * 10 + digit();
  }
  return n;
}

int expression();

int factor() {
  if (isdigit(S[cur]))
    return number();
  cur++;
  int n = expression();
  cur++;
  return n;
}

int term() {
  int t = factor();
  for (; cur < S.size() && (S[cur] == '*' || S[cur] == '/');) {
    if (S[cur++] == '*')
      t *= factor();
    else
      t /= factor();
  }
  return t;
}

int expression() {
  int t = term();
  for (; cur < S.size() && (S[cur] == '+' || S[cur] == '-');) {
    if (S[cur++] == '+')
      t += term();
    else
      t -= term();
  }
  return t;
}

int main() {
  int n;
  cin >> n;
  for (; n--;) {
    cur = 0;
    cin >> S;
    cout << expression() << endl;
  }
  return 0;
}
Problem p00112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  while (1) {
    int n, t[100], w;
    w = 0;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> t[i];
    }
    sort(t, t + n);
    for (int i = 0; i < n; i++) {
      w += t[i] * (n - 1 - i);
    }
    cout << w << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  while (1) {
    int n, t[10005];
    long long int w;
    w = 0;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> t[i];
    }
    sort(t, t + n);
    for (int i = 0; i < n; i++) {
      w += t[i] * (n - 1 - i);
    }
    cout << w << endl;
  }
  return 0;
}
Problem p00112 (C++)

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

using namespace std;
int n, m, temp;
int t[100];
int sum;
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    m = n;
    while (m--) {
      cin >> t[m];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < n - 1 - i; j++) {
        if (t[j] > t[j + 1]) {
          temp = t[j];
          t[j] = t[j + 1];
          t[j + 1] = temp;
        }
      }
    }
    sum = 0;
    for (int i = 0; i < n - 1; i++) {
      sum += (i + 1) * t[n - 2 - i];
    }
    cout << sum << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;
int n, m, temp;
int t[10005];
long long int sum;
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    m = n;
    while (m--) {
      cin >> t[m];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < n - 1 - i; j++) {
        if (t[j] > t[j + 1]) {
          temp = t[j];
          t[j] = t[j + 1];
          t[j + 1] = temp;
        }
      }
    }
    sum = 0;
    for (int i = 0; i < n - 1; i++) {
      sum += (i + 1) * t[n - 2 - i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    vector<int> ts;
    for (int i = 0; i < n; i++) {
      cin >> ts[i];
    }
    sort(ts.begin(), ts.end());
    long long sum = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      ans += sum;
      sum += ts[i];
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    vector<int> ts(n);
    for (int i = 0; i < n; i++) {
      cin >> ts[i];
    }
    sort(ts.begin(), ts.end());
    long long sum = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      ans += sum;
      sum += ts[i];
    }
    cout << ans << endl;
  }
}
Problem p00112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int need_time[500] = {0}, total_time[500] = {0}, total = 0;
    for (int i = 0; i < n; i++) {
      cin >> need_time[i];
    }
    sort(need_time, need_time + n);
    for (int i = 1; i < n; i++) {
      total_time[i] = need_time[i - 1] + total_time[i - 1];
      total += total_time[i];
    }
    cout << total << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    long long need_time[10100] = {0}, total_time[10100] = {0}, total = 0;
    for (int i = 0; i < n; i++) {
      cin >> need_time[i];
    }
    sort(need_time, need_time + n);
    for (int i = 1; i < n; i++) {
      total_time[i] = need_time[i - 1] + total_time[i - 1];
      total += total_time[i];
    }
    cout << total << endl;
  }
  return 0;
}
Problem p00112 (C++)

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

int main() {
  int n, time[1000];
  long long ans = 0;

  while (cin >> n) {
    if (n == 0)
      break;
    int wait = 0;
    for (int i = 0; i < n; i++)
      cin >> time[i];
    sort(time, time + n);
    for (int i = 0; i < n; i++) {
      ans += wait;
      wait += time[i];
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}

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

int main() {
  int n, time[10000];
  long long ans = 0;

  while (cin >> n) {
    if (n == 0)
      break;
    int wait = 0;
    for (int i = 0; i < n; i++)
      cin >> time[i];
    sort(time, time + n);
    for (int i = 0; i < n; i++) {
      ans += wait;
      wait += time[i];
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}
Problem p00114 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
typedef long long ll;
ll GCD(ll a, ll b) { return (a == 0) ? b : GCD(b % a, a); }
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
int main() {
  int a[3], m[3];
  while (1) {
    cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
    if (!a[0]) {
      break;
    }
    int val[3] = {1, 1, 1}, move[3];
    int cnt = 0;
    for (int i = 1;; ++i) {
      for (int j = 0; j < 3; ++j) {
        if (move[j])
          continue;
        val[j] = a[j] * val[j] % m[j];
        if (val[j] == 1) {
          move[j] = i;
          cnt++;
        }
      }
      if (cnt >= 3)
        break;
    }
    ll ans = LCM(LCM(move[0], move[1]), move[2]);
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
ll GCD(ll a, ll b) { return (a == 0) ? b : GCD(b % a, a); }
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
int main() {
  int a[3], m[3];
  while (1) {
    cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
    if (!a[0]) {
      break;
    }
    int val[3] = {1, 1, 1}, move[3] = {0};
    int cnt = 0;
    for (int i = 1;; ++i) {
      for (int j = 0; j < 3; ++j) {
        if (move[j])
          continue;
        val[j] = a[j] * val[j] % m[j];
        if (val[j] == 1) {
          move[j] = i;
          cnt++;
        }
      }
      if (cnt >= 3)
        break;
    }
    ll ans = LCM(LCM(move[0], move[1]), move[2]);
    cout << ans << endl;
  }
}
Problem p00114 (C++)

Buggy submission (status: Runtime Error):
// x^t = 1 mod m のとき、
// x^(kt) = 1 mod m.
#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll a1, m1, a2, m2, a3, m3;

  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    ll x = 1, y = 1, z = 1;

    ll tmp = a1;
    while (tmp != 1) {
      x++;
      tmp = (tmp * a1) % m1;
    }

    tmp = a2;
    while (tmp != 1) {
      y++;
      tmp = (tmp * a2) % m2;
    }

    tmp = a3;
    while (tmp != 1) {
      z++;
      tmp = (tmp * a3) % m3;
    }

    ll ans = lcm(lcm(x, y), z);
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
// x^t = 1 mod m のとき、
// x^(kt) = 1 mod m.
#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll a1, m1, a2, m2, a3, m3;

  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 + m1 + a2 + m2 + a3 + m3 == 0)
      break;

    ll x = 1, y = 1, z = 1;

    ll tmp = a1;
    while (tmp != 1) {
      x++;
      tmp = (tmp * a1) % m1;
    }

    tmp = a2;
    while (tmp != 1) {
      y++;
      tmp = (tmp * a2) % m2;
    }

    tmp = a3;
    while (tmp != 1) {
      z++;
      tmp = (tmp * a3) % m3;
    }

    ll ans = lcm(lcm(x, y), z);
    cout << ans << endl;
  }

  return 0;
}
Problem p00114 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define int long long

using namespace std;

int cou(int a, int m);
int ggd(int number1, int number2);
int lcm(int number1, int number2);

signed main() {
  int a1, a2, a3, m1, m2, m3;
  int x, y, z;
  int count;
  int countx, county, countz;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    x = 1;
    y = 1;
    z = 1;
    count = 0;

    countx = cou(a1, m1);
    county = cou(a2, m2);
    countz = cou(a3, m3);

    cout << lcm(lcm(countx, county), countz) << endl;
  }
  return 0;
}

int cou(int a, int m) {
  int x = 1;
  int count = 0;
  do {
    count++;
    x = (a * x) % m;
  } while (x != 1);
  return count;
}

int ggd(int number1, int number2) {
  int m = number1;
  int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }

  while (m != n) {
    int temp = n;
    n = m - n;
    m = temp;
  }

  return m;
}

int lcm(int number1, int number2) {
  return number1 * number2 / ggd(number1, number2);
}

Fixed submission:
#include <iostream>
#define int long long

using namespace std;

int cou(int a, int m);
int ggd(int number1, int number2);
int lcm(int number1, int number2);

signed main() {
  int a1, a2, a3, m1, m2, m3;
  int x, y, z;
  int count;
  int countx, county, countz;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    x = 1;
    y = 1;
    z = 1;
    count = 0;

    countx = cou(a1, m1);
    county = cou(a2, m2);
    countz = cou(a3, m3);

    cout << lcm(lcm(countx, county), countz) << endl;
  }
  return 0;
}

int cou(int a, int m) {
  int x = 1;
  int count = 0;
  do {
    count++;
    x = (a * x) % m;
  } while (x != 1);
  return count;
}

int ggd(int number1, int number2) {
  int m = number1;
  int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }

  while (n != 0) {
    int temp = m % n;
    m = n;
    n = temp;
  }

  return m;
}

int lcm(int number1, int number2) {
  return number1 * number2 / ggd(number1, number2);
}
Problem p00114 (C++)

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

using namespace std;

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

namespace std {
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }
} // namespace std

int main() {
  long long cyc[3], a[3], m[3];

#define in(i) , &a[i], &m[i]
#define o(i) | a[i] | m[i]

  while (scanf("%lld%lld%lld%lld%lld%lld" in(0)in(1)in(2)), 1 o(0) o(1) o(2)) {
    rep(i, 3) {
      cyc[i] = 0;
      int cnt = 1, k = 1;
      while (1) {
        k *= a[i];
        k %= m[i];
        if (k == 1) {
          cyc[i] = cnt;
          break;
        }
        cnt++;
      }
    }
    cout << lcm(lcm(cyc[0], cyc[1]), cyc[2]) << endl;
  }
}

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

using namespace std;

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

namespace std {
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }
} // namespace std

int main() {
  long long cyc[3], a[3], m[3];

#define in(i) , &a[i], &m[i]
#define o(i) | a[i] | m[i]

  while (scanf("%lld%lld%lld%lld%lld%lld" in(0)in(1)in(2)), 0 o(0) o(1) o(2)) {
    rep(i, 3) {
      cyc[i] = 0;
      int cnt = 1, k = 1;
      while (1) {
        k *= a[i];
        k %= m[i];
        if (k == 1) {
          cyc[i] = cnt;
          break;
        }
        cnt++;
      }
    }
    cout << lcm(lcm(cyc[0], cyc[1]), cyc[2]) << endl;
  }
}
Problem p00114 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
long long a[3], m[3], k[3];
long long gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
int main() {
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]) {
    for (int i = 0; i < 3; i++) {
      long long cnt = 0, x = 1;
      while (true) {
        if (x == 1 && cnt >= 1) {
          break;
        }
        x *= a[i];
        x %= m[i];
        cnt++;
      }
      k[i] = cnt;
    }
    int LCM = k[0] * k[1] / gcd(k[0], k[1]);
    cout << LCM * k[2] / gcd(LCM, k[2]) << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
long long a[3], m[3], k[3];
long long gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
int main() {
  while (true) {
    cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
    if (a[0] == 0 && m[0] == 0 && a[1] == 0 && m[1] == 0 && a[2] == 0 &&
        m[2] == 0) {
      break;
    }
    for (int i = 0; i < 3; i++) {
      long long cnt = 0, x = 1;
      while (true) {
        if (x == 1 && cnt >= 1) {
          break;
        }
        x *= a[i];
        x %= m[i];
        cnt++;
      }
      k[i] = cnt;
    }
    int LCM = k[0] * k[1] / gcd(k[0], k[1]);
    cout << LCM * k[2] / gcd(LCM, k[2]) << endl;
  }
  return 0;
}
Problem p00116 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  int H, W;
  while (cin >> H >> W) {
    if (H == 0)
      break;
    vector<string> grid(H);
    int sum[501][501] = {0};
    REP(i, H) { cin >> grid[i]; }
    REP(y, H) REP(x, W) {
      sum[y + 1][x + 1] = sum[y + 1][x] + sum[y][x + 1] - sum[y][x];
      sum[y + 1][x + 1] += (grid[y][x] == '*') ? 1 : 0;
    }
    int ans = 0;
    REP(y1, H) REP(x1, W) FOR(y2, y1 + 1, H) FOR(x2, x1 + 1, W) {
      if (sum[x2 + 1][y2 + 1] - sum[x1][y2 + 1] - sum[x2 + 1][y1] +
              sum[x1][y1] ==
          0) {
        ans = max((x2 - x1 + 1) * (y2 - y1 + 1), ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  int H, W;
  while (cin >> H >> W) {
    if (H == 0)
      break;
    vector<string> grid(H);
    int sum[501][501] = {0};
    REP(i, H) { cin >> grid[i]; }
    REP(y, H) REP(x, W) {
      sum[y + 1][x + 1] = sum[y + 1][x] + sum[y][x + 1] - sum[y][x];
      sum[y + 1][x + 1] += (grid[y][x] == '*') ? 1 : 0;
    }
    int ans = 0;
    /*
    REP(y1,H)REP(x1,W)FOR(y2,y1+1,H)FOR(x2,x1+1,W){
      if(sum[x2+1][y2+1]-sum[x1][y2+1]
          -sum[x2+1][y1]+sum[x1][y1]==0){
        ans = max((x2-x1+1)*(y2-y1+1),ans);
      }
    }
    */
    bool f = false;
    FOR(dy, 1, H + 1) {
      FOR(dx, 1, W + 1) {
        f = false;
        FOR(y, dy - 1, H) {
          FOR(x, dx - 1, W) {
            if (sum[y + 1][x + 1] - sum[y + 1 - dy][x + 1] -
                    sum[y + 1][x + 1 - dx] + sum[y + 1 - dy][x + 1 - dx] ==
                0) {
              f = true;
              break;
            }
          }
          if (f)
            break;
        }
        if (f)
          ans = max(ans, dx * dy);
        else
          break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00116 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long

int getS(vector<int> &H) {
  int N = H.size();
  vector<int> L(N), R(N);
  stack<int> S;
  for (int i = 0; i < N; i++) {
    while (!S.empty() && H[i] <= H[S.top()])
      S.pop();
    L[i] = S.empty() ? 0 : (S.top() + 1);
    S.push(i);
  }
  S = stack<int>();
  for (int i = N - 1; i >= 0; i--) {
    while (!S.empty() && H[i] <= H[S.top()])
      S.pop();
    R[i] = S.empty() ? N : S.top();
    S.push(i);
  }
  int ma = 0;
  for (int i = 0; i < N; i++)
    ma = max(ma, (R[i] - L[i]) * H[i]);
  return ma;
}

int H, W;
vector<int> V[300];
void solve() {
  fill_n(V, H, vector<int>(W, 0));
  int ma = 0;
  for (int i = 0; i < H; i++) {
    char fld[510];
    cin >> fld;
    for (int j = 0; j < W; j++) {
      if (fld[j] == '*')
        continue;
      V[i][j] = (i ? V[i - 1][j] : 0) + 1;
    }
    ma = max(ma, getS(V[i]));
  }
  cout << ma << endl;
}
signed main() {
  while (cin >> H >> W, H || W)
    solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long

int getS(vector<int> &H) {
  int N = H.size();
  vector<int> L(N), R(N);
  stack<int> S;
  for (int i = 0; i < N; i++) {
    while (!S.empty() && H[i] <= H[S.top()])
      S.pop();
    L[i] = S.empty() ? 0 : (S.top() + 1);
    S.push(i);
  }
  S = stack<int>();
  for (int i = N - 1; i >= 0; i--) {
    while (!S.empty() && H[i] <= H[S.top()])
      S.pop();
    R[i] = S.empty() ? N : S.top();
    S.push(i);
  }
  int ma = 0;
  for (int i = 0; i < N; i++)
    ma = max(ma, (R[i] - L[i]) * H[i]);
  return ma;
}

int H, W;
vector<int> V[500];
void solve() {
  fill_n(V, H, vector<int>(W, 0));
  int ma = 0;
  for (int i = 0; i < H; i++) {
    char fld[510];
    cin >> fld;
    for (int j = 0; j < W; j++) {
      if (fld[j] == '*')
        continue;
      V[i][j] = (i ? V[i - 1][j] : 0) + 1;
    }
    ma = max(ma, getS(V[i]));
  }
  cout << ma << endl;
}
signed main() {
  while (cin >> H >> W, H || W)
    solve();
  return 0;
}
Problem p00116 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int h, w;

  while (1) {
    cin >> h >> w;
    if (!h && !w)
      break;

    vector<string> grid;
    for (int i = 0; i < h; i++) {
      string tmp;
      cin >> tmp;
      grid.push_back(tmp);
    }

    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (grid[i][j] == '.') {
          int x = w;
          int tmp = 0;
          for (int k = i; k < h; k++) {
            if (grid[k][j] == '*')
              break;
            int l;
            for (l = j; l < w; l++) {
              if (grid[k][l] == '*') {
                x = min(x, l);
                break;
              }
            }
            tmp = max(tmp, (x - j) * (k - i + 1));
          }
          ans = max(ans, tmp);
        }
      }
    }
    cout << ans << endl;
  }
}

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

int main() {
  int h, w;

  while (1) {
    cin >> h >> w;
    if (!h && !w)
      break;

    vector<string> grid;
    for (int i = 0; i < h; i++) {
      string tmp;
      cin >> tmp;
      grid.push_back(tmp);
    }

    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (grid[i][j] == '.') {
          int x = w;
          int tmp = 0;
          for (int k = i; k < h; k++) {
            if (grid[k][j] == '*')
              break;
            int l;
            for (l = j; l < x; l++) {
              if (grid[k][l] == '*') {
                x = min(x, l);
                break;
              }
            }
            tmp = max(tmp, (x - j) * (k - i + 1));
          }
          ans = max(ans, tmp);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00117 (C++)

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

using namespace std;
int N, M, A, B, C, D, x1, x2, y1, y2;
int K[32][32];
const int inf = 1001001001;

int main() {
  for (int i = 0; i < 32; ++i) {
    for (int j = 0; j < 32; ++j) {
      K[i][j] = inf;
    }
  }
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    scanf("%d,%d,%d,%d", &A, &B, &C, &D);
    // cerr << "read " << A << " " << B << " " << C << " " << D << endl;
    K[A - 1][B - 1] = C;
    K[B - 1][A - 1] = D;
  }

  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[i][j] > (K[i][k] + K[k][j])) {
          K[i][j] = K[i][k] + K[k][j];
        }
      }
    }
  }
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);

  cerr << y1 - K[x1 - 1][x2 - 1] - y2 - K[x2 - 1][x1 - 1] << endl;
}

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

using namespace std;
int N, M, A, B, C, D, x1, x2, y1, y2;
int K[32][32];
const int inf = 1001001001;

int main() {
  for (int i = 0; i < 32; ++i) {
    for (int j = 0; j < 32; ++j) {
      K[i][j] = inf;
    }
  }
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    scanf("%d,%d,%d,%d", &A, &B, &C, &D);
    // cerr << "read " << A << " " << B << " " << C << " " << D << endl;
    K[A - 1][B - 1] = C;
    K[B - 1][A - 1] = D;
  }

  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[i][j] > (K[i][k] + K[k][j])) {
          K[i][j] = K[i][k] + K[k][j];
        }
      }
    }
  }
  scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);

  cout << y1 - K[x1 - 1][x2 - 1] - y2 - K[x2 - 1][x1 - 1] << endl;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000000 + 7
using namespace std;

int H, W;
char fruit[15][15];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void dfs(int h, int w, char c) {
  fruit[h][w] = '.';
  for (int i = 0; i < 4; i++) {
    if (h + dx[i] >= 0 && h + dx[i] <= H && w + dy[i] >= 0 && w + dy[i] <= W &&
        fruit[h + dx[i]][w + dy[i]] == c) {
      dfs(h + dx[i], w + dy[i], c);
    }
  }
}
int main() {
  while (cin >> H >> W && H != 0 && W != 0) {
    string a;
    for (int i = 0; i < H; i++) {
      cin >> a;
      for (int j = 0; j < W; j++) {
        fruit[i][j] = a[j];
      }
    }
    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (fruit[i][j] == '#')
          dfs(i, j, '#'), ans++;
        if (fruit[i][j] == '*')
          dfs(i, j, '*'), ans++;
        if (fruit[i][j] == '@')
          dfs(i, j, '@'), ans++;
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000000 + 7
using namespace std;

int H, W;
char fruit[200][200];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void dfs(int h, int w, char c) {
  fruit[h][w] = '.';
  for (int i = 0; i < 4; i++) {
    if (h + dx[i] >= 0 && h + dx[i] <= H && w + dy[i] >= 0 && w + dy[i] <= W &&
        fruit[h + dx[i]][w + dy[i]] == c) {
      dfs(h + dx[i], w + dy[i], c);
    }
  }
}
int main() {
  while (cin >> H >> W && H != 0 && W != 0) {
    string a;
    for (int i = 0; i < H; i++) {
      cin >> a;
      for (int j = 0; j < W; j++) {
        fruit[i][j] = a[j];
      }
    }
    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (fruit[i][j] == '#')
          dfs(i, j, '#'), ans++;
        if (fruit[i][j] == '*')
          dfs(i, j, '*'), ans++;
        if (fruit[i][j] == '@')
          dfs(i, j, '@'), ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
//
// Created by yijiezhu on 12/20/16.
//

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

const int MAX_N = 21;

int m, n;
char matrix[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int cnt;

void dfs(int i, int j, char ch) {
  visited[i][j] = true;
  matrix[i][j] = (char)('a' + cnt - 1);
  int delta[] = {0, 1, 0, -1, 0};
  for (int q = 0; q < 4; q++) {
    int ni = i + delta[q], nj = j + delta[q + 1];
    if (ni < 0 || ni >= m || nj < 0 || nj >= n)
      continue;
    if (visited[ni][nj] || matrix[ni][nj] != ch)
      continue;
    dfs(ni, nj, ch);
  }
}

void solve() {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (!visited[i][j]) {
        cnt++;
        dfs(i, j, matrix[i][j]);
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (true) {
    cin >> m >> n;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cin >> matrix[i][j];
      }
    }
    cnt = 0;
    memset(visited, false, sizeof(visited));
    solve();
  }
}

Fixed submission:
//
// Created by yijiezhu on 12/20/16.
//

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

#define MAX_N 101

int m, n;
char matrix[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int cnt;

void dfs(int i, int j, char ch) {
  visited[i][j] = true;
  matrix[i][j] = (char)('a' + cnt - 1);
  int delta[] = {0, 1, 0, -1, 0};
  for (int q = 0; q < 4; q++) {
    int ni = i + delta[q], nj = j + delta[q + 1];
    if (ni < 0 || ni >= m || nj < 0 || nj >= n)
      continue;
    if (visited[ni][nj] || matrix[ni][nj] != ch)
      continue;
    dfs(ni, nj, ch);
  }
}

void solve() {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (!visited[i][j]) {
        cnt++;
        dfs(i, j, matrix[i][j]);
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (true) {
    cin >> m >> n;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cin >> matrix[i][j];
      }
    }
    cnt = 0;
    memset(visited, false, sizeof(visited));
    solve();
  }
}
Problem p00118 (C++)

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

int main() {
  const int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
  while (true) {
    int h, w;
    char fld[32][32];
    scanf("%d%d", &h, &w);
    if (h == 0)
      break;
    for (int i = 0; i < h; ++i)
      scanf("%s", fld[i]);
    int ans = 0;
    for (int sx = 0; sx < h; ++sx) {
      for (int sy = 0; sy < w; ++sy) {
        if (fld[sx][sy] == 'x')
          continue;
        char c = fld[sx][sy];
        queue<pair<int, int>> q;
        q.push(make_pair(sx, sy));
        while (!q.empty()) {
          int x = q.front().first, y = q.front().second;
          q.pop();
          for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx >= h || ny >= w)
              continue;
            if (fld[nx][ny] != c)
              continue;
            fld[nx][ny] = 'x';
            q.push(make_pair(nx, ny));
          }
        }
        ans += 1;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

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

int main() {
  const int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
  while (true) {
    int h, w;
    char fld[128][128];
    scanf("%d%d", &h, &w);
    if (h == 0)
      break;
    for (int i = 0; i < h; ++i)
      scanf("%s", fld[i]);
    int ans = 0;
    for (int sx = 0; sx < h; ++sx) {
      for (int sy = 0; sy < w; ++sy) {
        if (fld[sx][sy] == 'x')
          continue;
        char c = fld[sx][sy];
        queue<pair<int, int>> q;
        q.push(make_pair(sx, sy));
        while (!q.empty()) {
          int x = q.front().first, y = q.front().second;
          q.pop();
          for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx >= h || ny >= w)
              continue;
            if (fld[nx][ny] != c)
              continue;
            fld[nx][ny] = 'x';
            q.push(make_pair(nx, ny));
          }
        }
        ans += 1;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
#define MAX_N 100
void makemap(int h);
void seach();
void setblank(int x, int y, char c);
int h, w, ans;
string str[MAX_N];

int main() {
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    ans = 0;
    makemap(h);
    seach();
    cout << ans << endl;
  }
  return 0;
}

void makemap(int h) {
  for (int i = 0; i < h; i++) {
    cin >> str[i];
  }
}

void seach() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (str[i][j] == '#' || str[i][j] == '@' || str[i][j] == '*') {
        ans++;
        setblank(j, i, str[i][j]);
      }
    }
  }
}

void setblank(int x, int y, char c) {
  int movex[] = {0, 1, 0, -1}, movey[] = {-1, 0, 1, 0};
  for (int i = 0; i < 4; i++) {
    int nowx = x + movex[i], nowy = y + movey[i];
    if (nowx > w || nowx < 0 || nowy > h || nowy < 0 || str[nowy][nowx] != c)
      ;
    else {
      str[nowy][nowx] = '!';
      setblank(nowx, nowy, c);
    }
  }
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
#define MAX_N 100
void makemap(int h);
void seach();
void setblank(int x, int y, char c);
int h, w, ans;
string str[MAX_N];

int main() {
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    ans = 0;
    makemap(h);
    seach();
    cout << ans << endl;
  }
  return 0;
}

void makemap(int h) {
  for (int i = 0; i < h; i++) {
    cin >> str[i];
  }
}

void seach() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (str[i][j] == '#' || str[i][j] == '@' || str[i][j] == '*') {
        ans++;
        setblank(j, i, str[i][j]);
      }
    }
  }
}

void setblank(int x, int y, char c) {
  int movex[] = {0, 1, 0, -1}, movey[] = {-1, 0, 1, 0};
  for (int i = 0; i < 4; i++) {
    int nowx = x + movex[i], nowy = y + movey[i];
    if (nowx >= w || nowx < 0 || nowy >= h || nowy < 0 || str[nowy][nowx] != c)
      ;
    else {
      str[nowy][nowx] = '!';
      setblank(nowx, nowy, c);
    }
  }
}