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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct skill {
  int c, t;
  skill() {}
  skill(int c, int t) : c(c), t(t) {}
};
vector<vector<int>> G;
bool used[111], tmp[111];
bool dfs(int v, int d) {
  if (d > 111)
    return 0;
  tmp[v] = 1;
  bool f = 1;
  for (int i = 0; f && (i < (int)G[v].size()); i++)
    if (!used[G[v][i]])
      f &= dfs(G[v][i], d + 1);
  return f;
}
bool solve(int m) {
  bool f = 1;
  for (int i = 0; f && (i < m); i++) {
    memset(tmp, 0, sizeof(tmp));
    if (!used[i])
      f &= dfs(i, 0);
    for (int j = 0; j < m; j++)
      used[j] |= tmp[j];
    // cout<<i<<endl;
  }
  return f;
}
int main() {
  int m, n;
  cin >> m >> n;
  vector<skill> ss[m][n];
  for (int i = 0; i < m; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int s, t;
      string c;
      cin >> s >> c >> t;
      ss[i][s - 1].push_back(skill(c == "<=", t));
    }
  }
  bool ff = 0;
  G.resize(m);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      bool f = 0;
      for (int k = 0; k < n; k++) {
        for (int x = 0; x < (int)ss[i][k].size(); x++) {
          for (int y = 0; y < (int)ss[j][k].size(); y++) {
            if (ss[i][k][x].t <= ss[j][k][y].t) {
              f |= (ss[i][k][x].c == 1 && ss[j][k][y].c == 0);
            }
          }
        }
      }
      if (f)
        G[j].push_back(i);
      ff |= ((i == j) && f);
      // if(f) cout<<i<<":"<<j<<endl;
    }
  }
  cout << (!ff && solve(m) ? "Yes" : "No") << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct skill {
  int c, t;
  skill() {}
  skill(int c, int t) : c(c), t(t) {}
};
vector<vector<int>> G;
bool dfs(int v, int d) {
  if (d > 111)
    return 0;
  bool f = 1;
  for (int i = 0; i < (int)G[v].size() && f; i++)
    f &= dfs(G[v][i], d + 1);
  return f;
}
bool solve(int m) {
  bool f = 1;
  for (int i = 0; i < m; i++)
    f &= dfs(i, 0);
  return f;
}
int main() {
  int m, n;
  cin >> m >> n;
  vector<skill> ss[m][n];
  for (int i = 0; i < m; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int s, t;
      string c;
      cin >> s >> c >> t;
      ss[i][s - 1].push_back(skill(c == "<=", t));
    }
  }
  bool ff = 0;
  G.resize(m);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      bool f = 0;
      for (int k = 0; k < n; k++) {
        for (int x = 0; x < (int)ss[i][k].size(); x++) {
          for (int y = 0; y < (int)ss[j][k].size(); y++) {
            if (ss[i][k][x].t <= ss[j][k][y].t) {
              f |= (ss[i][k][x].c == 1 && ss[j][k][y].c == 0);
            }
          }
        }
      }
      if (f)
        G[j].push_back(i);
      ff |= ((i == j) && f);
      // if(f) cout<<i<<":"<<j<<endl;
    }
  }
  cout << (!ff && solve(m) ? "Yes" : "No") << endl;
  return 0;
}
Problem p01667 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)

int M, N, ub[111][111], lb[111][111], skill[111];
bool learned[111];

bool all_learned() {
  for_(i, 0, M) if (!learned[i]) return false;
  return true;
}

int main() {
  cin >> M >> N;

  for_(i, 0, M) for_(j, 0, N) {
    ub[i][j] = 100;
    lb[i][j] = 0;
  }

  for_(i, 0, M) {
    int K;
    cin >> K;

    for_(j, 0, K) {
      int s, t;
      string cnd;
      cin >> s >> cnd >> t;
      --s;

      if (cnd == ">=")
        lb[i][s] = max(lb[i][s], t);
      else
        ub[i][s] = min(ub[i][s], t);
    }
  }

  memset(skill, 0, sizeof(skill));
  memset(learned, 0, sizeof(learned));

  for_(i, 0, M) {
    bool learn = true;
    for_(j, 0, N) learn &= (lb[i][j] <= skill[j] && skill[j] <= ub[i][j]);
    if (learn)
      learned[i] = true;
  }

  while (!all_learned()) {
    int tar = -1;

    for_(i, 0, N) {
      bool isok = true;

      for_(j, 0, M) {
        if (learned[j])
          continue;
        isok &= (skill[i] + 1 <= ub[j][i]);
      }

      if (isok) {
        tar = i;
        break;
      }
    }

    if (tar == -1) {
      cout << "No" << endl;
      return 0;
    }

    ++skill[tar];

    for_(i, 0, M) {
      bool learn = true;
      for_(j, 0, N) learn &= (lb[i][j] <= skill[j] && skill[j] <= ub[i][j]);
      if (learn)
        learned[i] = true;
    }
  }

  cout << "Yes" << endl;
}

Error: TLE

Fixed submission:

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)

int M, N, ub[111][111], lb[111][111], skill[111];

int main() {
  cin >> M >> N;

  for_(i, 0, M) for_(j, 0, N) {
    ub[i][j] = 100;
    lb[i][j] = 0;
  }

  for_(i, 0, M) {
    int K;
    cin >> K;

    for_(j, 0, K) {
      int s, t;
      string cnd;
      cin >> s >> cnd >> t;
      --s;

      if (cnd == ">=")
        lb[i][s] = max(lb[i][s], t);
      else
        ub[i][s] = min(ub[i][s], t);
    }

    for_(j, 0, N) if (ub[i][j] < lb[i][j]) {
      cout << "No" << endl;
      return 0;
    }
  }

  memset(skill, 0, sizeof(skill));

  unordered_set<int> learned;

  while ((int)learned.size() < N) {
    int tar = -1;

    for_(i, 0, N) {
      bool isok = true;

      for_(j, 0, M) {
        if (learned.count(j))
          continue;
        isok &= (skill[i] + 1 <= ub[j][i]);
      }

      if (isok) {
        tar = i;
        break;
      }
    }

    if (tar == -1) {
      cout << "No" << endl;
      return 0;
    }

    ++skill[tar];

    for_(i, 0, M) {
      bool learn = true;
      for_(j, 0, N) learn &= (lb[i][j] <= skill[j] && skill[j] <= ub[i][j]);
      if (learn)
        learned.insert(i);
    }
  }

  cout << "Yes" << endl;
}
Problem p01667 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
typedef pair<int, int> pii;
#define mkp make_pair
int INF = INT_MAX / 2;
int m, n;
vector<vector<pii>> v;
vector<vector<int>> vv;
vector<int> used;
bool foo(int a) {
  // cout<<"a "<<a<<endl;
  if (used[a] == 1)
    return false;
  if (used[a] == 2)
    return true;
  used[a] = 1;
  for (int x : vv[a]) {
    // cout<<"x "<<x<<endl;
    if (!foo(x)) {
      used[a] = 0;
      return false;
    }
  }
  used[a] = 2;
  return true;
}
int main() {
  cin >> m >> n;
  used = vector<int>(m, 0);
  v = vector<vector<pii>>(m, vector<pii>(n, pii(0, INF)));
  rep(i, m) {
    int K;
    cin >> K;
    rep(j, K) {
      int a, b;
      string s;
      cin >> a >> s >> b;
      if (s == ">=") {
        v[i][a - 1].first = max(v[i][a - 1].first, b);
      } else {
        v[i][a - 1].second = min(v[i][a - 1].second, b);
      }
    }
  }
  vv = vector<vector<int>>(m);
  rep(i, m) {
    rep(j, m) {
      bool f = false;
      rep(k, n) {
        if (v[i][k].first > v[j][k].second)
          f = true;
      }
      if (f) {
        // cout<<i<<" "<<j<<endl;
        vv[i].push_back(j);
      }
    }
  }
  // return 0;
  bool f = true;
  rep(i, m) {
    if (!foo(i))
      f = false;
  }
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

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

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
typedef pair<int, int> pii;
#define mkp make_pair
int INF = INT_MAX / 2;
int m, n;
vector<vector<pii>> v;
vector<vector<int>> vv;
vector<int> used;
bool foo(int a) {
  // cout<<"a "<<a<<endl;
  if (used[a])
    return false;
  used[a] = 1;
  for (int x : vv[a]) {
    // cout<<"x "<<x<<endl;
    if (!foo(x)) {
      used[a] = 0;
      return false;
    }
  }
  used[a] = 0;
  return true;
}
int main() {
  cin >> m >> n;
  used = vector<int>(m, 0);
  v = vector<vector<pii>>(m, vector<pii>(n, pii(0, INF)));
  rep(i, m) {
    int K;
    cin >> K;
    rep(j, K) {
      int a, b;
      string s;
      cin >> a >> s >> b;
      if (s == ">=") {
        v[i][a - 1].first = max(v[i][a - 1].first, b);
      } else {
        v[i][a - 1].second = min(v[i][a - 1].second, b);
      }
    }
  }
  vv = vector<vector<int>>(m);
  rep(i, m) {
    rep(j, m) {
      bool f = false;
      rep(k, n) {
        if (v[i][k].first > v[j][k].second)
          f = true;
      }
      if (f) {
        // cout<<i<<" "<<j<<endl;
        vv[i].push_back(j);
      }
    }
  }
  // return 0;
  bool f = true;
  rep(i, m) {
    if (!foo(i))
      f = false;
  }
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p01667 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define l first
#define r second
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
int ans = 1, m, n, used[101], memo[101][101];
P cmd[101][101];

int check(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  for (int i = 0; i < n; i++)
    if (cmd[a][i].l > cmd[b][i].r)
      return memo[a][b] = 0;
  return memo[a][b] = 1;
}

int solve() {
  memset(memo, -1, sizeof(memo));
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    int j = 0;
    while (!used[i] && j < m && (used[j] || check(i, j)))
      j++;
    if (j == m)
      used[i] = 1, cnt++, i = -1;
  }
  return cnt / m;
}

int main() {
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 101; j++)
      cmd[i][j] = P(-INF, INF);

  cin >> m >> n;
  for (int i = 0, k; i < m; i++) {
    cin >> k;
    while (k--) {
      string cond;
      int s, t;
      cin >> s >> cond >> t;
      s--;
      if (cond == "<=")
        cmd[i][s].r = min(cmd[i][s].r, t);
      if (cond == ">=")
        cmd[i][s].l = max(cmd[i][s].l, t);
      if (cmd[i][s].l > cmd[i][s].r)
        ans = 0;
    }
  }

  cout << (ans * solve() ? "Yes" : "No") << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define l first
#define r second
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
int ans = 1, m, n, used[101], memo[101][101];
P cmd[101][101];

int check(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  for (int i = 0; i < n; i++)
    if (cmd[a][i].l > cmd[b][i].r)
      return memo[a][b] = 0;
  return memo[a][b] = 1;
}

int solve() {
  memset(memo, -1, sizeof(memo));
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    int j = 0;
    while (j < m && (used[j] || check(i, j)))
      j++;
    if (j == m)
      used[i] = 1, cnt++, i = -1;
  }
  return cnt / m;
}

int main() {
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 101; j++)
      cmd[i][j] = P(-INF, INF);

  cin >> m >> n;
  for (int i = 0, k; i < m; i++) {
    cin >> k;
    while (k--) {
      string cond;
      int s, t;
      cin >> s >> cond >> t;
      s--;
      if (cond == "<=")
        cmd[i][s].r = min(cmd[i][s].r, t);
      if (cond == ">=")
        cmd[i][s].l = max(cmd[i][s].l, t);
      if (cmd[i][s].l > cmd[i][s].r)
        ans = 0;
    }
  }

  cout << (ans * solve() ? "Yes" : "No") << endl;
  return 0;
}
Problem p01670 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

typedef bitset<300000 / 10> BS;

BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];

int rec(const BS &bs, int nth, int r) {
  if (r < 0)
    return r;
  if (bs == absbs)
    return r;
  while ((is[idx[nth]] & ~bs).none()) {
    nth++;
  }
  BS n = is[idx[nth]] & ~bs;
  if (n.count() == 1) {
    for (int i = 0;; i++) {
      if (n[i]) {
        return rec(bs | is[a[i] ^ b[i] ^ idx[nth]], nth + 1, r - 1);
      }
    }
  } else {
    int rv = rec(bs | is[idx[nth]], nth + 1, r - 1);
    BS o = bs;
    int c = 0;
    for (int i = 0; n.any(); i++) {
      if (n[i]) {
        o |= bs | is[a[i] ^ b[i] ^ idx[nth]];
        c++;
        n[i] = false;
      }
    }
    return max(rv, rec(o, nth + 1, r - c));
  }
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int pop[32345] = {};
  for (int i = 0; i < M; i++) {
    absbs[i] = true;
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    pop[a[i]]++;
    pop[b[i]]++;
    is[a[i]][i] = true;
    is[b[i]][i] = true;
  }
  idx.resize(N);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
  int r = rec(BS(), 0, K);
  if (r < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << K - r << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

typedef bitset<300000> BS;

BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];

int rec(const BS &bs, int nth, int r) {
  if (r < 0)
    return r;
  if (bs == absbs)
    return r;
  while ((is[idx[nth]] & ~bs).none()) {
    nth++;
  }
  BS n = is[idx[nth]] & ~bs;
  if (n.count() == 1) {
    for (int i = 0;; i++) {
      if (n[i]) {
        return rec(bs | is[a[i] ^ b[i] ^ idx[nth]], nth + 1, r - 1);
      }
    }
  } else {
    int rv = rec(bs | is[idx[nth]], nth + 1, r - 1);
    BS o = bs;
    int c = 0;
    for (int i = 0; n.any(); i++) {
      if (n[i]) {
        o |= bs | is[a[i] ^ b[i] ^ idx[nth]];
        c++;
        n[i] = false;
      }
    }
    return max(rv, rec(o, nth + 1, r - c));
  }
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int pop[32345] = {};
  for (int i = 0; i < M; i++) {
    absbs[i] = true;
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    pop[a[i]]++;
    pop[b[i]]++;
    is[a[i]][i] = true;
    is[b[i]][i] = true;
  }
  idx.resize(N);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
  int r = rec(BS(), 0, K);
  if (r < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << K - r << endl;
  }
}
Problem p01670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int n, k, ans, cnt, tmp;
vector<set<Int>> G;
void dfs(Int x) {
  // cout<<x<<endl;
  if (x == n)
    ans = min(ans, tmp);
  if (tmp >= ans)
    return;

  if (G[x].empty()) {
    dfs(x + 1);
    return;
  }

  if (tmp + 1 < ans && G[x].size() > 1u) {
    vector<Int> v;
    for (Int u : G[x]) {
      v.emplace_back(u);
      G[u].erase(x);
    }
    cnt++;
    tmp++;
    dfs(x + 1);
    cnt--;
    tmp--;
    for (Int u : v) {
      G[u].emplace(x);
    }
  }

  if (tmp + (Int)G[x].size() < ans) {
    vector<Int> v;
    vector<set<Int>> S;
    for (Int u : G[x]) {
      if (G[u].empty())
        continue;
      v.emplace_back(u);
      S.emplace_back(G[u]);
      G[u].clear();
      cnt++;
      tmp++;
    }

    for (Int i = 0; i < (Int)v.size(); i++)
      for (int w : S[i])
        G[w].erase(v[i]);

    cnt++;
    dfs(x + 1);
    cnt--;

    for (Int i = 0; i < (Int)v.size(); i++) {
      G[v[i]] = S[i];
      for (int w : G[v[i]])
        G[w].emplace(v[i]);
      cnt--;
      tmp--;
    }
  }
};
signed main() {
  Int m, k;
  cin >> n >> m >> k;
  G.resize(n);
  for (Int i = 0; i < m; i++) {
    Int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace(v);
    G[v].emplace(u);
  }
  ans = k + 1;
  cnt = tmp = 0;
  dfs(0);
  if (ans <= k)
    cout << ans << endl;
  else
    cout << "Impossible" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int n, k, ans, cnt, tmp;
vector<set<Int>> G;
void dfs(Int x) {
  // cout<<x<<endl;
  if (x == n)
    ans = min(ans, tmp);
  if (tmp >= ans)
    return;

  if (G[x].empty()) {
    dfs(x + 1);
    return;
  }

  if (tmp + 1 < ans && G[x].size() > 1u) {
    vector<Int> v;
    for (Int u : G[x]) {
      v.emplace_back(u);
      G[u].erase(x);
    }
    cnt++;
    tmp++;
    dfs(x + 1);
    cnt--;
    tmp--;
    for (Int u : v) {
      G[u].emplace(x);
    }
  }

  if (tmp + (Int)G[x].size() < ans) {
    vector<Int> v;
    vector<set<Int>> S;
    for (Int u : G[x]) {
      v.emplace_back(u);
      S.emplace_back(G[u]);
      G[u].clear();
      cnt++;
      tmp++;
    }

    cnt++;
    dfs(x + 1);
    cnt--;

    for (Int i = 0; i < (Int)v.size(); i++) {
      G[v[i]] = S[i];
      cnt--;
      tmp--;
    }
  }
};
signed main() {
  Int m, k;
  cin >> n >> m >> k;
  G.resize(n);
  for (Int i = 0; i < m; i++) {
    Int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace(v);
    G[v].emplace(u);
  }
  ans = n;
  cnt = tmp = 0;
  dfs(0);
  if (ans <= k)
    cout << ans << endl;
  else
    cout << "Impossible" << endl;
  return 0;
}
Problem p01676 (C++) — buggy submission, verdict: Runtime Error

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

struct edge {
  int to, from;
  edge(int to, int from) : to(to), from(from) {}
  edge() {}
};

int N, M;
bool used[5000];
set<int> G[555];
vector<edge> E;

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    E.push_back(edge(t, s));
    G[s].insert(i);
    G[t].insert(i);
  }
  if (M == 0) {
    cout << 0 << endl;
    return 0;
  }
  bool ed = true;
  int res = 0;
  while (ed) {
    int minid = 0;
    while (G[minid].empty())
      minid++;
    for (int i = 0; i < N; i++) {
      if (G[i].empty())
        continue;
      if (G[minid].size() > G[i].size())
        minid = i;
    }
    //   cout << G[minid].size() << endl;
    res += G[minid].size() - 1;
    for (auto it = G[minid].begin(); it != G[minid].end(); it++) {
      used[*it] = true;
      // cout << "new " << *it << endl;
      for (int i = 0; i < N; i++) {
        if (i == minid)
          continue;
        G[i].erase(*it);
      }
    }
    G[minid].clear();
    ed = false;
    for (int i = 0; i < M; i++)
      if (!used[i])
        ed = true;
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

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

struct edge {
  int to, from;
  edge(int to, int from) : to(to), from(from) {}
  edge() {}
};

int N, M;
bool used[5000];
set<int> G[555];
vector<edge> E;

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    E.push_back(edge(t, s));
    G[s].insert(i);
    G[t].insert(i);
  }
  bool ed = true;
  int res = 0;
  while (ed) {
    int minid = 0;
    while (G[minid].empty())
      minid++;
    for (int i = 0; i < N; i++) {
      if (G[i].empty())
        continue;
      if (G[minid].size() > G[i].size())
        minid = i;
    }
    //   cout << G[minid].size() << endl;
    res += G[minid].size() - 1;
    for (auto it = G[minid].begin(); it != G[minid].end(); it++) {
      used[*it] = true;
      // cout << "new " << *it << endl;
      for (int i = 0; i < N; i++) {
        if (i == minid)
          continue;
        G[i].erase(*it);
      }
    }
    G[minid].clear();
    ed = false;
    for (int i = 0; i < M; i++)
      if (!used[i])
        ed = true;
  }
  cout << res << endl;
}
Problem p01678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 1e9 + 7;

void search(vector<int> &in, vector<lli> &out, int c, int st) {
  if (in[0] != -1 && in[1] != -1 && in[2] != -1) {
    if (in[0] + in[1] + c == in[2])
      out[0]++;
    if (in[0] + in[1] + c == in[2] + 10)
      out[1]++;
    return;
  }
  for (int i = 0; i < 3; i++) {
    if (in[i] == -1) {
      for (int d = st; d <= 9; d++) {
        in[i] = d;
        search(in, out, c, st);
        in[i] = -1;
      }
      return;
    }
  }
}

int main() {
  while (1) {
    string str[3];
    cin >> str[0];
    if (str[0] == "0")
      break;
    cin >> str[1] >> str[2];
    int n = str[0].length();

    vector<vector<lli>> pos_c(n, vector<lli>(2, 0));
    vector<vector<lli>> pos_nc(n, vector<lli>(2, 0));
    for (int i = 0; i < n; i++) {
      vector<int> in(3);
      for (int j = 0; j < 3; j++) {
        in[j] = (str[j][n - i - 1] == '?') ? -1 : str[j][n - i - 1] - '0';
      }
      search(in, pos_nc[i], 0, (i == n - 1) ? 1 : 0);
      search(in, pos_c[i], 1, (i == n - 1) ? 1 : 0);
    }

    vector<vector<lli>> dp = pos_nc;
    for (int i = 0; i < n - 1; i++) {
      dp[i + 1][0] =
          (dp[i][0] * pos_nc[i + 1][0] + dp[i][1] * pos_c[i + 1][0]) % mod;
      dp[i + 1][1] =
          (dp[i][0] * pos_nc[i + 1][1] + dp[i][1] * pos_c[i + 1][1]) % mod;
    }
    cout << dp[n - 1][0] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 1e9 + 7;

void search(vector<int> &in, vector<lli> &out, int c, int st) {
  if (in[0] != -1 && in[1] != -1 && in[2] != -1) {
    if (in[0] + in[1] + c == in[2])
      out[0]++;
    if (in[0] + in[1] + c == in[2] + 10)
      out[1]++;
    return;
  }
  for (int i = 0; i < 3; i++) {
    if (in[i] == -1) {
      for (int d = st; d <= 9; d++) {
        in[i] = d;
        search(in, out, c, st);
        in[i] = -1;
      }
      return;
    }
  }
}

int main() {
  while (1) {
    string str[3];
    cin >> str[0];
    if (str[0] == "0")
      break;
    cin >> str[1] >> str[2];
    int n = str[0].length();

    vector<vector<lli>> pos_c(n, vector<lli>(2, 0));
    vector<vector<lli>> pos_nc(n, vector<lli>(2, 0));
    for (int i = 0; i < n; i++) {
      vector<int> in(3);
      for (int j = 0; j < 3; j++) {
        in[j] = (str[j][n - i - 1] == '?') ? -1 : str[j][n - i - 1] - '0';
      }
      search(in, pos_nc[i], 0, (i == n - 1) ? 1 : 0);
      search(in, pos_c[i], 1, (i == n - 1) ? 1 : 0);
    }

    lli ans = 0;
    for (int i = 0; i < (1 << (n - 1)); i++) {
      lli subans = 1;
      bool carry = false;
      for (int j = 0; j < n; j++) {
        int uc = ((i & (1 << j)) == 0) ? 0 : 1;
        lli possible = carry ? pos_c[j][uc] : pos_nc[j][uc];
        subans = (subans * possible) % mod;
        carry = ((i & (1 << j)) == 0) ? false : true;
      }
      ans = (ans + subans) % mod;
    }
    cout << ans << endl;
  }
  return 0;
}