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

#include <iostream>
#include <vector>

using namespace std;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int field[8][8] = {{0}};
bool used[8][8] = {{false}};
vector<int> startX, startY;

bool dfs(int n, int x, int y, int r, int k) {
  if (r == 0) {
    if (k == startX.size()) {
      return true;
    }
    x = startX[k];
    y = startY[k];
    ++k;
  } else if (used[y][x] || field[y][x] < 0) {
    return false;
  }
  r -= field[y][x];
  if (r < 0) {
    return false;
  }
  used[y][x] = true;
  if (r == 0) {
    if (dfs(n, 0, 0, 0, k)) {
      return true;
    }
  } else {
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (x >= 0 && x < n && y >= 0 && y < n) {
        if (dfs(n, nx, ny, r, k)) {
          return true;
        }
      }
    }
  }
  used[y][x] = false;
  return false;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> field[i][j];
        sum += field[i][j];
        if (field[i][j] < 0) {
          startX.push_back(j);
          startY.push_back(i);
        }
        used[i][j] = false;
      }
    }
    cout << (sum == 0 && dfs(n, 0, 0, 0, 0) ? "YES" : "NO") << endl;
    startX.clear();
    startY.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int field[8][8] = {{0}};
bool used[8][8] = {{false}};
vector<int> startX, startY;

bool dfs(int n, int x, int y, int r, int k) {
  if (r == 0) {
    if (k == startX.size()) {
      return true;
    }
    x = startX[k];
    y = startY[k];
    ++k;
  } else if (used[y][x] || field[y][x] < 0) {
    return false;
  }
  r -= field[y][x];
  if (r < 0) {
    return false;
  }
  used[y][x] = true;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (x >= 0 && x < n && y >= 0 && y < n) {
      if (dfs(n, nx, ny, r, k)) {
        return true;
      }
    }
  }
  used[y][x] = false;
  return false;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> field[i][j];
        sum += field[i][j];
        if (field[i][j] < 0) {
          startX.push_back(j);
          startY.push_back(i);
        }
        used[i][j] = false;
      }
    }
    cout << (sum == 0 && dfs(n, 0, 0, 0, 0) ? "YES" : "NO") << endl;
    startX.clear();
    startY.clear();
  }
  return 0;
}
Problem p00620 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#include <vector>
#define pb push_back
using namespace std;
typedef pair<int, int> P;
int n;
vector<P> rt;
// map<vector<vector<int> >,bool> mp;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool inf(int y, int x) {
  if (y >= 0 && y < n && x >= 0 && x < n)
    return true;
  return false;
}
bool cal(vector<vector<int>> &, int);
bool f(vector<vector<int>> &v, int y, int x, int left, int now) {
  if (!left) {
    return cal(v, now + 1);
  }
  for (int i = 0; i < 4; i++) {
    int ny = dy[i] + y, nx = dx[i] + x;
    if (inf(ny, nx) && v[ny][nx] > 0 && left - v[ny][nx] >= 0) {
      int temp = v[ny][nx];
      v[ny][nx] = 0;
      if (f(v, ny, nx, left - temp, now)) {
        return true;
      }
      v[ny][nx] = temp;
    }
  }
  return false;
}

bool cal(vector<vector<int>> &v, int now) {
  if (now == rt.size()) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (v[i][j] != 0)
          return false;
    return true;
  }
  int y = rt[now].first;
  int x = rt[now].second;
  int temp = v[y][x];
  v[y][x] = 0;
  if (f(v, y, x, -temp, now))
    return true;
  v[y][x] = temp;
  return false;
}
int main() {
  while (cin >> n, n) {
    vector<vector<int>> v(n, vector<int>(n));
    rt.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> v[i][j];
        if (v[i][j] < 0)
          rt.pb(P(i, j));
      }
    if (cal(v, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
#define pb push_back
using namespace std;
typedef pair<int, int> P;
int n;
vector<P> rt;
// map<vector<vector<int> >,bool> mp;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool inf(int y, int x) {
  if (y >= 0 && y < n && x >= 0 && x < n)
    return true;
  return false;
}
bool cal(vector<vector<int>> &, int);
bool f(vector<vector<int>> v, int y, int x, int left, int now) {
  if (!left) {
    return cal(v, now + 1);
  }
  for (int i = 0; i < 4; i++) {
    int ny = dy[i] + y, nx = dx[i] + x;
    if (inf(ny, nx) && v[ny][nx] > 0 && left - v[ny][nx] >= 0) {
      int temp = v[ny][nx];
      v[ny][nx] = 0;
      if (f(v, ny, nx, left - temp, now)) {
        return true;
      }
      v[ny][nx] = temp;
    }
  }
  return false;
}

bool cal(vector<vector<int>> &v, int now) {
  if (now == rt.size()) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (v[i][j] != 0)
          return false;
    return true;
  }
  int y = rt[now].first;
  int x = rt[now].second;
  int temp = v[y][x];
  v[y][x] = 0;
  if (f(v, y, x, -temp, now))
    return true;
  v[y][x] = temp;
  return false;
}
int main() {
  while (cin >> n, n) {
    vector<vector<int>> v(n, vector<int>(n));
    rt.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> v[i][j];
        if (v[i][j] < 0)
          rt.pb(P(i, j));
      }
    if (cal(v, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p00621 (C++) — buggy submission, verdict: Runtime Error

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

struct st {
  int id, s, g;
};
int main() {
  int w, q;
  while (cin >> w >> q, w || q) {
    deque<st> que;
    rep(i, q) {
      char c;
      cin >> c;
      if (c == 'w') {
        int d;
        cin >> d;
        for (auto itr = que.begin(); itr != que.end(); itr++) {
          if ((*itr).id == d) {
            que.erase(itr);
            break;
          }
        }
        continue;
      }
      int d, k;
      cin >> d >> k;
      int t;
      if (que.empty()) {
        if (k <= w) {
          cout << "0" << endl;
          que.push_back({d, 0, k});
        } else
          cout << "impossible" << endl;
        continue;
      } else if (k <= (*que.begin()).s) {
        cout << "0" << endl;
        que.push_front({d, 0, k});
        continue;
      } else {
        rep(j, (int)que.size() - 1) {
          if (que[j].g + k <= que[j + 1].s) {
            cout << que[j].g << endl;
            que.insert(que.begin() + j + 1, {d, que[j].g, que[j].g + k});
            goto g;
          }
        }
      }
      if ((t = (*que.rbegin()).g) + k <= w) {
        cout << t << endl;
        que.push_back({
            d,
            t,
            t + k,
        });
        continue;
      }
      cout << "impossible" << endl;
    g:;
    }
    cout << "END" << endl;
  }
}

Error: 0

Fixed submission:

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

struct st {
  int id, s, g;
};
int main() {
  int w, q;
  while (cin >> w >> q, w || q) {
    deque<st> que;
    rep(i, q) {
      char c;
      cin >> c;
      if (c == 'w') {
        int d;
        cin >> d;
        for (auto itr = que.begin();; itr++) {
          if ((*itr).id == d) {
            que.erase(itr);
            break;
          }
        }
        continue;
      }
      int d, k;
      cin >> d >> k;
      rep(j, (int)que.size() - 1) {
        if (que[j].g + k <= que[j + 1].s) {
          cout << que[j].g << endl;
          que.insert(que.begin() + j + 1, {d, que[j].g, que[j].g + k});
          goto g;
        }
      }
      int t;
      if (que.empty() && k <= w) {
        if (k <= w) {
          cout << "0" << endl;
          que.push_back({d, 0, k});
        }
        // else cout << "impossible" << endl;
      } else if ((t = (*que.rbegin()).g) + k <= w) {
        cout << t << endl;
        que.push_back({
            d,
            t,
            t + k,
        });
      } else if (k <= (*que.begin()).s) {
        cout << "0" << endl;
        que.push_front({d, 0, k});
      } else
        cout << "impossible" << endl;
    g:;
    }
    cout << "END" << endl;
  }
}
Problem p00621 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main() {

  int w, q;
  int wall[100];
  int i, j;
  char dami[5], action;
  int n, haba;
  int count;
  int mark;
  int mark2;

  while (1) {
    scanf("%d %d", &w, &q);
    if (w == 0 && q == 0)
      break;

    for (i = 0; i < w; i++) {
      wall[i] = -1;
    }

    for (i = 0; i < q; i++) {
      scanf("%s", dami);
      action = dami[0];

      if (action == 's') {

        scanf("%d %d", &n, &haba);
        count = 0;
        for (j = 0; j < w; j++) {

          if (count == 0 && wall[j] < 0) {
            mark = j;
            count++;
          } else if (wall[j] < 0) {
            count++;
          } else if (wall[j] >= 0) {
            count = 0;
          }

          if (count == haba) {
            mark2 = j;
            break;
          }
        }

        if (count == haba) {
          printf("%d\\n", mark);
          for (j = mark; j <= mark2; j++) {
            wall[j] = n;
          }
        } else {
          printf("impossible\\n");
        }
      } else if (action == 'w') {
        scanf("%d", &n);
        for (j = 0; j < w; j++) {
          if (wall[j] == n) {
            wall[j] = -1;
          }
        }
      }
    }

    printf("END\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {

  int w, q;
  int wall[100];
  int i, j;
  char dami, action;
  int n, haba;
  int count;
  int mark;

  while (1) {

    scanf("%d %d", &w, &q);
    if (w == 0 && q == 0)
      break;

    for (i = 0; i < w; i++) {
      wall[i] = -1;
    }

    for (i = 0; i < q; i++) {
      scanf("%c", &dami);
      scanf("%c", &action);

      if (action == 's') {

        scanf("%d %d", &n, &haba);
        count = 0;
        for (j = 0; j < w; j++) {

          if (count == 0 && wall[j] < 0) {
            mark = j;
            count++;
          } else if (wall[j] < 0) {
            count++;
          } else if (wall[j] >= 0) {
            count = 0;
          }

          if (count == haba)
            break;
        }

        if (count == haba) {

          printf("%d\\n", mark);

          for (j = mark; j < mark + haba; j++) {

            wall[j] = n;
          }
        } else {

          printf("imposible\\n");
        }
      } else if (action == 'w') {

        scanf("%d", &n);

        for (j = 0; j < w; j++) {

          if (wall[j] == n) {
            wall[j] = -1;
          }
        }
      }
    }

    printf("END\\n");
  }

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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;

int kabe[201];

main() {
  int W, Q;

  while (cin >> W >> Q) {
    if (W == 0 && Q == 0)
      break;

    for (int i = 0; i < 101; i++) {
      kabe[i] = -1;
    }

    for (int i = 0; i < Q; i++) {
      char c;
      cin >> c;

      if (c == 's') {
        int id, w;
        cin >> id >> w;

        int f = 0, b = 0;

        for (int i = 0; i < W; i++) {
          int haba = 0, b = i;

          for (; haba + i < W; ++haba) {
            if (kabe[haba + i] != -1) {
              break;
            }
          }

          if (haba >= w) {
            cout << b << endl;
            for (int i = 0; i < w; i++) {
              kabe[b + i] = id;
            }
            f = 1;
            break;
          }
        }
        if (f == 0)
          puts("impossible");
      } else {
        int id;
        cin >> id;
        for (int i = 0; i <= W; i++) {
          if (kabe[i] == id)
            kabe[i] = -1;
        }
      }
    }
    puts("END");
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int W, Q, id, w, i, j, k, kabe[10];
  char c;
  while (0 <= scanf("%d%d", &W, &Q)) {
    if (W == 0 && Q == 0)
      break;
    i = W;
    while (i--)
      kabe[i] = -1;

    while (Q--) {
      scanf(" %c ", &c);
      if (c == 's') {
        scanf("%d%d", &id, &w);
        for (i = 0; i <= W - w; i++) {
          if (kabe[i] == -1) {
            k = 0;
            for (j = i; j < i + w; j++) {
              if (kabe[j] == -1)
                ++k;
            }
            if (k >= w) {
              printf("%d\\n", i);
              for (j = i; j < i + w; j++)
                kabe[j] = id;
              break;
            }
          }
        }
        if (k < w)
          printf("impossible\\n");
      }
      if (c == 'w') {
        scanf("%d", &id);
        for (i = 0; i < W; i++) {
          if (kabe[i] == id)
            kabe[i] = -1;
        }
      }
    }
    printf("END\\n");
  }
  return 0;
}
Problem p00621 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int x[1000], n, q, a, b;
int y[1000][2];
char c;

int main() {
  while (true) {
    for (int i = 0; i < 1000; i++) {
      x[i] = 0;
      y[i][0] = 0;
      y[i][1] = 0;
    }
    cin >> n >> q;
    if (n == 0 && q == 0) {
      break;
    }
    for (int i = 0; i < q; i++) {
      cin >> c >> a;
      if (c == 's') {
        cin >> b;
        for (int j = 0; j <= n - b; j++) {
          int t = b;
          for (int k = j; k < j + b; k++) {
            if (x[k] == 0) {
              t--;
            }
          }
          if (t == 0) {
            cout << j << endl;
            for (int k = j; k < j + b; k++) {
              x[k] = 1;
            }
            y[a][0] = j;
            y[a][1] = j + b;
            goto E;
          }
        }
        cout << "impossible" << endl;
      E:;
      }
      if (c == 'w') {
        for (int j = y[a][0]; j < y[a][1]; j++) {
          x[j] = 0;
        }
        y[a][0] = 0;
        y[a][1] = 0;
      }
    }
    cout << "END" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int x[1000], n, q, a, b;
int y[1000][2];
char c;

int main() {
  while (true) {
    for (int i = 0; i < 1000; i++) {
      x[i] = 0;
      y[i][0] = 0;
      y[i][1] = 0;
    }
    cin >> n >> q;
    if (n == 0 && q == 0) {
      break;
    }
    for (int i = 0; i < q; i++) {
      cin >> c >> a >> b;
      if (c == 's') {
        for (int j = 0; j <= a - b; j++) {
          int t = b;
          for (int k = j; k < j + b; k++) {
            if (x[k] == 0) {
              t--;
            }
          }
          if (t == 0) {
            cout << j << endl;
            for (int k = j; k < j + b; k++) {
              x[k] = 1;
            }
            y[a][0] = j;
            y[a][1] = j + b;
            goto E;
          }
        }
        cout << "impossible" << endl;
      E:;
      }
      if (c == 'w') {
        for (int j = y[a][0]; j < y[a][1]; j++) {
          x[j] = 0;
        }
        y[a][0] = 0;
        y[a][1] = 0;
      }
    }
    cout << "END" << endl;
  }
  return 0;
}
Problem p00621 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int W, Q, a, b, e, x[107], d[107], l[107], r[107];
char c;
int main() {
  while (cin >> W >> Q, W | Q) {
    for (int i = 0; i < 107; i++)
      x[i] = d[i] = l[i] = r[i] = 0;
    e = 0;
    for (int i = 0; i < Q; i++) {
      cin >> c >> a;
      if (c == 's') {
        cin >> b;
        int f = 0;
        for (int j = 0; j <= W - b; j++) {
          if (find(x + j, x + j + b, 1) == x + j + b) {
            fill(x + j, x + j + b, 1);
            d[e] = a;
            l[e] = j, r[e] = j + b;
            cout << j << endl;
            f = 1;
            e++;
            break;
          }
        }
        if (!f)
          cout << "impossible" << endl;
      } else {
        for (int j = 0; j < e; j++) {
          if (d[j] == a)
            fill(x + l[j], x + r[j], 0);
        }
      }
    }
    cout << "END" << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int W, Q, u, v, a, b, e, x[107], d[107], l[107], r[107];
char c;
int main() {
  while (cin >> W >> Q, W && Q) {
    fill(x, x + W, 0);
    for (int i = 0; i < Q; i++) {
      cin >> c >> a;
      if (c == 's') {
        cin >> b;
        int f = 0;
        for (int j = 0; j <= W - b; j++) {
          if (find(x + j, x + j + b, 1) == x + j + b) {
            fill(x + j, x + j + b, 1);
            d[e] = a;
            l[e] = j, r[e] = j + b;
            cout << j << endl;
            f = 1;
            e++;
            break;
          }
        }
        if (!f)
          cout << "impossible" << endl;
      } else {
        for (int j = 0; i < e; j++) {
          if (d[j] == a)
            fill(x + l[j], x + r[j], 0);
        }
      }
    }
    cout << "END" << endl;
  }
}
Problem p00621 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int d[110], w, q;

int check(int wide) {
  int cnt = 0;
  rep(i, 0, w + 1) {
    if (d[i]) {
      cnt = 0;
    } else {
      cnt++;
    }
    if (cnt == wide)
      return i + 1 - cnt;
  }
  return -1;
}

signed main() {
  while (1) {
    cin >> w >> q;
    if (w + q == 0)
      break;
    char c;
    int id, wide;
    rep(i, 0, w) d[i] = 0;
    d[w] = 1;

    map<int, pii> mp;
    rep(i, 0, q) {
      cin >> c;
      if (c == 's') {
        cin >> id >> wide;
        mp[id].first = wide;
        int ind = check(wide);
        if (ind == -1)
          o("impossible");
        else {
          o(ind);
          mp[id].second = ind;
          rep(j, 0, wide) { d[ind + j] = 1; }
        }
      } else {
        cin >> id;
        rep(j, 0, mp[id].first) { d[mp[id].second + j] = 0; }
      }
    }
    o("END");
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int d[110], w, q;

int check(int wide) {
  int cnt = 0;
  rep(i, 0, w + 1) {
    if (d[i]) {
      cnt = 0;
    } else {
      cnt++;
    }
    if (cnt = wide)
      return i + 1 - cnt;
  }
  return -1;
}

signed main() {
  while (1) {
    cin >> w >> q;
    if (w + q == 0)
      break;
    char c;
    int id, wide;
    rep(i, 0, w) d[i] = 0;
    d[w] = 1;

    map<int, pii> mp;
    rep(i, 0, q) {
      cin >> c;
      if (c == 's') {
        cin >> id >> wide;
        mp[id].first = wide;
        int ind = check(wide);
        if (ind == -1)
          o("impossible");
        else {
          o(ind);
          mp[id].second = ind;
          rep(j, 0, wide) { d[ind + j] = 1; }
        }
      } else {
        cin >> id;
        rep(j, 0, mp[id].first) { d[mp[id].second + j] = 0; }
      }
    }
    o("END");
  }
}
Problem p00622 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string A, B, C, D;
  while (cin >> A, A != "-") {
    cin >> B >> C;
    char m = ' ';
    D.clear();

    int a, b;
    a = 0;
    b = 0;
    for (int i = 0; a < A.size() || b < B.size();) {
      if (i < C.size()) {
        if (C[i] == m)
          m = A[a++], i++;
        else
          goto N;
      } else {
      N:
        D += m;
        m = B[b++];
      }
    }
    D += m;
    cout << D.substr(1, D.size() - 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cassert>
#include <deque>
#include <iostream>
using namespace std;

char Insert(deque<char> &A, deque<char> &B, char target) {
  if (A.front() == target) {
    A.pop_front();
    return target;
  } else if (B.front() == target) {
    B.pop_front();
    return target;
  } else
    return target;
}

int main() {
  deque<char> A, B;
  while (true) {
    string ans;
    string in;
    cin >> in;
    if (in == "-")
      break;
    for (int i = 0; i < in.size(); i++)
      A.push_back(in[i]);
    cin >> in;
    for (int i = 0; i < in.size(); i++)
      B.push_back(in[i]);
    cin >> in;

    char mid = ' ';

    while (!A.empty() || !B.empty()) {
      if (mid == ' ')
        mid = Insert(A, B, in[0]);

      if (in.find(mid) != string::npos)
        mid = Insert(A, B, A.front());
      else
        ans += mid, mid = Insert(A, B, B.front());
    }
    ans += mid;
    cout << ans << endl;
  }
  return 0;
}
Problem p00622 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string t, l, ad, ar, d, r;
int ct, cl;
char cc;

void push_right() {
  r += cc;
  cc = l[cl++];
}

void push_down() {
  d += cc;
  cc = t[ct++];
}

bool rec(int s) {
  char tmpc;
  if (s == t.length() + l.length() - 1) {
    int f;
    if (d.length() != t.length()) {
      d += cc;
      f = 0;
    } else {
      r += cc;
      f = 1;
    }
    if (d == ad) {
      ar = r;
      return true;
    }
    if (f == 0)
      d = d.substr(0, d.length() - 1);
    else
      r = r.substr(0, r.length() - 1);
    return false;
  }
  if (d.length() != 0 && d[d.length() - 1] != ad[d.length() - 1])
    return false;
  if (cl < l.length()) {
    tmpc = cc;
    push_right();
    if (rec(s + 1))
      return true;
    r = r.substr(0, r.length() - 1);
    cl--;
    cc = tmpc;
  }
  if (ct < t.length()) {
    tmpc = cc;
    push_down();
    if (rec(s + 1))
      return true;
    d = d.substr(0, d.length() - 1);
    ct--;
    cc = tmpc;
  }
  return false;
}

string solve() {
  cc = l[0];
  r = d = "";
  ct = 0;
  cl = 1;
  if (rec(0)) {
    return ar;
  } else {
    return "NA";
  }
}

int main() {
  while (1) {
    cin >> t;
    if (t == "-")
      break;
    cin >> l;
    cin >> ad;
    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

string t, l, ad, ar, d, r;
int ct, cl;
char cc;

void push_right() {
  r += cc;
  cc = l[cl++];
}

void push_down() {
  d += cc;
  cc = t[ct++];
}

bool rec(int s) {
  char tmpc;
  if (s == t.length() + l.length() - 1) {
    int f;
    if (d.length() != t.length()) {
      d += cc;
      f = 0;
    } else {
      r += cc;
      f = 1;
    }
    if (d == ad) {
      ar = r;
      return true;
    }
    if (f == 0)
      d = d.substr(0, d.length() - 1);
    else
      r = r.substr(0, r.length() - 1);
    return false;
  }
  if (cl < l.length()) {
    tmpc = cc;
    push_right();
    if (rec(s + 1))
      return true;
    r = r.substr(0, r.length() - 1);
    cl--;
    cc = tmpc;
  }
  if (ct < t.length()) {
    tmpc = cc;
    push_down();
    if (rec(s + 1))
      return true;
    d = d.substr(0, d.length() - 1);
    ct--;
    cc = tmpc;
  }
  return false;
}

string solve() {
  cc = l[0];
  r = d = "";
  ct = 0;
  cl = 1;
  if (rec(0)) {
    return ar;
  } else {
    return "NA";
  }
}

int main() {
  while (1) {
    cin >> t;
    if (t == "-")
      break;
    cin >> l;
    cin >> ad;
    cout << solve() << endl;
  }
  return 0;
}