alexjercan/bugnet huggingface dataset:
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define INF (1 << 30)
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  vector<int> dp(n + 1, 0);
  /*
  //	O(n^2) TLE
  //	dp[i] =
  a[i]???????????????????´???¨????????¨???????¢??????¨??????????????§??? for
  (int i = 0; i < n; i++) { dp[i] = 1; for (int j = 0; j < i;j++) { if (a[j] <
  a[i]) dp[i] = max(dp[i], dp[j] + 1);
          }
          ans = max(ans, dp[i]);
  }
  */
  //	O(nlogn)
  fill(dp.begin(), dp.end(), INF);
  for (int i = 0; i < n; i++) {
    *lower_bound(dp.begin(), dp.end(), a[i]) = a[i];
  }
  ans = distance(dp.begin(), lower_bound(dp.begin(), dp.end(), INF));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  vector<int> dp(n + 1, 0);
  for (int i = 0; i < n; i++) {
    dp[i] = 1;
    for (int j = 0; j < i; j++) {
      if (a[j] < a[i])
        dp[i] = max(dp[i], dp[j] + 1);
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a, b) (a >> b) & 1
#define BitSet(a, b) a |= (1 << b)
#define BitunSet(a, b) a &= ~(1 << b)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define Numof(array) (sizeof(array) / sizeof *(array))

struct cmp_c {
  bool operator()(int x, int y) { return x < y; }
} cmp;

int main() {
  int n, a[1000000], dp[1000001];
  scanf("%d", &n);
  fill(dp, &dp[n], INT_MAX);
  for (int i = 0; n > i; i++)
    scanf("%d", &a[i]);

  for (int i = 0; n > i; i++) {
    *lower_bound(dp, &dp[n], a[i]) = a[i];
  }
  printf("%d\\n", lower_bound(dp, &dp[n], INT_MAX) - dp);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a, b) (a >> b) & 1
#define BitSet(a, b) a |= (1 << b)
#define BitunSet(a, b) a &= ~(1 << b)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define numof(array) (sizeof(array) / sizeof *(array))

pair<long long int, int> a[100000];
vector<pair<long long int, int>> buf;
int n, dp[100001], ans;

struct stl_sort_functor_cmp {
  bool operator()(const pair<int, long long int> &a,
                  const pair<int, long long int> &b) const {
    return b.first > a.first;
  }
};

int main() {
  scanf("%d", &n);
  for (int i = 0; n > i; i++) {
    a[i].second = i;
    scanf("%lld", &a[i].first);
  }

  for (int i = 0; n > i; i++) {
    buf.insert(lower_bound(buf.begin(), buf.end(), a[i]), a[i]);
    /*buf[i] = a[i];
    sort(buf, buf +(i+1), stl_sort_functor_cmp());*/
    for (int j = 0; i > j; j++) {
      if (buf[j].first >= a[i].first)
        break;

      if (dp[buf[j].second] == ans) {
        dp[i] = ans + 1;
        break;
      }
      dp[i] = max(dp[i], dp[buf[j].second] + 1);
    }
    if (!dp[i])
      dp[i] = 1;
    ans = max(ans, dp[i]);
  }
  printf("%d\\n", ans);
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

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

int binarySearch(vector<int> A, int key) {
  int left = 0, right = A.size();
  while (left < right) {
    int mid = (left + right) / 2;
    if (A[mid] < key)
      left = mid + 1;
    else
      right = mid;
  }

  // cout<<left<<endl;
  return left;
}

void LISout(vector<int> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}

int LIS(vector<int> a) {
  vector<int> lis;

  lis.push_back(a[0]);
  for (int i = 1; i < a.size(); i++)
    if (a[i] > lis[lis.size() - 1]) {
      lis.push_back(a[i]);
      // LISout(lis);
    } else if (a[i] < lis[lis.size() - 1]) {
      // cout<<"* ";
      lis[binarySearch(lis, a[i])] = a[i];
      // LISout(lis);
    }

  return lis.size();
}

int main() {

  int n, num;
  vector<int> a, lis;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    a.push_back(num);
  }

  // cout<<endl;
  cout << LIS(a) << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int binarySearch(vector<int> A, int key, int left, int right) {
  int mid = (left + right) / 2;
  if (left == mid)
    if (A[mid] <= key)
      return binarySearch(A, key, mid, right);
    else
      return binarySearch(A, key, left, mid);
  else
    return left;
}

int LIS(vector<int> a) {
  vector<int> lis;

  lis.push_back(a[0]);
  for (int i = 1; i < a.size(); i++)
    if (a[i] > lis[lis.size() - 1]) {
      lis.push_back(a[i]);
    } else if (a[i] < lis[lis.size() - 1]) {
      lis[binarySearch(lis, a[i], 0, lis.size() - 1)] = a[i];
    }

  for (int i = 0; i < lis.size(); i++)
    cout << lis[i] << " ";
  cout << endl;

  return lis.size();
}

int main() {

  int n, num;
  vector<int> a, lis;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    a.push_back(num);
  }

  cout << LIS(a) << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int search(int beg, int end, int value, vector<int> seq) {

  int mid = -1;
  while (end - beg > 1) {
    mid = (beg + end) / 2;
    if (seq[mid] == value) {
      return mid;
    } else if (value < seq[mid]) {
      end = mid;
    } else {
      beg = mid;
    }
  }
  if (value <= seq[beg]) {
    return beg;
  } else {
    return end;
  }
}

int solve(vector<int> a) {
  int n = a.size();
  vector<int> seq;
  seq.push_back(a[0]);
  for (int i = 1; i < n; ++i) {
    if (a[i] > seq.back()) {
      seq.push_back(a[i]);
    } else {
      int pos = search(0, seq.size() - 1, a[i], seq);
      seq[pos] = a[i];
    }
  }
  return seq.size();
}

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%d", &a[i]);
  }
  int ans = solve(a);
  fprintf(stdout, "%d", ans);
  fprintf(stdout, "\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int solve(vector<int> a) {
  int n = a.size();
  vector<int> dp(n, 1);
  int ret = 1;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (a[i] > a[j]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
    ret = max(ret, dp[i]);
  }
  return ret;
}

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%d", &a[i]);
  }
  int ans = solve(a);
  fprintf(stdout, "%d", ans);
  fprintf(stdout, "\\n");
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#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 nibun(int a[], int d, int l, int h) {
  // cout<<"l="<<l<<" h="<<h<<" ";

  if (h - l <= 1 && a[h] > d)
    return l;
  else if (h - l <= 1)
    return h;
  else {
    int m = (l + h) / 2;
    if (a[m] >= d)
      nibun(a, d, l, m - 1);
    else
      nibun(a, d, m, h);
  }
}

int main() {
  int n, w;
  int a[100010];
  int dp[100010];
  cin >> n;
  for (int i = 0; i < n + 1; i++)
    dp[i] = inf;
  dp[0] = -1;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[1] = a[0];
  int length = 1;
  for (int i = 1; i < n; i++) {
    int k = nibun(dp, a[i], 0, n + 1);
    // cout<<k<<endl;
    if (dp[k] < a[i])
      dp[k + 1] = a[i];
    if (dp[length + 1] != inf)
      length++;
    // for(int b=0;b<n+1;b++) cout<<dp[b]<<" ";
    // cout<<endl;
  }

  cout << length << endl;
  return 0;
}

Error: TLE

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() {
  int n, w;
  int a[100010];
  int dp[100010];
  cin >> n;
  for (int i = 0; i < n + 1; i++)
    dp[i] = inf;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[1] = a[0];
  int length = 1;
  for (int i = 1; i < n; i++) {
    for (int j = length + 1; j >= 1; j--) {
      if (j == 1)
        dp[j] = min(dp[j], a[i]);
      else if (dp[j - 1] < a[i])
        dp[j] = min(dp[j], a[i]);
    }
    if (dp[length + 1] != inf)
      length++;
  }

  cout << length << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

int main() {

  int n;
  scanf("%d", &n);

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

  //	4
  //	2
  //	1
  //	6
  //	3
  //	5
  int L[n];
  L[0] = A[0];
  int length = 1;
  for (int i = 1; i < n; ++i) {
    if (A[i] > L[length - 1]) {
      L[length++] = A[i];
    } else {
      int left = 0;
      int right = length;
      while (left < right) {
        int mid = (left + right) / 2;
        if (L[mid] >= A[i]) {
          right = mid;
        } else {
          left = mid + 1;
        }
      }
      L[right] = A[i];
    }
  }
  cout << length << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {

  int n;
  scanf("%d", &n);

  int A[n + 1];
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &A[i]);
  }

  //	5
  //	1
  //	3
  //	2
  //	4
  int L[n + 1];
  L[0] = 0;
  for (int i = 1; i <= n; ++i) {
    L[i] = -1;
  }
  for (int i = 1; i <= n; ++i) {
    int k = 0;
    for (int j = 0; j < i; ++j) {
      if (L[j] > L[k] && A[j] < A[i]) {
        k = j;
      }
    }
    L[i] = L[k] + 1;
  }
  int maxv = 0;
  for (int i = 1; i <= n; ++i) {
    maxv = max(maxv, L[i]);
  }
  cout << maxv << endl;

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

#include <iostream>
#include <vector>

using namespace std;

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

int memo[1001][1001];
bool checked[1001][1001];
int lcs(string s, string t, int si, int ti) {
  if (si == -1 && ti == -1) {
    return 0;
  }
  if (si == -1) {
    return lcs(s, t, si, ti - 1) + 1;
  }
  if (ti == -1) {
    return lcs(s, t, si - 1, ti) + 1;
  }

  if (checked[si][ti]) {
    return memo[si][ti];
  }
  checked[si][ti] = true;

  int r1 = lcs(s, t, si - 1, ti) + 1; // sを一文字削除
  int r2 = lcs(s, t, si, ti - 1) + 1; // tを一文字削除
  int r3 =
      lcs(s, t, si - 1, ti - 1) +
      (s[si] == t[ti]
           ? 0
           : 1); // 最後の文字を交換して一致させる。もともと一致している場合は不要

  int res = min(r1, min(r2, r3));

  return memo[si][ti] = res;
}

int main() {
  ios::sync_with_stdio(false);

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

  cout << lcs(s, t, s.size(), t.size()) << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

int lcs(string s, string t, int si, int ti) {
  if (si == -1 && ti == -1) {
    return 0;
  }
  if (si == -1) {
    return lcs(s, t, si, ti - 1) + 1;
  }
  if (ti == -1) {
    return lcs(s, t, si - 1, ti) + 1;
  }

  int r1 = lcs(s, t, si - 1, ti) + 1; // sを一文字削除
  int r2 = lcs(s, t, si, ti - 1) + 1; // tを一文字削除
  int r3 =
      lcs(s, t, si - 1, ti - 1) +
      (s[si] == t[ti]
           ? 0
           : 1); // 最後の文字を交換して一致させる。もともと一致している場合は不要

  int res = min(r1, min(r2, r3));

  return res;
}

int main() {
  ios::sync_with_stdio(false);

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

  cout << lcs(s, t, s.size(), t.size()) << "\\n";

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

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

int editDistance(string a, string b) {
  int n = a.size();
  int m = b.size();
  vector<vector<int>> dp;
  dp.resize(n + 1);

  for (int i = n; i >= 0; i--) {
    dp[i].resize(m + 1);

    for (int j = m; j >= 0; j--) {
      if (i == n) {
        dp[i][j] = m - j;
      } else if (j == m) {
        dp[i][j] = n - i;
      } else {
        dp[i][j] = 1e9;
        if (a[i] == b[j])
          dp[i][j] = dp[i + 1][j + 1];
        else
          dp[i][j] = min(dp[i + 1][j + 1], min(dp[i][j + 1], dp[i + 1][j])) + 1;
      }
    }
  }
  return dp[0][0];
}

int main() {
  string a, b;
  cin >> a >> b;
  cout << editDistance(a, b) << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int editDistance(string a, string b) {
  int n = a.size();
  int m = b.size();
  vector<vector<int>> dp;
  dp.resize(n + 1);
  dp[n][m] = 0;
  for (int i = n; i >= 0; i--) {
    for (int j = m; j >= 0; j--) {
      if (i == n) {
        dp[i][j] = m - j;
      } else if (j == m) {
        dp[i][j] = n - i;
      } else {
        dp[i][j] = 1e9;
        if (a[i] == b[j])
          dp[i][j] = dp[i + 1][j + 1];
        else
          dp[i][j] = min(dp[i][j + 1], dp[i + 1][j]) + 1;
      }
    }
  }
  return dp[0][0];
}

int main() {
  string a, b;
  cin >> a >> b;
  cout << editDistance(a, b) << endl;
  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
int main() {

  int i, j;
  std::string x, y;
  int a[1001][1001];
  int knum;
  for (i = 0; i < 1001; i++) {
    for (j = 0; j < 1001; j++) {
      if (j == 0)
        a[i][j] = i;
      else if (i == 0)
        a[i][j] = j;
      else
        a[i][j] = 0;
    }
  }
  std::cin >> x;
  std::cin >> y;
  int vx, vy, vz;
  for (i = 1; i < x.size() + 1; i++) {
    for (j = 1; j < y.size() + 1; j++) {
      vx = a[i - 1][j] + 1;
      vy = a[i][j - 1] + 1;
      vz = a[i - 1][j - 1];
      if (x[i - 1] == y[j - 1]) {
        if (vx <= vy && vx <= vz) {
          a[i][j] = vx;
        } else if (vy <= vx && vy <= vz) {
          a[i][j] = vy;
        } else {
          a[i][j] = vz;
        }
      } else {
        if (vx <= vy && vx <= vz + 1) {
          a[i][j] = vx;
        } else if (vy <= vx && vy <= vz + 1) {
          a[i][j] = vy;
        } else {
          a[i][j] = vz + 1;
        }
      }
    }
    knum = a[x.size()][y.size()];
  }
  std::cout << knum << "\\n";
  return 0;
}


Error: 2

Fixed submission:

#include <iostream>
int main() { return 2; }
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>

int main() {
  std::string str1;
  std::string str2;
  std::cin >> str1;
  std::cin >> str2;
  int A[1001][1001] = {0};
  for (int i = 0; i <= str1.size(); i++) {
    A[i][0] = i;
  }
  for (int j = 0; j <= str2.size(); j++) {
    A[0][j] = j;
  }
  for (int i = 1; i <= str1.size(); i++) {
    for (int j = 1; j <= str2.size(); j++) {
      if (str1[i - 1] == str2[j - 1]) {
        A[i][j] = A[i - 1][j - 1];
      } else {
        A[i][j] = std::min(std::min(A[i - 1][j] + 1, A[i][j - 1] + 1),
                           A[i - 1][j - 1] + 1);
      }
    }
  }
  std::cout << A[str1.size()][str2.size()] << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>

int main() {
  std::string str1;
  std::string str2;
  std::cin >> str1;
  std::cin >> str2;
  int A[1000][1000] = {0};
  for (int i = 0; i <= str1.size(); i++) {
    A[i][0] = i;
  }
  for (int j = 0; j <= str2.size(); j++) {
    A[0][j] = j;
  }
  for (int i = 1; i <= str1.size(); i++) {
    for (int j = 1; j <= str2.size(); j++) {
      if (str1[i - 1] == str2[j - 1]) {
        A[i][j] = A[i - 1][j - 1];
      } else {
        A[i][j] = std::min(std::min(A[i - 1][j] + 1, A[i][j - 1] + 1),
                           A[i - 1][j - 1] + 1);
      }
    }
  }
  std::cout << A[str1.size()][str2.size()] << "\\n";
  return 0;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

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

int A[1001][1001];

int main() {
  string s1, s2;
  cin >> s1;
  cin >> s2;

  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        A[0][j] = j;
        continue;
      }
      if (j == 0) {
        A[i][0] = i;
        continue;
      } else {
        if (s1[i - 1] == s2[j - 1]) {
          A[i][j] = A[i - 1][j - 1];
        } else {
          A[i][j] = min(A[i][j - 1], A[i - 1][j]) + 1;
          A[i][j] = min(A[i][j], A[i - 1][j - 1] + 1);
        }
      }
      // cout<<A[i][j]<<" ";
    }
    // cout<<endl;
  }

  cout << A[s1.size()][s2.size()] << endl;
}


Error: 0

Fixed submission:

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

int A[1000][1000];

int main() {
  string s1, s2;
  cin >> s1;
  cin >> s2;

  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        A[0][j] = j;
        continue;
      }
      if (j == 0) {
        A[i][0] = i;
        continue;
      } else {
        if (s1[i - 1] == s2[j - 1]) {
          A[i][j] = A[i - 1][j - 1];
        } else {
          A[i][j] = min(A[i][j - 1], A[i - 1][j]) + 1;
          A[i][j] = min(A[i][j], A[i - 1][j - 1] + 1);
        }
      }
      // cout<<A[i][j]<<" ";
    }
    // cout<<endl;
  }

  cout << A[s1.size()][s2.size()] << endl;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string s1, s2;
  cin >> s1 >> s2;

  int dist[1001][1001];
  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        dist[i][j] = j;
      } else if (j == 0) {
        dist[i][j] = i;
      } else {
        int cost = (s1[i - 1] == s2[j - 1] ? 0 : 1);
        int add = dist[i - 1][j] + 1;
        int dlt = dist[i][j - 1] + 1;
        int exch = dist[i - 1][j - 1] + cost; // why * is necessary

        if (add <= dlt && add <= exch) {
          dist[i][j] = add;
        } else if (dlt <= add && dlt <= exch) {
          dist[i][j] = dlt;
        } else {
          dist[i][j] = exch;
        }
      }
    }
  }
  cout << dist[s1.size()][s2.size()] << "\\n";
}

Error: 0

Fixed submission:

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

int main() {
  string s1, s2;
  cin >> s1 >> s2;

  int dist[1000][1000];
  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        dist[i][j] = j;
      } else if (j == 0) {
        dist[i][j] = i;
      } else {
        int cost = (s1[i - 1] == s2[j - 1] ? 0 : 1);
        int add = dist[i - 1][j] + 1;
        int dlt = dist[i][j - 1] + 1;
        int exch = dist[i - 1][j - 1] + cost; // why * is necessary

        if (add <= dlt && add <= exch) {
          dist[i][j] = add;
        } else if (dlt <= add && dlt <= exch) {
          dist[i][j] = dlt;
        } else {
          dist[i][j] = exch;
        }
      }
    }
  }
  cout << dist[s1.size()][s2.size()] << "\\n";
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int L[1001][1001];
int main() {
  string a, b;
  cin >> a;
  cin >> b;

  for (int i = 0; i <= a.size(); ++i)
    L[i][0] = i;
  for (int i = 0; i <= b.size(); ++i)
    L[0][i] = i;

  for (int i = 1; i <= a.size(); ++i) {
    for (int j = 1; j <= b.size(); ++j) {
      int k = 0;
      if (a[i - 1] == b[j - 1])
        k = 0;
      else
        k = 1;
      L[i][j] = min(L[i - 1][j] + 1, min(L[i][j - 1] + 1, L[i - 1][j - 1] + k));
    }
  }
  cout << L[a.size()][b.size()] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int L[1000][1000];
int main() {
  string a, b;
  cin >> a;
  cin >> b;

  for (int i = 0; i <= a.size(); ++i)
    L[i][0] = i;
  for (int i = 0; i <= b.size(); ++i)
    L[0][i] = i;

  for (int i = 1; i <= a.size(); ++i) {
    for (int j = 1; j <= b.size(); ++j) {
      int k = 0;
      if (a[i - 1] == b[j - 1])
        k = 0;
      else
        k = 1;
      L[i][j] = min(L[i - 1][j] + 1, min(L[i][j - 1] + 1, L[i - 1][j - 1] + k));
    }
  }
  cout << L[a.size()][b.size()] << endl;
}
Problem p02318 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int dp[1001][1001]; // minimum edit distance
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s1;
  string s2;

  cin >> s1;
  cin >> s2;

  // init. dp table
  for (int i = 0; i <= s1.length(); i++) {
    dp[i][0] = i;
  }
  for (int j = 0; j <= s2.length(); j++) {
    dp[0][j] = j;
  }

  for (int i = 1; i <= s1.length(); i++) {
    for (int j = 1; j <= s2.length(); j++) {
      int RepCost = s1[i - 1] == s2[j - 1] ? 0 : 1;
      dp[i][j] = min(min(dp[i][j - 1] + 1,      // insertion
                         dp[i - 1][j] + 1),     // deletion
                     dp[i - 1][j - 1] + RepCost // replacement
      );
    }
  }

  /*
      for(int i=0; i<=s1.length(); i++){
          for(int j=0; j<=s2.length(); j++){
              cout << dp[i][j] << " ";
          }
          cout << "\\n";
      }
  */
  cout << dp[s1.length()][s2.length()] << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int dp[1000][1000]; // minimum edit distance
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s1;
  string s2;

  cin >> s1;
  cin >> s2;

  // init. dp table
  for (int i = 0; i <= s1.length(); i++) {
    dp[i][0] = i;
  }
  for (int j = 0; j <= s2.length(); j++) {
    dp[0][j] = j;
  }

  for (int i = 1; i <= s1.length(); i++) {
    for (int j = 1; j <= s2.length(); j++) {
      int RepCost = s1[i - 1] == s2[j - 1] ? 0 : 1;
      dp[i][j] = min(min(dp[i][j - 1] + 1,      // insertion
                         dp[i - 1][j] + 1),     // deletion
                     dp[i - 1][j - 1] + RepCost // replacement
      );
    }
  }

  /*
      for(int i=0; i<=s1.length(); i++){
          for(int j=0; j<=s2.length(); j++){
              cout << dp[i][j] << " ";
          }
          cout << "\\n";
      }
  */
  cout << dp[s1.length()][s2.length()] << "\\n";

  return 0;
}