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

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

int main() {
  int n;
  cin >> n;
  lp(z, n) {
    int a[5][5];
    lp(i, 5) {
      lp(j, 5) { cin >> a[j][i]; }
    }
    int check, ans = 0;
    lp(i, 5) {
      lp(j, 5) {
        lp(k, 5 - i) {
          lp(l, 5 - j) {
            check = 0;
            for (int m = k; m <= i + k; m++) {
              for (int n = l; n <= j + l; n++) {
                if (a[n][m] == 0) {
                  check = 1;
                  break;
                }
              }
              if (check == 1)
                break;
            }
            if (check == 0) {
              ans = max(ans, (i + 1) * (1 + j));
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  int N, i, j, k, l;
  string str;
  while (1) {
    int temp2[20] = {}, temp[20][20] = {}, aaa = -1, temp3[20] = {}, size;
    cin >> N;

    if (N == 0)
      break;
    j = 0;
    k = 0;
    for (i = 0; i < N; i++) {
      cin >> str;
      size = (int)str.size() - 1;

      if (str[size] == '*') {
        temp2[size] = 1;
        k = 0;
      } else if (str[size] == '+') {
        temp2[size] = 0;
        k = 0;
      } else {
        temp3[size]++;

        if (str.size() < aaa || i == N - 1) {

          if (temp2[size] == 1) {
            temp[size][temp3[size]] = 1;
            for (l = 0; l < temp3[aaa]; l++) {
              temp[size][temp3[size]] *= temp[aaa][l];
            }

          } else if (temp2[size] == 0) {
            temp[size][temp3[size]] = 0;
            for (l = 0; l < temp3[aaa]; l++) {
              temp[size][temp3[size]] += temp[aaa][l];
            }
          }
        }
      }
      temp[j][k] = str[size] - '0';
      aaa = size;
      for (i = 0; i < N; i++) {
        for (j = 0; j < 10; j++) {
          cout << temp[i][j] << " ";
        }
        cout << endl;
      }
    }

    for (i = 0; i < N; i++) {
      for (j = 0; j < 10; j++) {
        cout << temp[i][j] << " ";
      }
      cout << endl;
    }
    cout << temp[0][temp3[0] - 1] << endl;
  }
  return 0;
}
Problem p00695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#pragma warning(disable : 4996)
using namespace std;
int x, p[5][5];
int main() {
  scanf("%d", &x);
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = 0; k < 5; k++) {
        scanf("%d", &p[j][k]);
      }
    }
    int ret = 0;
    for (int j = 0; j < 5; j++) {
      for (int k = 0; k < 5; k++) {
        for (int l = j + 1; l <= 5; l++) {
          for (int m = k + 1; m <= 5; m++) {
            bool flag = true;
            for (int n = j; n < l; n++) {
              for (int o = k; o < m; o++) {
                flag = flag && p[n][o];
              }
            }
            if (flag) {
              ret = max(ret, (l - j) * (m - k));
            }
          }
        }
      }
    }
    printf("%d\\n", ret);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int x, p[5][5];
int main() {
  scanf("%d", &x);
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = 0; k, 5; k++) {
        scanf("%d", &p[j][k]);
      }
    }
    int ret = 0;
    for (int j = 0; j < 5; j++) {
      for (int k = 0; k < 5; k++) {
        for (int l = j + 1; l <= 5; l++) {
          for (int m = k + 1; m <= 5; m++) {
            bool flag = true;
            for (int n = j; n < l; n++) {
              for (int o = k; o < m; o++) {
                flag = flag && p[n][o];
              }
            }
            if (flag) {
              ret = max(ret, (l - j) * (m - k));
            }
          }
        }
      }
    }
    printf("%d\\n", ret);
  }
  return 0;
}
Problem p00697 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string s[9], t[3][3];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (!t[i][j].empty())
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || t[nx][ny].empty())
          continue;
        if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j] += s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j].clear();
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) cin >> s[j];
    printf("%d\\n", dfs(0));
  }
}

Error: TLE

Fixed submission:

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

string s[9], t[3][3];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (!t[i][j].empty())
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || t[nx][ny].empty())
          continue;
        char c = t[nx][ny][(l + 2) % 4];
        if (isupper(s[p][(l + k) % 4])) {
          if (isupper(c) || toupper(c) != s[p][(l + k) % 4])
            goto g;
        } else if (islower(c) || tolower(c) != s[p][(l + k) % 4])
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j] += s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j].clear();
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) cin >> s[j];
    printf("%d\\n", dfs(0));
  }
}
Problem p00697 (C++) — buggy submission, verdict: Time Limit Exceeded

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

char s[9][5], t[3][3][5];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (t[i][j][0])
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || !t[nx][ny][0])
          continue;
        if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j][l] = s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j][0] = 0;
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) scanf("%s", s[j]);
    printf("%d\\n", dfs(0));
  }
}

Error: TLE

Fixed submission:

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

char s[9][5], t[3][3][5];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (t[i][j][0])
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || !t[nx][ny][0])
          continue;
        if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j][l] = s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j][0] = 0;
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) scanf("%s", s[i]);
    printf("%d\\n", dfs(0));
  }
}
Problem p00697 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

char arr[5][5][4];
char puzzle[36][4];
bool used[36];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

bool check(char a, char b) {
  int d = -32;
  return (a + d == b || b + d == a || a == '0' || b == '0');
}

int rec(int y, int x) {
  if (y == 4 && x == 1) {
    return 1;
  }
  int res = 0;
  for (int i = 0; i < 36; i++) {
    if (used[i])
      continue;
    int cnt = 0;
    for (int j = 0; j < 4; j++) {
      arr[y][x][j] = puzzle[i][j];
    }
    for (int j = 0; j < 4; j++) {
      int nx = x + dx[j];
      int ny = y + dy[j];

      if (check(arr[ny][nx][(j + 2) % 4], arr[y][x][j])) {
        cnt++;
      }
    }
    if (cnt == 4) {
      int ii = i / 4 * 4;
      for (int j = ii; j < ii + 4; j++) {
        used[j] = true;
      }
      int a = 0, b = 0;
      if (x == 3)
        a++;
      if (x > 2)
        b++;
      res += rec(y + a, x + 1 - 3 * b);
      for (int j = ii; j < ii + 4; j++) {
        used[j] = false;
      }
    }
    for (int j = 0; j < 4; j++) {
      arr[y][x][j] = '0';
    }
  }
  return res;
}

int main() {
  int Tc;
  string str;

  cin >> Tc;
  while (Tc--) {
    memset(used, false, sizeof(used));
    fill(arr[0][0], arr[5][5], '0');
    for (int i = 0; i < 9; i++) {
      cin >> str;
      for (int j = 0; j < 4; j++) {
        puzzle[i * 4][j] = str[j];
      }
      for (int j = 1; j < 4; j++) {
        for (int k = j; k < j + 4; k++) {
          puzzle[i * 4 + j][k - j] = str[k % 4];
        }
      }
    }
    cout << rec(1, 1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(a, b, n) for (int a = b; a < n; a++)
const int MAX = 36;
const int F = 5;

int n, ans, t[F][F][F - 1];
bool used[MAX];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
string pz[MAX], str;

void init() {
  str.clear();
  fill(used, used + MAX, false);
  rep(i, F) rep(j, F) rep(k, F - 1) t[i][j][k] = '0';
}

bool judge(char a, char b) {
  int c = 'A' - 'a';
  if (a + c == b || b + c == a || a == '0' || b == '0')
    return true;
  return false;
}

void dfs(int y, int x) {
  if (y == 4 && x == 1) {
    ++ans;
    return;
  }
  rep(i, MAX) {
    if (used[i])
      continue;
    int cnt = 0;

    rep(j, F - 1) { t[y][x][j] = pz[i][j]; }
    rep(j, F - 1) {
      int nx = x + dx[j];
      int ny = y + dy[j];
      if (judge(t[ny][nx][(j + 2) % 4], t[y][x][j]))
        cnt++;
    }
    if (cnt == 4) {
      int ti = i / 4 * 4;
      used[ti] = used[ti + 1] = used[ti + 2] = used[ti + 3] = true;
      dfs(y + (x == 3), x + 1 - 3 * (x + 1 > 3));
      int fi = i / 4 * 4;
      used[fi] = used[fi + 1] = used[fi + 2] = used[fi + 3] = false;
    }
    rep(j, F - 1) { t[y][x][j] = '0'; }
  }
}

int main() {
  cin >> n;
  while (n--) {
    init();
    rep(i, MAX / 4) {
      cin >> str;
      rep(j, F - 1) pz[i * 4][j] = str[j];
      rep1(j, 1, F - 1) {
        rep1(k, j, j + F - 1) { pz[i * 4 + j][k - j] = str[k % 4]; }
      }
    }
    ans = 0;
    dfs(1, 1);
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef stack<int> st;
int n;
st A[301];
int idx[301];
int h[301];

// aテ」ツつ鍛テ」ツ?ョテ、ツクツ甘」ツ?ォテ」ツ?甘」ツ??
void sim(int a, int b) {
  int i = idx[a], j = b < 0 ? a : idx[b];
  if (b == -1 && i == a)
    return;
  if (i == j && h[a] > h[b])
    return;

  while (A[i].top() != a) {
    int t = A[i].top();
    idx[t] = t;
    A[t].push(t);
    h[t] = 1;
    A[i].pop();
  }
  j = b < 0 ? a : idx[b];
  A[i].pop();
  A[j].push(a);
  idx[a] = j;
  h[a] = A[j].size();
}

signed main() {
  while (cin >> n, n) {
    for (int i = 0; i < 301; i++)
      while (!A[i].empty())
        A[i].pop();
    for (int i = 0; i < n; i++)
      A[i].push(i), idx[i] = i, h[i] = 1;

    while (1) {
      int i, j;
      cin >> i >> j;
      if (i == 0 && j == 0)
        break;
      if (i == j)
        continue;
      sim(i - 1, j - 1);
    }

    vector<int> ans;
    for (int i = 0; i < n; i++)
      if (!A[i].empty())
        ans.push_back(A[i].size());
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++)
      cout << ans[i] << endl;
    cout << "end" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef stack<int> st;
int n;
vector<st> A;
int idx[301];
int h[301];

// aテ」ツつ鍛テ」ツ?ョテ、ツクツ甘」ツ?ォテ」ツ?甘」ツ??
void sim(int a, int b) {
  int i = idx[a], j = b < 0 ? b : idx[b];
  if (b == -1 && i == a)
    return;
  if (i == j && h[a] > h[b])
    return;

  while (A[i].top() != a) {
    int t = A[i].top();
    idx[t] = t;
    A[t].push(t);
    h[t] = 1;
    A[i].pop();
  }

  A[i].pop();
  A[j].push(a);
  idx[a] = j;
  h[a] = A[j].size();
}

signed main() {
  while (cin >> n, n) {
    A.resize(n);
    for (int i = 0; i < n; i++)
      while (!A[i].empty())
        A[i].pop();
    for (int i = 0; i < n; i++)
      A[i].push(i), idx[i] = i, h[i] = 1;

    while (1) {
      int i, j;
      cin >> i >> j;
      if (i == 0 && j == 0)
        break;
      if (i == j)
        continue;
      if (i == 0)
        continue;
      sim(i - 1, j - 1);
    }

    vector<int> ans;
    for (int i = 0; i < n; i++)
      if (!A[i].empty())
        ans.push_back(A[i].size());
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++)
      cout << ans[i] << endl;
    cout << "end" << endl;
  }
  return 0;
}
Problem p00701 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct pile {
  int p, o;
  pile() {}
  pile(int v) {
    p = v;
    o = -1;
  }
};
pile p[300];
int h(int v) {
  if (p[v].o == -1)
    return 1;
  return h(p[v].o) + 1;
}
void tf(int v) {
  if (p[v].p != v) {
    int i;
    for (i = p[v].p; ~i && p[i].o != v; i = p[i].o)
      ;
    p[i].o = -1;
  }
  p[v].p = v;
  if (p[v].o != -1)
    tf(p[v].o);
  p[v].o = -1;
}
void ta(const int a, const int b) {
  // cout<<a<<"-"<<b<<endl;
  // cout<<p[a].o<<"+"<<p[b].o<<endl;
  assert(a != b);
  if (p[b].o == -1) {
    p[a].p = p[b].p;
    p[b].o = a;
    return;
  }
  ta(a, p[b].o);
}
signed main() {
  int m;
  while (cin >> m, m) {
    for (int i = 0; i < 300; i++)
      p[i] = pile(i);
    int a, b;
    while (cin >> a >> b, a || b) {
      // cout<<a<<":"<<b<<endl;
      a--;
      b--;
      if (a == b)
        continue;
      if (b < 0) {
        if (p[a].p != a)
          tf(a);
      } else {
        if (p[a].p == p[b].p) {
          if (h(a) < h(b))
            continue;
          tf(a);
          // cout<<a<<" "<<b<<":"<<p[a].p<<" "<<p[b].p<<endl;
          ta(a, b);
        } else {
          tf(a);
          // cout<<a<<" "<<b<<":"<<p[a].p<<" "<<p[b].p<<endl;
          ta(a, b);
        }
      }
    }
    // puts("OK");
    vector<int> v;
    for (int i = 0; i < m; i++) {
      if (p[i].p == i) {
        // cout<<i<<endl;
        // cout<<h(p[i])<<endl;
        v.push_back(h(i));
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++)
      cout << v[i] << endl;
    cout << "end" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct pile {
  int p, o;
  pile() {}
  pile(int v) {
    p = v;
    o = -1;
  }
};
pile p[300];
int h(int v) {
  if (p[v].o == -1)
    return 1;
  return h(p[v].o) + 1;
}
void tf(int v) {
  p[v].p = v;
  if (p[v].o != -1)
    tf(p[v].o);
  p[v].o = -1;
}
void ta(int a, int b) {
  if (p[b].o == -1) {
    p[a].p = p[b].p;
    p[b].o = a;
    return;
  }
  ta(a, p[b].o);
}
signed main() {
  int m;
  while (cin >> m, m) {
    for (int i = 0; i < m; i++)
      p[i] = pile(i);
    int a, b;
    while (cin >> a >> b, a || b) {
      // cout<<a<<":"<<b<<endl;
      a--;
      b--;
      if (a == b)
        continue;
      if (b < 0) {
        if (p[a].p != a)
          tf(a);
      } else {
        if (p[a].p == p[b].p) {
          if (h(a) < h(b))
            continue;
          tf(a);
          ta(a, b);
        } else {
          tf(a);
          ta(a, b);
        }
      }
    }
    // puts("OK");
    vector<int> v;
    for (int i = 0; i < m; i++) {
      if (p[i].p == i) {
        // cout<<i<<endl;
        // cout<<h(p[i])<<endl;
        v.push_back(h(i));
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++)
      cout << v[i] << endl;
    cout << "end" << endl;
  }
  return 0;
}
Problem p00701 (C++) — buggy submission, verdict: Runtime Error

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

const int MAXN = 105;

int N;
int top[MAXN], bottom[MAXN];

int findTop(int x, vector<int> *v = NULL) {
  if (v != NULL)
    v->push_back(x);
  return top[x] == x ? x : findTop(top[x], v);
}

int getH(int x) { return bottom[x] == 0 ? 1 : getH(bottom[x]) + 1; }

void dispose(int x) {
  vector<int> v;
  findTop(x, &v);
  int y = bottom[x];
  if (y != 0)
    top[y] = y;
  for (int i = 0; i < v.size(); ++i) {
    top[v[i]] = v[i];
    bottom[v[i]] = 0;
  }
}

void move(int i, int j) {
  if (j == 0) {
    if (bottom[i] == 0)
      return;
    dispose(i);
  } else {
    if (findTop(i) == findTop(j) && getH(i) >= getH(j))
      return;
    dispose(i);
    j = findTop(j);
    bottom[i] = j;
    top[j] = i;
  }
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i <= N; ++i) {
      top[i] = i;
      bottom[i] = 0;
    }
    {
      int I, J;
      while (cin >> I >> J && (I | J)) {
        move(I, J);
      }
    }
    vector<int> h;
    int sum = 0;
    for (int i = 1; i <= N; ++i) {
      if (top[i] == i) {
        h.push_back(getH(i));
        sum += h.back();
      }
    }
    assert(sum == N);
    sort(h.begin(), h.end());
    for (int i = 0; i < h.size(); ++i) {
      cout << h[i] << endl;
    }
    cout << "end" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int MAXN = 105;

int N;
int top[MAXN], bottom[MAXN];

int findTop(int x, vector<int> *v = NULL) {
  if (v != NULL)
    v->push_back(x);
  return top[x] == x ? x : findTop(top[x], v);
}

int getH(int x) { return bottom[x] == 0 ? 1 : getH(bottom[x]) + 1; }

void dispose(int x) {
  vector<int> v;
  findTop(x, &v);
  int y = bottom[x];
  if (y != 0)
    top[y] = y;
  for (int i = 0; i < v.size(); ++i) {
    top[v[i]] = v[i];
    bottom[v[i]] = 0;
  }
}

void move(int i, int j) {
  if (j == 0) {
    if (bottom[i] == 0)
      return;
    dispose(i);
  } else {
    if (findTop(i) == findTop(j) && getH(i) >= getH(j))
      return;
    if (top[i] != i)
      dispose(top[i]);
    j = findTop(j);
    bottom[i] = j;
    top[j] = i;
  }
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i <= N; ++i) {
      top[i] = i;
      bottom[i] = 0;
    }
    {
      int I, J;
      while (cin >> I >> J && (I | J)) {
        move(I, J);
      }
    }
    vector<int> h;
    int sum = 0;
    for (int i = 1; i <= N; ++i) {
      if (top[i] == i) {
        h.push_back(getH(i));
        sum += h.back();
      }
    }
    assert(sum == N);
    sort(h.begin(), h.end());
    for (int i = 0; i < h.size(); ++i) {
      cout << h[i] << endl;
    }
    cout << "end" << endl;
  }
  return 0;
}
Problem p00702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int tmp;
  cin >> tmp;

  string kan[] = {"ld", "mb", "mp", "nc", "nd", "ng",
                  "nt", "nw", "ps", "qu", "cw", "ts"};
  map<string, int> index;
  for (int i = 0; i < 12; ++i)
    index.insert(make_pair(kan[i], 26 + i));

  vector<vector<int>> num(38, vector<int>(38, 0));
  for (;;) {
    string s;
    if (!(cin >> s))
      break;
    int n = s.size();

    vector<int> j;
    for (int i = 0; i < n; ++i) {
      if (i < n - 1 && index.find(s.substr(i, 2)) != index.end()) {
        j.push_back(index[s.substr(i, 2)]);
        ++i;
      } else {
        j.push_back(s[i] - 'a');
      }
    }

    for (unsigned i = 0; i < j.size() - 1; ++i)
      ++num[j[i]][j[i + 1]];
  }

  for (int i = 0; i < 38; ++i) {
    int k = 0;
    for (int j = 1; j < 38; ++j) {
      if (num[i][j] > num[i][k])
        k = j;
    }
    if (i < 26)
      cout << (char)('a' + i) << ' ';
    else
      cout << kan[i - 26] << ' ';
    if (k < 26)
      cout << (char)('a' + k) << ' ';
    else
      cout << kan[k - 26] << ' ';
    cout << num[i][k] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  cin.ignore();

  string kan[] = {"ld", "mb", "mp", "nc", "nd", "ng",
                  "nt", "nw", "ps", "qu", "cw", "ts"};
  map<string, int> index;
  for (int i = 0; i < 12; ++i)
    index.insert(make_pair(kan[i], 26 + i));

  vector<vector<int>> num(38, vector<int>(38, 0));
  for (;;) {
    string s;
    if (!(cin >> s))
      break;
    int n = s.size();

    vector<int> j;
    for (int i = 0; i < n; ++i) {
      if (i < n - 1 && index.find(s.substr(i, 2)) != index.end()) {
        j.push_back(index[s.substr(i, 2)]);
        ++i;
      } else {
        j.push_back(s[i] - 'a');
      }
    }

    for (unsigned i = 0; i < j.size() - 1; ++i)
      ++num[j[i]][j[i + 1]];
  }

  for (int i = 0; i < 38; ++i) {
    int k = 0;
    for (int j = 1; j < 38; ++j) {
      if (num[i][j] > num[i][k])
        k = j;
    }
    if (i < 26)
      cout << (char)('a' + i) << ' ';
    else
      cout << kan[i - 26] << ' ';
    if (k < 26)
      cout << (char)('a' + k) << ' ';
    else
      cout << kan[k - 26] << ' ';
    cout << num[i][k] << endl;
  }

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

// 01
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int l, h;
vector<string> tr;
vector<int> hi, bl;
char t[11];
string a;
int ac;

void dfs(int d, int u) {
  if (d != l) {
    for (int i = 0; i < 10; i++) {
      if (!(u >> i & 1)) {
        t[d] = '0' + i;
        for (int i = 0; i < tr.size(); i++) {
          int ch = 0, cb = 0;
          for (int j = 0; j <= d; j++) {
            if (tr[i].find(t[j]) != string::npos) {
              ((t[j] == tr[i][j]) ? ch : cb)++;
            }
          }
          if (ch + l - d - 1 < hi[i] || ch > hi[i] || cb + l - d - 1 < bl[i] ||
              cb > bl[i])
            goto next;
        }
        dfs(d + 1, u | 1 << i);
      next:;
      }
    }
  } else {
    t[d] = 0;
    ac++;
    a = t;
  }
}

int main() {
  while (cin >> l >> h, l | h) {
    tr.resize(h);
    hi.resize(h);
    bl.resize(h);
    while (h--) {
      cin >> tr[h] >> hi[h] >> bl[h];
    }
    ac = 0;
    dfs(0, 0);
    if (ac == 1) {
      cout << a << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

// 01
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int l, h;
vector<string> tr;
vector<int> hi, bl;
char t[11];
string a;
int ac;

void dfs(int d, int u) {
  if (d != l) {
    for (int i = 0; i < 10; i++) {
      if (!(u >> i & 1)) {
        t[d] = '0' + i;
        dfs(d + 1, u | 1 << i);
      }
    }
  } else {
    t[d] = 0;
    for (int i = 0; i < tr.size(); i++) {
      int ch = 0, cb = 0;
      for (int j = 0; j < l; j++) {
        if (tr[i].find(t[j]) != string::npos) {
          ((t[j] == tr[i][j]) ? ch : cb)++;
        }
      }
      if (ch != hi[i] || cb != bl[i])
        return;
    }
    ac++;
    a = t;
  }
}

int main() {
  while (cin >> l >> h, l | h) {
    tr.resize(h);
    hi.resize(h);
    bl.resize(h);
    while (h--) {
      cin >> tr[h] >> hi[h] >> bl[h];
    }
    ac = 0;
    dfs(0, 0);
    if (ac == 1) {
      cout << a << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}