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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define LL long long
#define ULL unsigned long long
#define N 100100
#define M 5010
#define MOD 1000000007
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define X first
#define Y second
using namespace std;
char st[N];
int dp[(1 << 26) + 10];
int pre[N];
int main() {
  scanf("%s", st + 1);
  int len = strlen(st + 1), ans = INF;
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= len; i++) {
    pre[i] = pre[i - 1] ^ (1 << (st[i] - 'a'));
    // printf("%d\n",pre[i]);
    for (int j = 0; j < 26; j++) {
      int sta = pre[i] ^ (1 << j);
      dp[pre[i]] = min(dp[pre[i]], dp[sta] + 1);
      // printf("(%d %d) ",sta,dp[sta]);
    }
    // printf("%d ",dp[pre[i]]);
    // printf("\n");
  }
  printf("%d\n", max(dp[pre[len]], 1));
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define LL long long
#define ULL unsigned long long
#define N 300100
#define M 5010
#define MOD 1000000007
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define X first
#define Y second
using namespace std;
char st[N];
int dp[(1 << 26) + 10];
int pre[N];
int main() {
  scanf("%s", st + 1);
  int len = strlen(st + 1), ans = INF;
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= len; i++) {
    pre[i] = pre[i - 1] ^ (1 << (st[i] - 'a'));
    // printf("%d\n",pre[i]);
    for (int j = 0; j < 26; j++) {
      int sta = pre[i] ^ (1 << j);
      dp[pre[i]] = min(dp[pre[i]], dp[sta] + 1);
      // printf("(%d %d) ",sta,dp[sta]);
    }
    // printf("%d ",dp[pre[i]]);
    // printf("\n");
  }
  printf("%d\n", max(dp[pre[len]], 1));
}
Problem p03570 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
string s;
int dp[(1 << 26) + 1];
long long now = 0;
int main() {
  cin >> s;
  memset(dp, 63, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i <= s.length() - 1; i++) {
    now ^= (1 << (s[i] - 'a' + 1));
    for (int j = 1; j <= 26; j++) {
      if ((now ^ (1 << j)) != now) {
        dp[now] = min(dp[now], dp[now ^ (1 << j)] + 1);
      }
    }
  }
  cout << max(1, dp[now]) << endl;
  return 0;
}
/*

in:
abcabcxabcx

out:
3

*/

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
string s;
int dp[(1 << 26) + 1];
long long now = 0;
int main() {
  cin >> s;
  memset(dp, 63, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i <= s.length() - 1; i++) {
    now ^= (1 << (s[i] - 'a'));
    for (int j = 0; j <= 25; j++) {
      if ((now ^ (1 << j)) != now) {
        dp[now] = min(dp[now], dp[now ^ (1 << j)] + 1);
      }
    }
  }
  cout << max(1, dp[now]) << endl;
  return 0;
}
/*

in:
abcabcxabcx

out:
3

*/
Problem p03570 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  int N = S.size();
  int M[67108864] = {};
  int A[200010] = {};
  bool odd[30] = {};
  for (int i = 1; i <= N; i++) {
    int c = S.at(i - 1) - 'a';
    if (odd[c]) {
      A[i] = A[i - 1] - (1 << c);
      odd[c] = false;
    } else {
      A[i] = A[i - 1] + (1 << c);
      odd[c] = true;
    }
  }
  M[0] = 1;
  for (int i = 1; i <= N; i++) {
    int tmp = 1145141919;
    if (M[A[i]])
      tmp = M[A[i]];
    for (int j = 0; j < 26; j++) {
      if (((A[i] >> j) & 1) && M[A[i] - (1 << j)]) {
        tmp = min(tmp, M[A[i] - (1 << j)] + 1);
      }
      if (!((A[i] >> j) & 1) && M[A[i] + (1 << j)]) {
        tmp = min(tmp, M[A[i] + (1 << j)] + 1);
      }
    }
    M[A[i]] = tmp;
  }
  cout << max(M[A[N]] - 1, 1) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<int> M(67108864);
  int A[200010] = {};
  bool odd[30] = {};
  for (int i = 1; i <= N; i++) {
    int c = S.at(i - 1) - 'a';
    if (odd[c]) {
      A[i] = A[i - 1] - (1 << c);
      odd[c] = false;
    } else {
      A[i] = A[i - 1] + (1 << c);
      odd[c] = true;
    }
  }
  M[0] = 1;
  for (int i = 1; i <= N; i++) {
    int tmp = 1145141919;
    if (M[A[i]])
      tmp = M[A[i]];
    for (int j = 0; j < 26; j++) {
      if (((A[i] >> j) & 1) && M[A[i] - (1 << j)]) {
        tmp = min(tmp, M[A[i] - (1 << j)] + 1);
      }
      if (!((A[i] >> j) & 1) && M[A[i] + (1 << j)]) {
        tmp = min(tmp, M[A[i] + (1 << j)] + 1);
      }
    }
    M[A[i]] = tmp;
  }
  cout << max(M[A[N]] - 1, 1) << endl;
}
Problem p03570 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  int cur = 0;
  int dp[n + 1];
  int mn[1 << 26];
  fill(dp, dp + n + 1, INF);
  fill(mn, mn + (1 << 26), INF);
  dp[0] = 0;
  mn[0] = 0;
  for (int i = 0; i < n; i++) {
    cur ^= 1 << (s[i] - 'a');
    dp[i + 1] = min(dp[i + 1], mn[cur] + 1);
    for (int c = 0; c < 26; c++) {
      int nxt = cur ^ (1 << c);
      dp[i + 1] = min(dp[i + 1], mn[nxt] + 1);
    }
    mn[cur] = min(mn[cur], dp[i + 1]);
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  int cur = 0;
  vector<int> dp(n + 1, INF);
  vector<int> mn(1 << 26, INF);
  dp[0] = 0;
  mn[0] = 0;
  for (int i = 0; i < n; i++) {
    cur ^= 1 << (s[i] - 'a');
    dp[i + 1] = min(dp[i + 1], mn[cur] + 1);
    for (int c = 0; c < 26; c++) {
      int nxt = cur ^ (1 << c);
      dp[i + 1] = min(dp[i + 1], mn[nxt] + 1);
    }
    mn[cur] = min(mn[cur], dp[i + 1]);
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03570 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
int op[maxn], a[maxn], str[30];
int dp[1 << 26];
#define inf 999999
int main() {
  string s;
  int n;
  cin >> s;
  n = s.size();
  memset(str, 0, sizeof(str));
  for (int i = 0; i < n; i++) {
    int bit = 0;
    str[s[i] - 'a']++;
    for (int j = 0; j < 26; j++) {
      if (str[j] % 2) {
        bit += (1 << j);
      }
    }
    a[i + 1] = bit; // 表示前i个长度的字串，都有哪些字母是奇数个;
  }
  for (int i = 0; i < 1 << 26; i++) {
    dp[i] = inf; // 表示在i状态下，能够满足题意得最少分解数目
  }
  for (int i = 0; i <= n; i++)
    op[i] = inf;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    op[i] = min(op[i], dp[a[i]] + 1); // 最差的情况是前i-1的最佳结果加上1
    for (int j = 0; j < 26; j++) {
      op[i] = min(
          op[i],
          dp[a[i] ^ (1 << j)] +
              1); // 转移到（异或上任意一个字符再加上当前字符就最小）的那个状态
    }
    dp[a[i]] = min(dp[a[i]], op[i]); // 更新当前状态
  }
  cout << op[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 200005
int op[maxn], a[maxn], str[30];
int dp[1 << 26];
#define inf 999999
int main() {
  string s;
  int n;
  cin >> s;
  n = s.size();
  memset(str, 0, sizeof(str));
  for (int i = 0; i < n; i++) {
    int bit = 0;
    str[s[i] - 'a']++;
    for (int j = 0; j < 26; j++) {
      if (str[j] % 2) {
        bit += (1 << j);
      }
    }
    a[i + 1] = bit; // 表示前i个长度的字串，都有哪些字母是奇数个;
  }
  for (int i = 0; i < 1 << 26; i++) {
    dp[i] = inf; // 表示在i状态下，能够满足题意得最少分解数目
  }
  for (int i = 0; i <= n; i++)
    op[i] = inf;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    op[i] = min(op[i], dp[a[i]] + 1); // 最差的情况是前i-1的最佳结果加上1
    for (int j = 0; j < 26; j++) {
      op[i] = min(
          op[i],
          dp[a[i] ^ (1 << j)] +
              1); // 转移到（异或上任意一个字符再加上当前字符就最小）的那个状态
    }
    dp[a[i]] = min(dp[a[i]], op[i]); // 更新当前状态
  }
  cout << op[n] << endl;
  return 0;
}
Problem p03570 (Python)

Buggy submission (status: Time Limit Exceeded):
from string import ascii_lowercase

cd = {c: 1 << i for i, c in enumerate(ascii_lowercase)}
bt = set(cd.values())
bt.add(0)


def solve(s):
    q = {s}
    n = 0
    while True:
        nq = set()
        for s in q:
            if not s:
                return n
            b = 0
            for i, c in enumerate(s):
                b ^= cd[c]
                if b in bt:
                    nq.add(s[i + 1 :])
        q = nq
        n += 1


print(solve(input()))


Fixed submission:
from string import ascii_lowercase

cd = {c: 1 << i for i, c in enumerate(ascii_lowercase)}
bt = set(cd.values())
bt.add(0)


def solve(s):
    a, b = [], 0
    for c in s:
        b ^= cd[c]
        a.append(b)
    if a[-1] == 0:
        return 1

    dp = {0: 0}
    for b1 in a:
        for b2 in bt:
            b3 = b1 ^ b2
            if b3 not in dp:
                continue
            if b1 in dp:
                dp[b1] = min(dp[b1], dp[b3] + 1)
            else:
                dp[b1] = dp[b3] + 1
    return dp[a[-1]]


print(solve(input()))
Problem p03573 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import Counter

sys.stdin = open("a1.in")


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    n = read_int_list()
    c = Counter(n)
    for i in c:
        if c[i] == 1:
            print(i)


main()


Fixed submission:
import sys
from collections import Counter

# sys.stdin = open('a1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    n = read_int_list()
    c = Counter(n)
    for i in c:
        if c[i] == 1:
            print(i)


main()
Problem p03573 (Python)

Buggy submission (status: Runtime Error):
a = list(map(int, input().split()))
if a[0] == a[1]:
    print(a[3])
elif a[0] == a[2]:
    print(a[1])
elif a[1] == a[2]:
    print(a[0])


Fixed submission:
a = list(map(int, input().split()))
if a[0] == a[1]:
    print(a[2])
elif a[0] == a[2]:
    print(a[1])
elif a[1] == a[2]:
    print(a[0])
Problem p03573 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> data(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char a;
      cin >> a;
      if (a == '.')
        data.at(i).at(j) = 0;
      else
        data.at(i).at(j) = -1;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (data.at(i).at(j) == 0) {
        int count = 0;
        if (i != 0) {
          if (data.at(i - 1).at(j) == -1)
            count++;
        }
        if (i != 0 && j != 0) {
          if (data.at(i - 1).at(j - 1) == -1)
            count++;
        }
        if (i != 0 && j != W - 1) {
          if (data.at(i - 1).at(j + 1) == -1)
            count++;
        }
        if (j != 0) {
          if (data.at(i).at(j - 1) == -1)
            count++;
        }
        if (j != W - 1) {
          if (data.at(i).at(j + 1) == -1)
            count++;
        }
        if (i != H - 1) {
          if (data.at(i + 1).at(j) == -1)
            count++;
        }
        if (i != H - 1 && j != 0) {
          if (data.at(i + 1).at(j - 1) == -1)
            count++;
        }
        if (i != H - 1 && j != W - 1) {
          if (data.at(i + 1).at(j + 1) == -1)
            count++;
        }
        data.at(i).at(j) = count;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (data.at(i).at(j) == -1)
        cout << "#";
      else
        cout << data.at(i).at(j);
    }
    cout << endl;
  }
}


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

int main() {
  int A, B, C;
  cin >> A >> B >> C;
  if (A == B)
    cout << C << endl;
  else if (A == C)
    cout << B << endl;
  else
    cout << A << endl;
}
Problem p03573 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;

  // 0行目 S_above
  string S_above = "..";
  for (int i = 0; i < W; i++)
    S_above += '.';

  // 1～H行目
  string S, S_below;
  cin >> S;
  S = '.' + S + '.';
  for (int i = 1; i < H; i++) { // 　iは行番号 最下行は別に処理
    cin >> S_below;
    S_below = '.' + S_below + '.';
    for (int j = 1; j < W + 1; j++) { // 　行の処理　jは文字番号
      int k = 0;                      // kは#の数
      if (S.at(j) == '#')
        cout << '#';
      else {
        if (S_above.at(j - 1) == '#')
          k += 1;
        if (S_above.at(j) == '#')
          k += 1;
        if (S_above.at(j + 1) == '#')
          k += 1;
        if (S.at(j - 1) == '#')
          k += 1;
        if (S.at(j + 1) == '#')
          k += 1;
        if (S_below.at(j - 1) == '#')
          k += 1;
        if (S_below.at(j) == '#')
          k += 1;
        if (S_below.at(j + 1) == '#')
          k += 1;
        cout << k;
      }
    } // 各行ごとの処理おわり
    cout << endl;
    S_above = S;
    S = S_below;
  }
  cout << endl;
  // H-1行目まで処理おわり

  // H行目の処理
  for (int j = 1; j < W + 1; j++) { // 　jは文字番号
    int k = 0;                      // kは#の数
    if (S.at(j) == '#')
      cout << '#';
    else {
      if (S_above.at(j - 1) == '#')
        k += 1;
      if (S_above.at(j) == '#')
        k += 1;
      if (S_above.at(j + 1) == '#')
        k += 1;
      if (S.at(j - 1) == '#')
        k += 1;
      if (S.at(j + 1) == '#')
        k += 1;
      cout << k;
    }
  }
  cout << endl;
}


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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  if (a == b)
    cout << c;
  if (b == c)
    cout << a;
  if (c == a)
    cout << b;
}
Problem p03573 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> s(H + 2, vector<char>(W + 2));
  //  vector<vector<int>> c(H+2, vector<int>(W+2));
  for (int i = 0; i < H + 2; i++) {
    for (int j = 1; j < W + 2; j++) {
      s.at(i).at(j) = '.';
    }
  }

  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cin >> s.at(i).at(j);
    }
  }

  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      int bomb = 0;
      if (s.at(i).at(j) != '#') {
        for (int k = -1; k < 2; k++) {
          for (int l = -1; l < 2; l++) {
            if (k != 0 || l != 0) {
              if (s.at(i + k).at(j + l) == '#')
                bomb++;
            }
          }
        }
        s.at(i).at(j) = (char)bomb;
      }
    }
  }

  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cout << s.at(i).at(j);
    }
    cout << endl;
  }
}


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

int main() {
  int A, B, C;
  cin >> A >> B >> C;
  if (A == B)
    cout << C << endl;
  if (B == C)
    cout << A << endl;
  if (A == C)
    cout << B << endl;
}
Problem p03573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll tmp, n, m, i, j;

int main() {
  ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  cin >> n >> m >> i;
  cout << (n ^ m ^ i);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll tmp, n, m, i, j;

int main() {
  // Create graphs given in above diagrams

  cin >> n >> m >> i;
  cout << (n ^ m ^ i);

  return 0;
}
Problem p03573 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;

  vector<string> board(50);
  for (int i = 0; i < h; ++i)
    cin >> board.at(i);

  const vector<int> dx = {1, 0, -1, 0, 1, 1, -1, -1};
  const vector<int> dy = {0, 1, 0, -1, 1, -1, 1, -1};

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (board.at(i).at(j) == '#')
        continue;

      int n = 0;
      for (int k = 0; k < 8; ++k) {
        const int ni = i + dy.at(k);
        const int nj = j + dx.at(k);
        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;

        if (board.at(ni).at(nj) == '#')
          n++;
      }

      board.at(i).at(j) = char(n + '0');
    }
  }
  for (int i = 0; i < h; ++i)
    cout << board.at(i) << endl;
}


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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  if (a == b)
    cout << c << endl;
  else if (b == c)
    cout << a << endl;
  else
    cout << b << endl;
}
Problem p03573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B, C;
  cin >> A >> B >> C;

  vector<int> vec = {A, B, C};
  sort(vec.begin(), vec.end());

  if (vec.at(0) == vec.at(1)) {
    cout << vec.at(3) << endl;
  } else if (vec.at(1) == vec.at(2)) {
    cout << vec.at(0) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B, C;
  cin >> A >> B >> C;

  vector<int> vec = {A, B, C};
  sort(vec.begin(), vec.end());

  if (vec.at(0) == vec.at(1)) {
    cout << vec.at(2) << endl;
  } else if (vec.at(1) == vec.at(2)) {
    cout << vec.at(0) << endl;
  }
}
Problem p03574 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  string s[60];
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '#')
        continue;

      int counter = 0;
      for (int d = 0; d < 8; ++d) {
        const int ni = i + dx[d];
        const int nj = j + dy[d];

        if (s[ni][nj] == '#')
          ++counter;
      }

      s[i][j] = '0' + counter;
    }
  }

  for (int i = 0; i < h; ++i)
    cout << s[i] << endl;
}


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

int main() {
  int h, w;
  cin >> h >> w;
  string s[60];
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '#')
        continue;

      int counter = 0;
      for (int d = 0; d < 8; ++d) {
        const int ni = i + dx[d];
        const int nj = j + dy[d];

        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (s[ni][nj] == '#')
          ++counter;
      }

      s[i][j] = '0' + counter;
    }
  }

  for (int i = 0; i < h; ++i)
    cout << s[i] << endl;
}
Problem p03574 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  string s[50];
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int buff[51][51];
  for (int i = 0; i < 51; i++)
    for (int j = 0; j < 51; j++)
      buff[i][j] = 0;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        // buffの[i][j]から[i+2][j+2]までインクリメント
        for (int x = 0; x < 3; x++) {
          for (int y = 0; y < 3; y++) {
            buff[i + x][j + y]++;
          }
        }
      }
    }
  }
  // output
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        cout << '#';
      else
        cout << buff[i + 1][j + 1];
    }
    cout << endl;
  }
}


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

int main() {
  int h, w;
  cin >> h >> w;
  string s[50];
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int buff[52][52];
  for (int i = 0; i < 52; i++)
    for (int j = 0; j < 52; j++)
      buff[i][j] = 0;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        // buffの[i][j]から[i+2][j+2]までインクリメント
        for (int x = 0; x < 3; x++) {
          for (int y = 0; y < 3; y++) {
            buff[i + x][j + y]++;
          }
        }
      }
    }
  }
  // output
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        cout << '#';
      else
        cout << buff[i + 1][j + 1];
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int h, w;
  char s[52][52];
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[j][i];
      if (s[j][i] == '.')
        s[j][i] = '0';
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[j][i] == '#') {
        if (s[j + 1][i] != '#' && s[j + 1][i] != ' ')
          s[j + 1][i]++;
        if (s[j - 1][i] != '#' && s[j - 1][i] != ' ')
          s[j - 1][i]++;
        if (s[j][i + 1] != '#' && s[j][i + 1] != ' ')
          s[j][i + 1]++;
        if (s[j][i - 1] != '#' && s[j][i - 1] != ' ')
          s[j][i - 1]++;
        if (s[j + 1][i + 1] != '#' && s[j + 1][i + 1] != ' ')
          s[j + 1][i + 1]++;
        if (s[j + 1][i - 1] != '#' && s[j + 1][i - 1] != ' ')
          s[j + 1][i - 1]++;
        if (s[j - 1][i + 1] != '#' && s[j - 1][i + 1] != ' ')
          s[j - 1][i + 1]++;
        if (s[j - 1][i - 1] != '#' && s[j - 1][i - 1] != ' ')
          s[j - 1][i - 1]++;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << s[j][i];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int h, w;
  char s[52][52];
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[j][i];
      if (s[j][i] == '.')
        s[j][i] = '0';
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[j][i] != '#') {
        if (s[j + 1][i] == '#')
          s[j][i]++;
        if (s[j - 1][i] == '#')
          s[j][i]++;
        if (s[j][i + 1] == '#')
          s[j][i]++;
        if (s[j][i - 1] == '#')
          s[j][i]++;
        if (s[j + 1][i + 1] == '#')
          s[j][i]++;
        if (s[j + 1][i - 1] == '#')
          s[j][i]++;
        if (s[j - 1][i + 1] == '#')
          s[j][i]++;
        if (s[j - 1][i - 1] == '#')
          s[j][i]++;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << s[j][i];
    }
    cout << endl;
  }
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> s(w + 2, vector<char>(h + 2, '.'));
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j] == '#') {
        cout << s[i][j];
      } else {
        int count = 0;
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            if (s[i + k][j + l] == '#') {
              count++;
            }
          }
        }
        cout << count;
      }
    }
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;

  char s[52][52];

  for (int i = 0; i < 52; i++) {
    for (int j = 0; j < 52; j++) {
      s[i][j] = '.';
    }
  }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j] == '#') {
        cout << s[i][j];
      } else {
        int count = 0;
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            if (s[i + k][j + l] == '#') {
              count++;
            }
          }
        }
        cout << count;
      }
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(), (a).end()

using namespace std;

int main(int argc, char const *argv[]) {
  int H, W;
  scanf("%d%d", &H, &W);
  bool S[H + 2][W + 2];
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      S[i][j] = false;
    }
  }
  string temp;
  for (int i = 1; i <= H; i++) {
    std::cin >> temp;
    for (int j = 1; j <= W; j++) {
      if (temp[j - 1] == '#') {
        S[i][j] = true;
      } else {
        S[i][j] = false;
      }
    }
  }

  int ans[H][W], bomb;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (S[i][j] == true) {
        ans[i][j] = -1;
      } else {
        bomb = 0;
        for (int k = i; k < i + 3; k++) {
          for (int z = j; z < j + 3; z++) {
            if (S[k - 1][z - 1] == true) {
              ++bomb;
            }
          }
        }
        ans[i][j] = bomb;
      }
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (ans[i][j] == -1) {
        printf("#");
      } else {
        printf("%d", ans[i][j]);
      }
    }
    printf("\n");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(), (a).end()

using namespace std;

int main(int argc, char const *argv[]) {
  int H, W;
  scanf("%d%d", &H, &W);
  bool S[H + 2][W + 2];
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      S[i][j] = false;
    }
  }
  string temp;
  for (int i = 1; i <= H; i++) {
    std::cin >> temp;
    for (int j = 1; j <= W; j++) {
      if (temp[j - 1] == '#') {
        S[i][j] = true;
      } else {
        S[i][j] = false;
      }
    }
  }

  int ans[H + 2][W + 2], bomb;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (S[i][j] == true) {
        ans[i][j] = -1;
      } else {
        bomb = 0;
        for (int k = i; k < i + 3; k++) {
          for (int z = j; z < j + 3; z++) {
            if (S[k - 1][z - 1] == true) {
              ++bomb;
            }
          }
        }
        ans[i][j] = bomb;
      }
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (ans[i][j] == -1) {
        printf("#");
      } else {
        printf("%d", ans[i][j]);
      }
    }
    printf("\n");
  }
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int H, W, count;
  cin >> H >> W;
  vector<vector<char>> input(W + 2, vector<char>(H + 2));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> input[i + 1][j + 1];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      count = 0;
      if (input[i + 1][j + 1] == '.') {
        for (int k = i; k < i + 3; k++) {
          for (int l = j; l < j + 3; l++) {
            if (input[k][l] == '#') {
              count++;
            }
          }
        }
        input[i + 1][j + 1] = (char)(count + 48);
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << input[i + 1][j + 1];
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int H, W, count;
  cin >> H >> W;
  vector<vector<char>> input(H + 2, vector<char>(W + 2));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> input[i + 1][j + 1];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      count = 0;
      if (input[i + 1][j + 1] == '.') {
        for (int k = i; k < i + 3; k++) {
          for (int l = j; l < j + 3; l++) {
            if (input[k][l] == '#') {
              count++;
            }
          }
        }
        input[i + 1][j + 1] = (char)(count + 48);
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << input[i + 1][j + 1];
    }
    cout << endl;
  }
}
Problem p03574 (Python)

Buggy submission (status: Runtime Error):
H, W = map(int, input().split())
S = ["." * (W + 2)] + ["." + input() + "." for _ in range(H)] + ["." * (W + 2)]
ans = [[] for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == "#":
            ans[h - 1].append("#")
        else:
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == "#" for n in N]))

for h in range(H):
    print("".join(ans[h]))


Fixed submission:
H, W = map(int, input().split())
S = ["." * (W + 2)] + ["." + input() + "." for _ in range(H)] + ["." * (W + 2)]
ans = [[] for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == "#":
            ans[h - 1].append("#")
        else:
            ans[h - 1].append(str(sum([S[h + nh][w + nw] == "#" for nh, nw in N])))
for h in range(H):
    print("".join(ans[h]))
Problem p03574 (Python)

Buggy submission (status: Runtime Error):
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))

d = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for r in range(8):
                x = i + d[r][0]
                y = j + d[r][1]
                if 0 <= x < W and 0 <= y < H and S[x][y] == "#":
                    cnt = cnt + 1
            S[i][j] = str(cnt)

for i in range(H):
    print("".join(S[i]))


Fixed submission:
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))

d = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for r in range(8):
                y = i + d[r][0]
                x = j + d[r][1]
                if 0 <= x < W and 0 <= y < H and S[y][x] == "#":
                    cnt = cnt + 1
            S[i][j] = str(cnt)

for i in range(H):
    print("".join(S[i]))
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
#define p(x) cout << (x) << endl

int mine_counter(const vector<string> s, int i, int j) {
  int count = 0;
  int hi, wi;
  hi = s.size();
  wi = s[0].size();

  for (int l = max(0, i - 1); l <= min(wi - 1, i + 1); l++) {
    for (int m = max(0, j - 1); m <= min(hi - 1, j + 1); m++) {
      if (s[l][m] == '#') {
        count++;
      }
    }
  }

  return count;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);

  for (auto &e : s) {
    cin >> e;
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] != '#') {
        s[i][j] = '0' + mine_counter(s, i, j);
      }
    }
  }

  for (auto &e : s) {
    p(e);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
#define p(x) cout << (x) << endl

int mine_counter(const vector<string> s, int i, int j) {
  int count = 0;
  int hi, wi;
  hi = s.size();
  wi = s[0].size();

  for (int l = max(0, i - 1); l <= min(hi - 1, i + 1); l++) {
    for (int m = max(0, j - 1); m <= min(wi - 1, j + 1); m++) {
      if (s[l][m] == '#') {
        count++;
      }
    }
  }

  return count;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);

  for (auto &e : s) {
    cin >> e;
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] != '#') {
        s[i][j] = '0' + mine_counter(s, i, j);
      }
    }
  }

  for (auto &e : s) {
    p(e);
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int h, w;
  cin >> h >> w;
  int a[h][w];
  rep(i, h) rep(j, w) a[i][j] = 0;
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] == '#') {
      a[i][j] = -100;
      rep(k, 3) rep(l, 3)++ a[i - 1 + k][j - 1 + l];
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (a[i][j] < 0)
        cout << '#';
      else
        cout << a[i][j];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int h, w;
  cin >> h >> w;
  int a[h][w];
  rep(i, h) rep(j, w) a[i][j] = 0;
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] == '#') {
      a[i][j] = -100;
      rep(k, 3) rep(l, 3) {
        if (i - 1 + k >= 0 && i - 1 + k < h && j - 1 + l >= 0 && j - 1 + l < w)
          ++a[i - 1 + k][j - 1 + l];
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (a[i][j] < 0)
        cout << '#';
      else
        cout << a[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p03574 (C++)

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

using namespace std;

int main() {
  int H, W;
  vector<string> S(50);
  int count;

  cin >> H >> W;

  for (int j = 0; j < W + 2; j++)
    S[0] += ".";

  for (int i = 1; i <= H; i++) {
    cin >> S[i];
    S[i] = "." + S[i] + ".";
  }

  for (int j = 0; j < W + 2; j++)
    S[H + 1] += ".";

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      count = 0;
      if (S[i][j] == '#') {
        cout << '#';
        continue;
      }
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          if (S[i + y][j + x] == '#')
            count++;
        }
      }
      cout << count;
    }
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  int H, W;
  vector<string> S(52);
  int count;

  cin >> H >> W;

  for (int j = 0; j < W + 2; j++)
    S[0] += ".";

  for (int i = 1; i <= H; i++) {
    cin >> S[i];
    S[i] = "." + S[i] + ".";
  }

  for (int j = 0; j < W + 2; j++)
    S[H + 1] += ".";

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      count = 0;
      if (S[i][j] == '#') {
        cout << '#';
        continue;
      }
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          if (S[i + y][j + x] == '#')
            count++;
        }
      }
      cout << count;
    }
    cout << endl;
  }

  return 0;
}
Problem p03574 (C++)

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

#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++) // 0 ~ n-1

int main() {
  // input
  int H, W;
  cin >> H >> W;
  vector<string> vec(H);
  REP(i, H) vec[i].reserve(W);
  REP(i, H) cin >> vec[i];

  // calculation
  int cnt;
  REP(i, H) {
    REP(j, W) {
      if (vec[i][j] == '.') {
        cnt = 0;
        for (int k = i - 1; k <= i + 1; ++k) {
          for (int l = j - 1; l <= j + 1; ++l) {
            if (k < 0 || l < 0) {
              continue;
            } else if (vec[k][l] == '#') {
              cnt++;
            }
          }
        }
        cout << cnt;
      } else {
        cout << '#';
      }
    }
    cout << "\n";
  }

  return 0;
}

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

#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++) // 0 ~ n-1

int main() {
  // input
  int H, W;
  cin >> H >> W;
  char vec[55][55];
  for (int i = 0; i < H; i++)
    scanf("%s", vec[i]);

  // calculation
  int cnt;
  REP(i, H) {
    REP(j, W) {
      if (vec[i][j] == '.') {
        cnt = 0;
        for (int k = i - 1; k <= i + 1; ++k) {
          for (int l = j - 1; l <= j + 1; ++l) {
            if (k < 0 || l < 0) {
              continue;
            } else if (vec[k][l] == '#') {
              cnt++;
            }
          }
        }
        cout << cnt;
      } else {
        cout << '#';
      }
    }
    cout << "\n";
  }

  return 0;
}
Problem p03574 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  int a[60][60];
  for (int i = 0; i <= H + 1; ++i)
    for (int j = 0; j <= j + 1; ++j)
      a[i][j] = 0;
  string S;
  for (int i = 0; i < H; ++i) {
    cin >> S;
    for (int j = 0; j < W; ++j) {
      if (S[j] == '#') {
        for (int k = i; k < i + 3; ++k) {
          ++a[k][j];
          ++a[k][j + 1];
          ++a[k][j + 2];
        }
        a[i + 1][j + 1] = -100;
      }
    }
  }
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (a[i][j] < 0)
        cout << '#';
      else
        cout << a[i][j];
    }
    cout << endl;
  }
  return 0;
}


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

int main() {
  int H, W;
  cin >> H >> W;
  int a[60][60];
  for (int i = 0; i <= H + 1; ++i)
    for (int j = 0; j <= W + 1; ++j)
      a[i][j] = 0;
  string S;
  for (int i = 0; i < H; ++i) {
    cin >> S;
    for (int j = 0; j < W; ++j) {
      if (S[j] == '#') {
        for (int k = i; k < i + 3; ++k) {
          ++a[k][j];
          ++a[k][j + 1];
          ++a[k][j + 2];
        }
        a[i + 1][j + 1] = -100;
      }
    }
  }
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (a[i][j] < 0)
        cout << '#';
      else
        cout << a[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  vector<string> s(h + 2);
  rep(i, h) {
    string S;
    cin >> S;
    s.at(i + 1) = ' ' + S + ' ';
  }
  rep(i, h) {
    rep(j, w) {
      if (s[i + 1][j + 1] == '.') {
        int cnt = 0;
        rep(k, 3) rep(l, 3) if (s[i + k][j + l] == '#') cnt++;
        s.at(i + 1).at(j + 1) = char(cnt + '0');
      }
    }
  }
  rep(i, h) {
    s.at(i + 1) = s.at(i + 1).erase(h + 1);
    cout << s.at(i + 1).substr(1) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  vector<string> s(h + 2);
  rep(i, h) {
    string S;
    cin >> S;
    s.at(i + 1) = ' ' + S + ' ';
  }
  rep(i, h) {
    rep(j, w) {
      if (s[i + 1][j + 1] == '.') {
        int cnt = 0;
        rep(k, 3) rep(l, 3) if (s[i + k][j + l] == '#') cnt++;
        s.at(i + 1).at(j + 1) = char(cnt + '0');
      }
    }
  }
  rep(i, h) {
    s.at(i + 1) = s.at(i + 1).erase(w + 1);
    cout << s.at(i + 1).substr(1) << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SORT(a) sort(a.begin(), a.end())
#define RSORT(a) sort(a.rbegin(), a.rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n; 0 <= i; i--)
#define FOR(i, start, end) for (int i = start; i < end; i++)
#define RFOR(i, start, end) for (int i = start; end <= i; i--)
#define ALL(a) a.begin(), a.end()
#define MOD(a) a %= 1000000007
using ll = long long;
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

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

  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, H) { cin >> S[i]; }

  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        continue;
      int cnt = 0;
      REP(k, 8) {
        int y = i + dy[k];
        int x = j + dx[k];
        if (y < 0 || H <= y)
          continue;
        if (x < 0 || W <= x)
          continue;
        if (S[y][x] == '#')
          cnt++;
      }
      S[i][j] = '0' + cnt;
    }
  }

  REP(i, W) std::cout << S[i] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define SORT(a) sort(a.begin(), a.end())
#define RSORT(a) sort(a.rbegin(), a.rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n; 0 <= i; i--)
#define FOR(i, start, end) for (int i = start; i < end; i++)
#define RFOR(i, start, end) for (int i = start; end <= i; i--)
#define ALL(a) a.begin(), a.end()
#define MOD(a) a %= 1000000007
using ll = long long;
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

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

  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, H) { cin >> S[i]; }

  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        continue;
      int cnt = 0;
      REP(k, 8) {
        int y = i + dy[k];
        int x = j + dx[k];
        if (y < 0 || H <= y)
          continue;
        if (x < 0 || W <= x)
          continue;
        if (S[y][x] == '#')
          cnt++;
      }
      S[i][j] = '0' + cnt;
    }
  }

  REP(i, H) std::cout << S[i] << '\n';
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> data1(50, vector<char>(50));
  vector<vector<int>> data2(50, vector<int>(50));

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> data1.at(i).at(j);
      if (data1.at(i).at(j) == '#') {
        if (i - 1 >= 0)
          data2.at(i - 1).at(j)++;
        if (i - 1 >= 0 && j - 1 >= 0)
          data2.at(i - 1).at(j - 1)++;
        if (i - 1 >= 0 && j + 1 <= W)
          data2.at(i - 1).at(j + 1)++;
        if (j + 1 <= W && i + 1 <= H)
          data2.at(i + 1).at(j + 1)++;
        if (j + 1 <= W)
          data2.at(i).at(j + 1)++;
        if (j - 1 >= 0 && i + 1 <= H)
          data2.at(i + 1).at(j - 1)++;
        if (i + 1 <= H)
          data2.at(i + 1).at(j)++;
        if (j - 1 >= 0)
          data2.at(i).at(j - 1)++;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (data1.at(i).at(j) == '#')
        cout << data1.at(i).at(j);
      else
        cout << data2.at(i).at(j);
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> data1(50, vector<char>(50));
  vector<vector<int>> data2(50, vector<int>(50));

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> data1.at(i).at(j);
      if (data1.at(i).at(j) == '#') {
        if (j + 1 < W)
          data2.at(i).at(j + 1)++; // right
        if (j >= 1)
          data2.at(i).at(j - 1)++; // left
        if (i + 1 < H)
          data2.at(i + 1).at(j)++; // up
        if (i >= 1)
          data2.at(i - 1).at(j)++; // down
        if (j + 1 < W && i >= 1)
          data2.at(i - 1).at(j + 1)++; // right down
        if (j + 1 < W && i + 1 < H)
          data2.at(i + 1).at(j + 1)++; // right up
        if (j >= 1 && i >= 1)
          data2.at(i - 1).at(j - 1)++; // left down
        if (j >= 1 && i + 1 < H)
          data2.at(i + 1).at(j - 1)++; // left up
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (data1.at(i).at(j) == '#')
        cout << data1.at(i).at(j);
      else
        cout << data2.at(i).at(j);
    }
    cout << endl;
  }
  return 0;
}
Problem p03574 (C++)

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

int main() {
  //\W×Hのマスの周を"."で囲んでやり、中身だけを出力;
  int h, w, count[50][50];
  char s[55][55];

  cin >> h >> w; // s[i][j]=マス(i,j);
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j++) {
      count[i][j] = 0;
    }
  }

  for (int i = 0; i < h + 2; i += h + 1) {
    for (int j = 0; j < w + 2; j++) {
      s[i][j] = '.';
    }
  }
  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j += w + 1) {
      s[i][j] = '.';
    }
  }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j + 1] == '#')
        count[i][j]++;
      if (s[i][j - 1] == '#')
        count[i][j]++;
      if (s[i + 1][j] == '#')
        count[i][j]++;
      if (s[i - 1][j] == '#')
        count[i][j]++;
      if (s[i + 1][j + 1] == '#')
        count[i][j]++;
      if (s[i - 1][j - 1] == '#')
        count[i][j]++;
      if (s[i + 1][j - 1] == '#')
        count[i][j]++;
      if (s[i - 1][j + 1] == '#')
        count[i][j]++;
    }
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j] == '#')
        cout << "#";
      else
        cout << count[i][j];
    }
    cout << endl;
  }
}

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

int main() {
  //\W×Hのマスの周を"."で囲んでやり、中身だけを出力;
  int h, w, count[55][55];
  char s[55][55];

  cin >> h >> w; // s[i][j]=マス(i,j);
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j++) {
      count[i][j] = 0;
    }
  }

  for (int i = 0; i < h + 2; i += h + 1) {
    for (int j = 0; j < w + 2; j++) {
      s[i][j] = '.';
    }
  }
  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j += w + 1) {
      s[i][j] = '.';
    }
  }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j + 1] == '#')
        count[i][j]++;
      if (s[i][j - 1] == '#')
        count[i][j]++;
      if (s[i + 1][j] == '#')
        count[i][j]++;
      if (s[i - 1][j] == '#')
        count[i][j]++;
      if (s[i + 1][j + 1] == '#')
        count[i][j]++;
      if (s[i - 1][j - 1] == '#')
        count[i][j]++;
      if (s[i + 1][j - 1] == '#')
        count[i][j]++;
      if (s[i - 1][j + 1] == '#')
        count[i][j]++;
    }
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j] == '#')
        cout << "#";
      else
        cout << count[i][j];
    }
    cout << endl;
  }
}