alexjercan/bugnet huggingface dataset:
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

class _in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    constexpr int operator*() { return it; }
    constexpr bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr _in(int n) : i(0), n(n) {}
  explicit constexpr _in(int i, int n) : i(i, n < i), n(n) {}
  constexpr const my_iterator &begin() { return i; }
  constexpr const my_iterator &end() { return n; }
};

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

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int N = s1.size(), M = s2.size();
  vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
  for (int i : _in(N + 1))
    dp[i][0] = i;
  for (int j : _in(M + 1))
    dp[0][j] = j;
  for (int i : _in(N))
    for (int j : _in(M)) {
      dp[i + 1][j + 1] = dp[i][j];
      if (s1[i] != s2[j])
        ++dp[i + 1][j + 1];
      int temp = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1);
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], temp);
    }
  cout << dp[N][M] << endl;
}

Error: 0

Fixed submission:

class _in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    constexpr int operator*() { return it; }
    constexpr bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr _in(int n) : i(0), n(n) {}
  explicit constexpr _in(int i, int n) : i(i, n < i), n(n) {}
  constexpr const my_iterator &begin() { return i; }
  constexpr const my_iterator &end() { return n; }
};

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

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int N = s1.size(), M = s2.size();
  vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
  for (int i : _in(N + 1))
    dp[i][0] = i;
  for (int j : _in(N + 1))
    dp[0][j] = j;
  for (int i : _in(N))
    for (int j : _in(M)) {
      dp[i + 1][j + 1] = dp[i][j];
      if (s1[i] != s2[j])
        ++dp[i + 1][j + 1];
      int temp = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1);
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], temp);
    }
  cout << dp[N][M] << endl;
}
Problem p02318 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  string s1;
  string s2;
  cin >> s1;
  cin >> s2;
  int a = s1.size();
  int b = s2.size();
  vvi dp(a + 1, vi(b + 1, 0));
  for (int i = 0; i <= a; i++)
    dp[i][0] = i;
  for (int i = 0; i <= b; i++)
    dp[0][i] = i;
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      dp[i][j] =
          min(dp[i - 1][j] + 1, min(dp[i - 1][j - 1] + 1, dp[i][j - 1] + 1));
      if (i >= 1 && j >= 1 && s1[i - 1] == s2[j - 1]) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
      }
    }
  }
  /*for (int i = 0; i <= a; i++) {
          for (int j = 0; j <= b; j++) {
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/
  cout << dp[a][b] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  while (true) {
    string s1;
    string s2;
    cin >> s1;
    cin >> s2;
    int a = s1.size();
    int b = s2.size();
    vvi dp(a + 1, vi(b + 1, 0));
    for (int i = 1; i <= a; i++) {
      for (int j = 1; j <= b; j++) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (i >= 1 && j >= 1 && s1[i - 1] == s2[j - 1]) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
        }
      }
    }
    cout << max(a, b) - dp[a][b] << endl;
  }
  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>

#define rep(index, max) for (int index = 0; index < max; index++)

using namespace std;

int main() {
  string s1, s2;
  int l1, l2;
  cin >> s1 >> s2;
  l1 = s1.length();
  l2 = s2.length();
  int dp[1010][1010];

  rep(i, l1 + 1) dp[i][0] = i;
  rep(j, l2 + 1) dp[0][j] = j;

  rep(i, l1) {
    rep(j, l2) {
      if (s1[i] == s2[j]) {
        dp[i + 1][j + 1] = dp[i][j];
      } else {
        dp[i + 1][j + 1] = min(min(dp[i + 1][j], dp[i][j + 1]), dp[i][j]) + 1;
      }
    }
  }

  cout << dp[l1][l2] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int l1, l2;
  l1 = s1.size();
  l2 = s2.size();

  int dp[1020][1020];
  for (int i = 0; i < l1; i++) {
    if (s1[i] == s2[0])
      dp[i][0] = 1;
    else
      dp[i][0] = 0;
  }
  for (int j = 0; j < l2; j++) {
    if (s1[0] == s2[j])
      dp[0][j] = 1;
    else
      dp[0][j] = 0;
  }
  for (int i = 1; i < l1; l1++) {
    for (int j = 1; j < l2; j++) {
      if (s1[i] == s2[j]) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + 1;
      } else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  cout << abs(l1 - l2) + (min(l1, l2) - dp[l1 - 1][l2 - 1]) << endl;

  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int result[1001][1001] = {};
  for (int i = 0; i <= s1.length(); i++) {
    result[i][0] = i;
  }
  for (int i = 0; i <= s2.length(); i++) {
    result[0][i] = i;
  }
  for (int i = 1; i <= s1.length(); i++) {
    for (int j = 1; j <= s2.length(); j++) {
      result[i][j] = result[i - 1][j - 1];
      if (s1[i - 1] != s2[j - 1])
        result[i][j]++;
      result[i][j] =
          min(result[i][j], min(result[i - 1][j] + 1, result[i][j - 1] + 1));
    }
  }
  // for(int i = 0; i <= s1.length(); i++){
  //     for(int j = 0; j <= s2.length(); j++){
  //         cout << result[i][j] << " ";
  //     }
  //     cout << endl;
  // }
  cout << result[s1.length()][s2.length()] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

map<string, int> m;
string s1, s2;

int solve(string s, int index) {
  if (s == s2)
    return 0;
  if (m.find(s) != m.end())
    return m[s];
  m[s] = mod;
  while (index < s.length() && index < s2.length() && s[index] == s2[index]) {
    index++;
  }
  if (index >= s.length()) {
    m[s] = s2.length() - s.length();
  } else if (index >= s2.length()) {
    m[s] = s.length() - s2.length();
  } else {
    string next = s.substr(0, index) + s.substr(index + 1);
    m[s] = min(m[s], solve(next, index) + 1);
    next = s;
    next[index] = s2[index];
    // cout << next << endl;
    m[s] = min(m[s], solve(next, index + 1) + 1);
    next = s.substr(0, index) + s2[index] + s.substr(index);
    m[s] = min(m[s], solve(next, index + 1) + 1);
  }
  // cout << s << " " << index << " " << m[s] << endl;
  return m[s];
}

int main() {
  cin >> s1 >> s2;
  m[s2] = mod;
  cout << solve(s1, 0) << endl;
  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+18 + 10;

std::string str1, str2;
int s1, s2;
int dp[1005][1005];

int main() {
  std::cin >> str1 >> str2;
  s1 = str1.size();
  s2 = str2.size();

  rep(i, s1 + 1) dp[i][0] = i;
  rep(i, s2 + 1) dp[0][i] = i;

  REP(i, 1, s1 + 1) {
    REP(j, 1, s2 + 1) {
      int cost = 1;
      if (str1[i - 1] == str2[j - 1])
        cost = 0;
      dp[i][j] = std::min(dp[i - 1][j] + 1,
                          std::min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost));
    }
  }

  std::cout << dp[s1][s2] << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

std::string s1, s2;
int ans = 0;

int main() {
  std::cin >> s1 >> s2;
  s1 += '?', s2 += '?';
  if (s1.size() > s2.size())
    std::swap(s1, s2);

  int i1 = 0, i2 = 0;
  for (; i1 < s1.size(); ++i1, ++i2) {
    if (s1[i1] != s2[i2]) {
      if (s2[i2 + 1] == '?')
        ++ans;
      else
        ++ans, --i1;
    }
  }

  std::cout << ans << std::endl;

  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;
#define repeat(i, n) for (int i = 0; i < (n); i++)
const int M = 10000;
int memo[M + 1][M + 1];

int main() {

  string s, t;
  cin >> s >> t;

  repeat(i, M + 1) memo[i][0] = memo[0][i] = i;
  repeat(i, s.size() + 1) {
    repeat(j, t.size() + 1) {
      if (i == 0 or j == 0)
        continue;
      memo[i][j] = memo[i - 1][j] + 1;
      memo[i][j] = min(memo[i][j], memo[i][j - 1] + 1);
      memo[i][j] = min(memo[i][j], memo[i - 1][j - 1] + (s[i - 1] != t[j - 1]));
    }
  }

  cout << memo[s.size()][t.size()] << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;
#define repeat(i, n) for (int i = 0; i < (n); i++)
const int M = 10000;
int memo[M + 1][M + 1];

int main() {

  string s, t;
  cin >> s >> t;

  repeat(i, M + 1) memo[i][0] = memo[0][i] = i;
  repeat(i, s.size() + 1) {
    repeat(j, t.size() + 1) {
      if (i + j <= 1)
        continue;
      memo[i][j] = memo[i - 1][j] + 1;
      memo[i][j] = min(memo[i][j], memo[i][j - 1] + 1);
      memo[i][j] = min(memo[i][j], memo[i - 1][j - 1] + (s[i - 1] != t[i - 1]));
    }
  }

  cout << memo[s.size()][t.size()] << endl;

  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

/*
 * Contents   : AOJ DPL 1 E
 * Author     : KitauraHiromi
 * LastUpdate : 20180614
 * Since      : 20180614
 */
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

string S, T;
int INF = 1e9 + 1;
int dp[1010][1010];
int main(void) {
  cin >> S >> T;
  Fill(dp, INF);
  dp[0][0] = 0;
  for (int i = -1; i <= (int)S.size(); ++i) {
    for (int j = -1; j <= (int)T.size(); ++j) {
      if (i == -1 && j == -1)
        continue;
      // 変更
      if (i >= 0 && j >= 0) {
        if (S[i] == T[j])
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
        else
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1);
      }
      // 削除
      if (i >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j + 1] + 1);
      // 追加
      if (j >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i + 1][j] + 1);
    }
  }
  cout << dp[S.size()][T.size()] << endl;
  return 0;
}


Error: 0

Fixed submission:

/*
 * Contents   : AOJ DPL 1 E
 * Author     : KitauraHiromi
 * LastUpdate : 20180614
 * Since      : 20180614
 */
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

string S, T;
int INF = 1e9 + 1;
int dp[1001][1001];
int main(void) {
  cin >> S >> T;
  Fill(dp, INF);
  dp[0][0] = 0;
  for (int i = -1; i <= (int)S.size(); ++i) {
    for (int j = -1; j <= (int)T.size(); ++j) {
      if (i == -1 && j == -1)
        continue;
      // 変更
      if (i >= 0 && j >= 0) {
        if (S[i] == T[j])
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
        else
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1);
      }
      // 削除
      if (i >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j + 1] + 1);
      // 追加
      if (j >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i + 1][j] + 1);
    }
  }
  cout << dp[S.size()][T.size()] << endl;
  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define IOI 101
#define IOIOIOIOI 101010101
int dp[10 * IOI][10 * IOI];
string S, T;
int main() {
  for (int i = 0; i < 10 * IOI; i++) {
    for (int j = 0; j < 10 * IOI; j++) {
      dp[i][j] = IOIOIOIOI;
    }
  }
  cin >> S;
  cin >> T;
  for (int i = 0; i <= S.size(); i++)
    dp[i][0] = i;
  for (int i = 0; i <= T.size(); i++)
    dp[0][i] = i;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < T.size(); j++) {
      dp[i + 1][j + 1] = min(dp[i][j], min(dp[i + 1][j], dp[i][j + 1])) + 1;
      if (S[i] == T[j])
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
    }
  }
  cout << dp[S.size()][T.size()] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define IOI 101
#define IOIOIOIOI 101010101
int dp[10 * IOI][10 * IOI];
string S, T;
int main() {
  for (int i = 0; i <= 10 * IOI; i++) {
    for (int j = 0; j <= 10 * IOI; j++) {
      dp[i][j] = IOIOIOIOI;
    }
  }
  cin >> S >> T;
  for (int i = 0; i <= S.size(); i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= T.size(); i++) {
    dp[0][i] = 0;
  }
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < T.size(); j++) {
      if (S[i] == T[j]) {
        dp[i + 1][j + 1] = dp[i][j];
      } else {
        dp[i + 1][j + 1] = min(dp[i][j + 1], min(dp[i + 1][j], dp[i][j + 1]));
      }
    }
  }
  cout << dp[S.size()][T.size()] << endl;
  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define inf 1999999999
#define pa pair<int, int>
using namespace std;

int main() {
  // cout<<"1"<<endl;
  string s, t;
  cin >> s >> t;
  int ls = s.length(), lt = t.length();
  // cout<<ls<<endl;
  // cout<<s<<endl;

  int dp[1010][1010];
  for (int i = 0; i <= max(ls, lt); i++) {
    dp[i][0] = i;
    dp[0][i] = i;
  }

  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1]);
      else
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 1);
      // cout<<dp[i][j]<<endl;
    }
  }
  cout << dp[ls][lt] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define inf 1999999999
#define pa pair<int, int>
using namespace std;

int main() {
  // cout<<"1"<<endl;
  string s, t;
  cin >> s >> t;
  int ls = s.length(), lt = t.length();
  // cout<<ls<<endl;
  // cout<<s<<endl;

  int dp[101][101];
  for (int i = 0; i <= max(ls, lt); i++) {
    dp[i][0] = i;
    dp[0][i] = i;
  }

  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1]);
      else
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 1);
      // cout<<dp[i][j]<<endl;
    }
  }
  cout << dp[ls][lt] << endl;

  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string A, B;
  cin >> A >> B;
  int a = A.length(), b = B.length();
  vector<vector<int>> Dist(a + 1, vector<int>(b + 1, 0));
  for (int i = 0; i <= a; i++)
    Dist[i][0] = i;
  for (int i = 0; i <= b; i++)
    Dist[0][i] = i;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      Dist[i + 1][j + 1] = min({Dist[i + 1][j] + 1, Dist[i][j + 1] + 1,
                                Dist[i][j] + (A[i] != B[j])});
    }
  }
  cout << Dist[a][b] << "\\n";
}


Error: 0

Fixed submission:

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

int main() {
  string A, B;
  cin >> A >> B;
  int a = A.length(), b = B.length();
  vector<vector<int>> Dist(a + 1, vector<int>(b, 0));
  for (int i = 0; i <= a; i++)
    Dist[i][0] = i;
  for (int i = 0; i <= b; i++)
    Dist[0][i] = i;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      Dist[i + 1][j + 1] = min({Dist[i + 1][j] + 1, Dist[i][j + 1] + 1,
                                Dist[i][j] + (A[i] != B[j])});
    }
  }
  cout << Dist[a][b] << "\\n";
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

const int SIZE = 1010;
int dp[SIZE][SIZE];

int editDistance(string s, string t) {
  rep(i, 0, SIZE) rep(j, 0, SIZE) dp[i][j] = 0;
  rep(i, 0, SIZE) dp[i][0] = i;
  rep(i, 0, SIZE) dp[0][i] = i;
  rep(i, 0, s.size()) {
    rep(j, 0, t.size()) {
      int cost = (s[i] == t[j]) ? 0 : 1;
      dp[i + 1][j + 1] =
          min(dp[i][j] + cost, min(dp[i + 1][j] + 1, dp[i][j + 1] + 1));
    }
  }
  return dp[s.size()][t.size()];
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  cout << editDistance(s, t) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int editDistance(string s, string t) {
  int dp[51][51];
  rep(i, 0, 51) rep(j, 0, 51) dp[i][j] = 0;
  rep(i, 0, 51) dp[i][0] = i;
  rep(i, 0, 51) dp[0][i] = i;
  rep(i, 0, s.size()) {
    rep(j, 0, t.size()) {
      int cost = (s[i] == t[j]) ? 0 : 1;
      dp[i + 1][j + 1] =
          min(dp[i][j] + cost, min(dp[i + 1][j] + 1, dp[i][j + 1] + 1));
    }
  }
  return dp[s.size()][t.size()];
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  cout << editDistance(s, t) << endl;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int min(int a, int b, int c) {
  int t = a < b ? a : b;
  return t < c ? t : c;
}
int main() {
  string s1, s2;
  int d[1010][1010];
  cin >> s1 >> s2;
  int n = s1.length();
  int m = s2.length();
  if (n == 0) {
    return n;
  }
  if (m == 0) {
    return m;
  }
  for (int i = 0; i <= n; i++) {
    d[i][0] = i;
  }
  for (int j = 0; j <= m; j++) {
    d[0][j] = j;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        d[i][j] = d[i - 1][j - 1];
      } else {
        d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + 1);
      }
    }
  }
  cout << d[n][m] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int min(int a, int b, int c) {
  int t = a < b ? a : b;
  return t < c ? t : c;
}
int main() {
  string s1, s2;
  int d[1000][1000];
  cin >> s1 >> s2;
  int n = s1.length();
  int m = s2.length();
  if (n == 0) {
    return n;
  }
  if (m == 0) {
    return m;
  }
  for (int i = 0; i <= n; i++) {
    d[i][0] = i;
  }
  for (int j = 0; j <= m; j++) {
    d[0][j] = j;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        d[i][j] = d[i - 1][j - 1];
      } else {
        d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + 1);
      }
    }
  }
  cout << d[n][m] << endl;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#define MAXW 1000000500
#define MAXN 105
#define MAXV 10001
// question why I can not get the right size, try in java
// the sizze of array is the same as W
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int main() {

  // i means:the first ith ele and j means: the capacity
  int N = 0, W = 0;
  scanf("%d %d", &N, &W);
  int dp[10001]; // store the value
  int ele[N + 1][2];
  for (int i = 1; i <= N; i++) {
    scanf("%d %d", &ele[i][0], &ele[i][1]); // v,w
  }
  // initialize the weight MAXW
  for (int i = 0; i < MAXV; i++) {
    dp[i] = MAXW;
  }
  dp[0] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 10000; j >= ele[i][0]; j--) {
      dp[j] = min(dp[j], dp[j - ele[i][0]] + ele[i][1]);
    }
  }
  int value = 0;
  for (int i = 0; i < MAXV; i++) {
    if (dp[i] <= W)
      value = max(value, i);
  }
  printf("%d\\n", value);
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>

#define MAXW 10050
#define MAXN 105
int dp[MAXN][MAXW];
int max(int a, int b) { return a > b ? a : b; }

int main() {
  // i means:the first ith ele and j means: the capacity
  int N = 0, W = 0;
  scanf("%d %d", &N, &W);

  int ele[N + 1][2];
  for (int i = 1; i <= N; i++) {
    scanf("%d %d", &ele[i][0], &ele[i][1]); // v,w
  }
  // boundary case for dp
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      dp[i][j] = 0;
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      dp[i][j] = dp[i - 1][j]; // not using the ith ele
      if (j - ele[i][1] >= 0) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ele[i][1]] + ele[i][0]);
      }
    }
  }
  /*for(int i = 0; i<=N; i++){
          for(int j = 0; j<=W; j++){
                  printf("%d ",dp[i][j]);
          }
          printf("\\n");
  }*/
  printf("%d\\n", dp[N][W]);
  return 0;
}