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

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

string t[15] = {"Rock",  "Fire",   "Scissors", "Snake",     "Human",
                "Tree",  "Wolf",   "Sponge",   "Paper",     "Air",
                "Water", "Dragon", "Devil",    "Lightning", "Gun"};

int search(string s) {
  for (int i = 0; i < 15; i++)
    if (t[i] == s)
      return i;
  assert(0);
  return 0;
}

int check(int a, int b) {
  if (a == b)
    return 0;
  for (int i = 1; i <= 7; i++)
    if ((a + i) % 15 == b)
      return 1;
  return -1;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<string> t(n);
    vector<int> u(n);
    for (int i = 0; i < n; i++) {
      cin >> t[i];
      u[i] = search(t[i]);
    }

    string ans = "Draw";
    for (int i = 0; i < n; i++) {
      int A = 0, B = 0;
      for (int j = 0; j < n; j++) {
        int k = check(u[i], u[j]);
        if (k == 1)
          A++;
        if (k == -1)
          B++;
      }
      if (A > 0 && B == 0)
        ans = t[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -6
412405a4-085c-4f63-ae28-781c3e4874a6.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01156/C++/s797365619.cpp:30: int check(int, int): Assertion `0' failed.


Fixed submission:

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

string t[15] = {"Rock",  "Fire",   "Scissors", "Snake",     "Human",
                "Tree",  "Wolf",   "Sponge",   "Paper",     "Air",
                "Water", "Dragon", "Devil",    "Lightning", "Gun"};

int search(string s) {
  for (int i = 0; i < 15; i++)
    if (t[i] == s)
      return i;
  return 0;
}

int check(int a, int b) {
  if (a == b)
    return 0;
  for (int i = 1; i <= 7; i++)
    if ((a + i) % 15 == b)
      return 1;
  assert(0);
  return -1;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<string> t(n);
    vector<int> u(n);
    for (int i = 0; i < n; i++) {
      cin >> t[i];
      u[i] = search(t[i]);
    }

    string ans = "Draw";
    for (int i = 0; i < n; i++) {
      int A = 0, B = 0;
      for (int j = 0; j < n; j++) {
        int k = check(u[i], u[j]);
        if (k == 1)
          A++;
        if (k == -1)
          B++;
      }
      if (A > 0 && B == 0)
        ans = t[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01156 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int main() {
  map<string, map<string, bool>> winmp;
  string hoge[] = {"Rock",  "Fire",   "Scissors", "Snake",     "Human",
                   "Tree",  "Wolf",   "Sponge",   "Paper",     "Air",
                   "Water", "Dragon", "Devil",    "Lightning", "Gun"};

  REP(i, 15) {
    REP(j, 7) { winmp[hoge[i]][hoge[(i + j + 1) % 15]] = 1; }
  }
  int n;
  while (cin >> n, n) {
    set<string> ss;
    REP(i, n) {
      string s;
      cin >> s;
      ss.insert(s);
    }
    set<string> se;
    map<string, bool> win;
    for (set<string>::iterator it = ss.begin(); it != ss.end(); ++it)
      win[*it] = 1;

    map<string, bool> ww;
    for (set<string>::iterator it = ss.begin(); it != ss.end(); ++it) {
      for (set<string>::iterator jt = ss.begin(); jt != ss.end(); ++jt) {

        if (winmp[*it][*jt]) {
          win[*jt] = 0;
          ww[*it] = 1;
        }
      }
    }
    for (set<string>::iterator it = ss.begin(); it != ss.end(); ++it) {
      if (win[*it] && ww[*it])
        se.insert(*it);
    }
    if (se.size() == 1) {
      cout << *se.begin() << endl;
    } else {
      cout << "Draw" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int main() {
  map<string, map<string, bool>> winmp;
  string hoge[] = {"Rock",  "Fire",   "Scissors", "Snake",     "Human",
                   "Tree",  "Wolf",   "Sponge",   "Paper",     "Air",
                   "Water", "Dragon", "Devil",    "Lightning", "Gun"};

  REP(i, 15) {
    REP(j, 7) { winmp[hoge[i]][hoge[(i + j + 1) % 15]] = 1; }
  }
  int n;
  while (cin >> n, n) {
    string s[n];
    REP(i, n) { cin >> s[i]; }
    set<string> se;
    bool win[n];
    REP(i, n) win[i] = 1;
    vector<bool> ww(n);
    REP(i, n) {
      bool f = 1;
      REP(j, n) {
        if (i == j)
          continue;

        if (winmp[s[i]][s[j]]) {
          win[j] = 0;
          ww[i] = 1;
        }
      }
    }
    REP(i, n) {
      if (win[i] && ww[i])
        se.insert(s[i]);
    }
    if (se.size() == 1) {
      cout << *se.begin() << endl;
    } else {
      cout << "Draw" << endl;
    }
  }
}
Problem p01156 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
  char *S[] = {"Rock",  "Gun",   "Lightning", "Devil",    "Dragon",
               "Water", "Air",   "Paper",     "Sponge",   "Wolf",
               "Tree",  "Human", "Snake",     "Scissors", "Fire"},
       s[64];
  map<string, int> M;
  int i, j, n, x, y;
  for (i = 0; i < 15; ++i)
    M[S[i]] = i;
  while (scanf("%d", &n), n) {
    vector<int> h;
    int w[15] = {0}, l[15] = {0};
    while (n--)
      scanf("%s", &s), h.push_back(M[s]);
    sort(h.begin(), h.end());
    h.erase(unique(h.begin(), h.end()), h.end());
    for (i = 0; i < h.size(); ++i)
      for (j = 0; j < h.size(); ++j) {
        x = h[i], y = h[j];
        if (x > y)
          y += 15;
        if (y - x) {
          if (y - x < 8)
            l[i]++;
          else
            w[i]++;
        }
      }
    for (x = i = 0; i < h.size(); ++i)
      if (w[i] && !l[i])
        printf("%s\\n", S[h[i]]), ++x;
    if (!x)
      puts("Draw");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <map>
#include <stdio.h>
#include <string>
int main() {
  char *S[] = {"Rock",  "Gun",   "Lightning", "Devil",    "Dragon",
               "Water", "Air",   "Paper",     "Sponge",   "Wolf",
               "Tree",  "Human", "Snake",     "Scissors", "Fire"},
       s[64];
  std::map<std::string, int> M;
  int i, j, n, x, y;
  for (i = 0; i < 15; ++i)
    M[S[i]] = i;
  while (scanf("%d", &n), n) {
    int h[1000], w[15] = {0}, l[15] = {0};
    for (i = 0; i < n; ++i)
      scanf("%s", &s), h[i] = M[s];
    for (i = 0; i < n; ++i)
      for (j = 0; j < n; ++j) {
        x = h[i], y = h[j];
        if (x > y)
          y += 15;
        if (y - x) {
          if (y - x < 8)
            l[i]++;
          else
            w[i]++;
        }
      }
    for (x = i = 0; i < 15; ++i)
      if (w[i] && !l[i])
        printf("%s\\n", S[h[i]]), ++x;
    if (!x)
      puts("Draw");
  }
  return 0;
}
Problem p01158 (C++) — buggy submission, verdict: Time Limit Exceeded

// 55
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int n;
string na[1000], su[1000];
int d[1000], dd[1000];
map<string, int> m;
vector<int> ds[1000];
bool p[1000];
int cut[1000];

int dfs(int x) {
  if (p[x])
    return 0;
  int s = p[m[su[x]]] ? d[x] - dd[x] : 0;
  p[x] = true;
  for (int i = 0; i < ds[x].size(); i++) {
    s += dfs(ds[x][i]);
  }
  return s;
}

bool cmp(int a, int b) { return cut[a] > cut[b]; }

int main() {
  while (cin >> n, n) {
    m.clear();
    for (int i = 0; i < n; i++) {
      cin >> na[i] >> d[i] >> su[i] >> dd[i];
      m[na[i]] = i;
    }
    for (int i = 0; i < n; i++) {
      ds[i].clear();
    }
    for (int i = 0; i < n; i++) {
      ds[m[su[i]]].push_back(m[na[i]]);
    }
    for (int i = 0; i < 1000; i++) {
      fill(p, p + n, false);
      cut[i] = dfs(i);
    }
    deque<int> ef(n);
    for (int i = 0; i < n; i++) {
      ef[i] = i;
    }
    sort(ef.begin(), ef.end(), cmp);
    bool done[1000] = {};
    int a = 0;
    for (int i = 0; i < n; i++) {
      for (int nt = 0; nt < n; nt++) {
        if (!done[nt]) {
          if (done[m[su[nt]]]) {
            a += dd[nt];
            done[nt] = true;
            goto next;
          }
        }
      }
      while (done[ef[0]]) {
        ef.pop_front();
      }
      a += d[ef[0]];
      done[ef[0]] = true;
    next:;
    }
    cout << a << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 55
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    string na[1000], su[1000];
    int d[1000], dd[1000];
    for (int i = 0; i < n; i++) {
      cin >> na[i] >> d[i] >> su[i] >> dd[i];
    }
    bool done[1000] = {};
    int a = 0;
    for (int i = 0; i < n; i++) {
      int x, nd = 1 << 30;
      for (int nt = 0; nt < n; nt++) {
        if (!done[nt]) {
          if (na[nt] == su[nt] || done[find(na, na + n, su[nt]) - na]) {
            a += (na[nt] == su[nt]) ? d[nt] : dd[nt];
            done[nt] = true;
            goto next;
          }
          if (d[nt] - dd[nt] < nd) {
            nd = d[nt] - dd[nt];
            x = nt;
          }
        }
      }
      a += d[x];
      done[x] = true;
    next:;
    }
    cout << a << endl;
  }
  return 0;
}
Problem p01159 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int N;
  double R;
  while (cin >> N >> R && N > 0) {
    vector<double> xs(N), ys(N);
    REP(i, N) cin >> xs[i] >> ys[i];

    vector<double> p;
    REP(i, xs.size()) {
      double x = xs[i];
      if (xs.front() <= x + R && x + R <= xs.back()) {
        p.push_back(x);
      }
    }
    REP(i, xs.size()) {
      double x = xs[i] - R;
      if (xs.front() <= x && x <= xs.back()) {
        p.push_back(x);
      }
    }
    sort(p.begin(), p.end());
    p.erase(unique(p.begin(), p.end()), p.end());
    double ans = 0.0;
    for (int i = 0; i + 1 < p.size(); i++) {
      double lx = p[i];
      double rx = p[i + 1];
      double cx = (lx + rx) / 2;

      int k = upper_bound(xs.begin(), xs.end(), cx) - xs.begin() - 1;
      // assert(xs[k] <= lx && rx <= xs[k+1]);
      assert(k >= 0);
      double A = (ys[k + 1] - ys[k]) / (xs[k + 1] - xs[k]);
      double B = ys[k] - A * xs[k];

      double lxr = p[i] + R;
      double rxr = p[i + 1] + R;
      double cxr = (lxr + rxr) / 2;
      int kr = upper_bound(xs.begin(), xs.end(), cxr) - xs.begin() - 1;
      // assert(xs[kr] <= lxr && rxr <= xs[kr+1]);
      assert(kr >= 0);
      double C = (ys[kr + 1] - ys[kr]) / (xs[kr + 1] - xs[kr]);
      double D = ys[kr] - C * xs[kr] + C * R;

      double E = A * C;
      double F = A * D + B * C;
      double G = B * D;
      // E * x^3/3 + F * x^2/2 + G*x
      auto f = [&](double x) -> double {
        return E * x * x * x / 3 + F * x * x / 2 + G * x;
      };
      ans += f(p[i + 1]) - f(p[i]);
    }
    printf("%.12f\\n", ans);
  }
  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int N, R;
  while (cin >> N >> R) {
    vector<int> xs(N), ys(N);
    REP(i, N) cin >> xs[i] >> ys[i];
    vector<int> p = xs;
    REP(i, xs.size()) {
      int x = xs[i] - R;
      if (x >= xs.front()) {
        p.push_back(x);
      }
    }
    sort(p.begin(), p.end());
    double ans = 0.0;
    for (int i = 0; i + 1 < p.size(); i++) {
      int lx = p[i];
      int rx = p[i + 1];
      int cx = (lx + rx) / 2;

      int k = upper_bound(xs.begin(), xs.end(), cx) - xs.begin() - 1;
      assert(k >= 0);
      double A = (ys[k + 1] - ys[k]) / (xs[k + 1] - xs[k]);
      double B = ys[k] - A * xs[k];

      int lxr = p[i] + R;
      int rxr = p[i + 1] + R;
      int cxr = (lxr + rxr) / 2;
      int kr = upper_bound(xs.begin(), xs.end(), cxr) - xs.begin() - 1;
      assert(kr >= 0);
      double C = (ys[kr + 1] - ys[kr]) / (xs[kr + 1] - xs[kr]);
      double D = ys[kr] - C * xs[kr];

      double E = A * C;
      double F = A * D + B * C;
      double G = B * D;
      // E * x^3/3 + F * x^2/2 + G*x
      auto f = [&](double x) -> double {
        return E * x * x * x / 3 + F * x * x / 2 + G * x;
      };
      ans += f(p[i + 1]) - f(p[i]);
    }
    printf("%.12f\\n", ans);
  }
  return 0;
}
Problem p01160 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

#define MAX 2001

int dp[MAX][MAX];
int arr[MAX][MAX];

void init(int N) {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = 0;
      arr[i][j] = MAX;
    }
  }
}

int main() {
  string s;

  while (cin >> s) {
    string t = s;
    reverse(t.begin(), t.end());

    int len = s.size();
    init(len + 1);
    for (int i = 0; i < len; i++) {
      for (int j = 0; j < len; j++) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          arr[i + 1][j + 1] = 0;
        } else {
          if (dp[i][j + 1] < dp[i + 1][j]) {
            dp[i + 1][j + 1] = dp[i + 1][j];
            arr[i + 1][j + 1] = -1;
          } else {
            dp[i + 1][j + 1] = dp[i][j + 1];
            arr[i + 1][j + 1] = 1;
          }
        }
      }
    }
    string str;
    for (int i = len, j = len; i > 0 && j > 0;) {
      if (!arr[i][j]) {
        str += s[i - 1];
        i--, j--;
      } else if (arr[i][j] == 1) {
        i--;
      } else {
        j--;
      }
    }
    cout << str << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define MAX 2001

int dp[MAX][MAX];
string str[MAX][MAX];

void init(int N) {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = 0;
      str[i][j].clear();
    }
  }
}

int main() {
  string s;

  while (cin >> s) {
    string t = s;
    reverse(t.begin(), t.end());

    int len = s.size();
    init(len + 1);
    for (int i = 0; i < len; i++) {
      for (int j = 0; j < len; j++) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          str[i + 1][j + 1] = str[i][j] + s[i];
        } else {
          if (dp[i][j + 1] < dp[i + 1][j]) {
            dp[i + 1][j + 1] = dp[i + 1][j];
            str[i + 1][j + 1] = str[i + 1][j];
          } else {
            dp[i + 1][j + 1] = dp[i][j + 1];
            str[i + 1][j + 1] = str[i][j + 1];
          }
        }
      }
    }
    cout << str[len][len] << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define N 2050
string ss[2][N + 1];
int c[N + 1][N + 1];
string lcs(string X, string Y) {
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  ss[0][0] = "";
  memset(c, 0, sizeof(c));
  for (int i = 0; i < N + 1; i++)
    ss[0][i] = ss[1][i] = "";
  for (int i = 0; i <= m; i++)
    c[i][0] = 0;
  for (int j = 1; j <= n; j++)
    c[0][j] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j])
        c[i][j] = c[i - 1][j - 1] + 1,
        ss[i % 2][j] = ss[(i - 1) % 2][j - 1] + X[i];
      else {
        if (c[i - 1][j] >= c[i][j - 1])
          c[i][j] = c[i - 1][j], ss[i % 2][j] = ss[(i - 1) % 2][j];
        else
          c[i][j] = c[i][j - 1], ss[i % 2][j] = ss[i % 2][j - 1];
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  string ret = ss[m % 2][n], ret2 = ret;
  reverse(ret2.begin(), ret2.end());
  // assert(ret==ret2);
  assert((int)ret.size() == maxl);
  return ss[m % 2][n];
}
int main() {
  string s;
  while (cin >> s) {
    string t = s;
    reverse(t.begin(), t.end());
    cout << lcs(s, t) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N 2050
string ss[2][N + 1];
int c[N + 1][N + 1];
string lcs(string X, string Y) {
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  ss[0][0] = "";
  memset(c, 0, sizeof(c));
  for (int i = 0; i <= m; i++)
    c[i][0] = 0;
  for (int j = 1; j <= n; j++)
    c[0][j] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j])
        c[i][j] = c[i - 1][j - 1] + 1,
        ss[i % 2][j] = ss[(i - 1) % 2][j - 1] + X[i];
      else {
        if (c[i - 1][j] >= c[i][j - 1])
          c[i][j] = c[i - 1][j], ss[i % 2][j] = ss[(i - 1) % 2][j];
        else
          c[i][j] = c[i][j - 1], ss[i % 2][j] = ss[i % 2][j - 1];
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  string ret = ss[m % 2][n], ret2 = ret;
  reverse(ret2.begin(), ret2.end());
  // assert(ret==ret2);
  assert((int)ret.size() == maxl);
  return ss[m % 2][n];
}
int main() {
  string s;
  while (cin >> s) {
    string t = s;
    reverse(t.begin(), t.end());
    cout << lcs(s, t) << endl;
  }
  return 0;
}
Problem p01160 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for (int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for (auto &a : b)

typedef pair<int, int> P;

int dp[2010][2010];
int tr[2010][2010];

signed main() {
  string s;
  while (cin >> s) {
    string t = s;
    reverse(all(s));
    memset(dp, 0, sizeof(dp));
    memset(tr, 0, sizeof(dp));
    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] + 1, tr[i + 1][j + 1] = 1;
        else {
          if (dp[i + 1][j] > dp[i][j + 1])
            dp[i + 1][j + 1] = dp[i + 1][j], tr[i + 1][j + 1] = 2;
          else
            dp[i + 1][j + 1] = dp[i][j + 1], tr[i + 1][j + 1] = 3;
        }
      }
    }
    string x;
    for (int i = s.size(), j = t.size(); i > 0 && j > 0;) {
      switch (tr[i][j]) {
      case 1:
        x += s[i - 1], i--, j--;
        break;
      case 2:
        j--;
        break;
      case 3:
        i--;
        break;
      }
    }
    cout << x << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for (int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for (auto &a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;

int dp[2010][2010];
int tr[2010][2010];

signed main() {
  string s;
  while (cin >> s) {
    string t = s;
    reverse(all(s));
    memset(dp, 0, sizeof(dp));
    memset(tr, 0, sizeof(dp));
    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] + 1, tr[i + 1][j + 1] = 1;
        else {
          if (dp[i + 1][j] > dp[i][j + 1])
            dp[i + 1][j + 1] = dp[i + 1][j], tr[i + 1][j + 1] = 2;
          else
            dp[i + 1][j + 1] = dp[i][j + 1], tr[i + 1][j + 1] = 3;
        }
      }
    }
    string x;
    for (int i = s.size(), j = t.size(); i > 0 && j > 0;) {
      switch (tr[i][j]) {
      case 1:
        x += s[i - 1], i--, j--;
        break;
      case 2:
        j--;
        break;
      case 3:
        i--;
        break;
      }
    }
    cout << x << endl;
  }
  return 0;
}
Problem p01160 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s, t;
int dp[2011][2011];
int used[2011][2011];
string solve() {
  string ans = "";
  int i = s.size() - 1;
  int j = t.size() - 1;
  while (i > 0 && j > 0) {
    if (used[i][j] == 1)
      ans += t[j], i--, j--;
    else if (used[i][j] == 2)
      i--;
    else
      j--;
  }
  return ans;
}
int main() {
  while (cin >> s) {
    memset(dp, 0, sizeof(dp));
    memset(used, 0, sizeof(used));
    t = s;
    reverse(s.begin(), s.end());
    t = "@" + t;
    s = "@" + s;
    for (int i = 1; i < s.size(); i++)
      for (int j = 1; j < t.size(); j++)
        if (s[i] == t[j]) {
          if (dp[i - 1][j] >= dp[i - 1][j - 1] + 1)
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;

          else
            dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1), used[i][j] = 1;
        } else {
          if (dp[i - 1][j] >= dp[i][j - 1])
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;
          else
            dp[i][j] = max(dp[i][j], dp[i][j - 1]), used[i][j] = 3;
        }
    cout << solve() << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s, t;
int dp[1011][1011];
int used[1011][1011];
string solve() {
  string ans = "";
  int i = s.size() - 1;
  int j = t.size() - 1;
  while (i > 0 && j > 0) {
    if (used[i][j] == 1)
      ans += t[j], i--, j--;
    else if (used[i][j] == 2)
      i--;
    else
      j--;
  }
  return ans;
}
int main() {
  while (cin >> s) {
    memset(dp, 0, sizeof(dp));
    memset(used, 0, sizeof(used));
    t = s;
    reverse(s.begin(), s.end());
    t = "@" + t;
    s = "@" + s;
    for (int i = 1; i < s.size(); i++)
      for (int j = 1; j < t.size(); j++)
        if (s[i] == t[j]) {
          if (dp[i - 1][j] >= dp[i - 1][j - 1] + 1)
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;

          else
            dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1), used[i][j] = 1;
        } else {
          if (dp[i - 1][j] >= dp[i][j - 1])
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;
          else
            dp[i][j] = max(dp[i][j], dp[i][j - 1]), used[i][j] = 3;
        }
    cout << solve() << endl;
  }
}
Problem p01160 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 2005
using namespace std;
string s, t, ans;
int dp[N][N];

void dfs(int y, int x) {
  if (!y || !x)
    return;
  if (dp[y][x] - dp[y - 1][x - 1] == 1 && dp[y][x] - dp[y - 1][x] == 1 &&
      dp[y][x] - dp[y][x - 1] == 1) {
    ans += s[y - 1];
    dfs(y - 1, x - 1);
    return;
  }
  if (dp[y][x - 1] == dp[y][x])
    dfs(y, x - 1);
  else
    dfs(y - 1, x);
}

int main() {
  // IAIDDAII

  while (cin >> s) {
    // s.resize(8);
    // for(int i=0;i<8;i++) s[i]='A'+(rand())%10;
    t = s;
    reverse(t.begin(), t.end());
    memset(dp, 0, sizeof(dp));
    int n = s.size(), m = t.size();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i] == t[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        else
          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
    ans = "";
    dfs(n, m);
    if (ans.size() % 2 == 0) {
      ans = ans.substr(0, ans.size() / 2);
      string tmp = ans;
      reverse(tmp.begin(), tmp.end());
      ans = ans + tmp;
    } else {
      string tmp = ans.substr(0, ans.size() / 2);
      reverse(tmp.begin(), tmp.end());
      ans = ans.substr(0, ans.size() / 2) + ans[ans.size() / 2] + tmp;
    }
    string temp = ans;
    reverse(temp.begin(), temp.end());
    assert(ans == temp);
    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define N 2005
using namespace std;
string s, t, ans;
int dp[N][N];

void dfs(int y, int x) {
  if (!y || !x)
    return;
  if (dp[y][x] - dp[y - 1][x - 1] == 1 && dp[y][x] - dp[y - 1][x] == 1 &&
      dp[y][x] - dp[y][x - 1] == 1) {
    ans += s[y - 1];
    dfs(y - 1, x - 1);
    return;
  }
  if (dp[y - 1][x] == dp[y][x])
    dfs(y - 1, x);
  else
    dfs(y, x - 1);
}

int main() {
  while (cin >> s) {
    t = s;
    reverse(t.begin(), t.end());
    memset(dp, 0, sizeof(dp));
    int n = s.size(), m = t.size();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i] == t[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        else
          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
    ans = "";
    dfs(n, m);
    string temp = ans;
    reverse(temp.begin(), temp.end());
    assert(ans == temp);
    cout << ans << endl;
  }
  return 0;
}