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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i, j) for (int__ i = 0; i < (int__)(j); i++)
#define repeat(i, j, k) for (int__ i = (j); i < (int__)(k); i++)
#define all(v) v.begin(), v.end()

const int alpha_size = 'z' - 'a' + 1;

void visit(int now, vector<bool> &visited, const vector<set<int>> &E) {
  visited[now] = true;
  for (int nxt : E[now]) {
    if (not visited[nxt]) {
      visit(nxt, visited, E);
    }
  }
}

bool solve() {
  int N;
  cin >> N;
  if (N == 0)
    return false;
  vector<int> d(alpha_size);
  vector<set<int>> E(alpha_size);
  rep(i, N) {
    string s;
    cin >> s;
    int f = s.front() - 'a';
    int b = s.back() - 'a';
    ++d[f];
    --d[b];
    E[f].insert(b);
  }
  vector<bool> visited(alpha_size);
  rep(i, E.size()) {
    if (E[i].size() > 0) {
      visit(i, visited, E);
      break;
    }
  }
  bool flg = true;
  rep(i, alpha_size) {
    if (d[i] != 0 or (E[i].size() > 0 and not visited[i]))
      flg = false;
  }
  cout << (flg ? "OK" : "NG") << endl;

  return true;
}

int main() {
  ios::sync_with_stdio(false);
  while (solve())
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i, j) for (int__ i = 0; i < (int__)(j); i++)
#define repeat(i, j, k) for (int__ i = (j); i < (int__)(k); i++)
#define all(v) v.begin(), v.end()

bool dfs(int now, const vector<string> &S, vector<bool> &visited, int depth) {
  visited[now] = true;
  if (depth == S.size() - 1)
    return true;
  bool flg = false;
  rep(i, S.size()) {
    if (not visited[i] and S[now].back() == S[i].front()) {
      flg |= dfs(i, S, visited, depth + 1);
      if (flg)
        break;
    }
  }
  visited[now] = false;
  return flg;
}

bool solve() {
  int N;
  cin >> N;
  if (N == 0)
    return false;

  vector<string> S(N);
  rep(i, N) cin >> S[i];
  vector<bool> visited(S.size());
  bool flg = false;
  rep(i, S.size()) {
    flg |= dfs(i, S, visited, 0);
    if (flg)
      break;
  }

  cout << (flg ? "OK" : "NG") << endl;

  return true;
}

int main() {
  ios::sync_with_stdio(false);
  while (solve())
    ;
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

/* 01 - 19*/

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int n;
int data[26][26], fi[26], la[26];
string st;

bool flag;

void bt(char next, int depth) {
  if (depth == n - 1) {
    if (next == st[0])
      flag = true;
  }
  if (flag)
    return;

  for (int i = 0; i < 26; i++) {
    if (data[next - 'a'][i]) {
      data[next - 'a'][i]--;
      bt('a' + i, depth + 1);
      data[next - 'a'][i]++;
    }
  }
}
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < 26; i++) {
      fi[i] = la[i] = 0;
      for (int j = 0; j < 26; j++)
        data[i][j] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> st;
      data[st[0] - 'a'][st[st.length() - 1] - 'a']++;
      fi[st[0] - 'a']++;
      la[st[st.length() - 1] - 'a']++;
    }

    data[st[0] - 'a'][st[st.length() - 1] - 'a']--;
    flag = false;
    bool notSearch = false;

    for (int i = 0; i < 26; i++) {
      if (fi[i] != la[i])
        notSearch = true;
    }

    if (!notSearch)
      bt(st[st.length() - 1], 0);
    cout << (flag ? "OK" : "NG") << endl;
  }
}

Error: TLE

Fixed submission:

/* 01 - 19*/

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int n;
map<string, int> data;
string start;
bool flag;

void bt(string s, int depth) {
  if (depth == n - 1) {
    if (s[1] == start[0])
      flag = true;
  }
  if (flag)
    return;
  for (map<string, int>::iterator it = data.begin(); it != data.end(); ++it) {
    if (s[1] == (*it).first[0] && (*it).second) {
      (*it).second--;
      bt((*it).first, depth + 1);
      (*it).second++;
    }
  }
}
int main() {
  string s;
  while (cin >> n, n) {
    data.clear();
    for (int i = 0; i < n; i++) {
      cin >> s;
      s = s.substr(0, 1) + s.substr(s.length() - 1, 1);
      data[s]++;
    }
    start = s; // ツ催古」ツづ可禿シツ療債つウツづェツつスs
    data[start]--;
    flag = false;
    bt(start, 0);
    cout << (flag ? "OK" : "NG") << endl;
  }
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int parent;

int wd[26][26];
bool done[26];

int st[26] = {0}, en[26] = {0};

int dfs(int pos) {
  done[pos] = true;
  rep(v, 26) if (!done[v] && wd[pos][v]) dfs(v);
}
bool wrap() {
  int nc = 0;
  bool f = false;
  rep(i, 26) done[i] = 0;
  rep(i, 26) {
    if (!st[i] && !en[i])
      continue;
    if (!done[i]) {
      if (f)
        return false;
      else
        f = true;
      dfs(i);
    }
  }
  return true;
}

int main() {
  int n;

  while (cin >> n, n) {
    rep(i, 26) rep(j, 26) wd[i][j] = 0;
    rep(i, 26) st[i] = en[i] = 0;
    bool f = true;
    rep(i, n) {
      string in;
      cin >> in;
      st[in[0] - 'a']++;
      en[in[in.size() - 1] - 'a']++;
      wd[in[0] - 'a'][in[in.size() - 1] - 'a'] = 1;
    }
    rep(i, 26) f &= st[i] == en[i];

    cout << (f && wrap() ? "OK" : "NG") << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int parent;

int main() {
  int n;

  while (cin >> n, n) {
    int st[26] = {0}, en[26] = {0};
    vector<string> wd;
    bool f = true;
    rep(i, n) {
      string in;
      cin >> in;
      wd.push_back(in);
      st[in[0] - 'a']++;
      en[in[in.size() - 1] - 'a']++;
    }
    rep(i, 26) f &= st[i] == en[i];

    vector<vector<bool>> wf(n, vector<bool>(n));
    rep(i, n) {
      rep(j, n) { wf[i][j] = wd[i][wd[i].size() - 1] == wd[j][0]; }
    }
    rep(k, n) rep(i, n) rep(j, n) {
      wf[i][j] = wf[i][j] | (wf[i][k] & wf[k][j]);
    }
    rep(i, n) {
      rep(j, n) { f &= wf[i][j]; }
    }
    cout << (f ? "OK" : "NG") << endl;
  }
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int data[26][26];
bool visited[26];
bool exist[26];

void rec(int now) {
  visited[now] = true;
  for (int i = 0; i < 26; i++) {
    if (exist[i] && !visited[i] && data[now][i] != 0) {
      rec(i);
    }
  }
}

main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    fill(data[0], data[0] + 26 * 26, 0);
    fill(exist, exist + 26, false);
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      data[str[0] - 'a'][str[str.length() - 1] - 'a'] += 1;
      exist[str[0] - 'a'] = exist[str[str.length() - 1] - 'a'] = true;
    }

    int num[26];
    fill(num, num + 26, 0);

    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        if (data[i][j] >= 0) {
          num[i] += data[i][j];
          num[j] -= data[i][j];
        }
      }
    }

    bool flag = true;
    for (int i = 0; i < 26; i++) {
      if (num[i] != 0) {
        flag = false;
        break;
      }
    }

    if (flag == true) {
      fill(visited, visited + 26, false);
      int cnt = 0;
      for (int i = 0; i < 26; i++) {
        if (exist[i] && !visited[i]) {
          rec(i);
          cnt++;
        }
      }
      if (cnt != 1)
        flag = false;
    }

    if (flag)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int n;
string start;
vector<string> V[26];

bool rec(set<string> visited, char bfrC, int cnt) {
  if (cnt == n) {
    if (start[0] == bfrC) {
      return true;
    }
  }
  for (vector<string>::iterator it = V[bfrC - 'a'].begin();
       it != V[bfrC - 'a'].end(); it++) {
    string s = *it;
    if (visited.find(s) != visited.end())
      continue;
    set<string> v = visited;
    v.insert(s);
    if (rec(v, s[s.length() - 1], cnt + 1)) {
      return true;
    }
  }
  return false;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    string s;
    for (int i = 0; i < n; i++) {
      cin >> s;
      V[s[0] - 'a'].push_back(s);
    }
    start = s;
    set<string> visited;
    visited.insert(s);
    if (rec(visited, s[s.length() - 1], 1)) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int path[26][26];
int visited[26];

int dfs(int src, int *visited) {
  visited[src] = 1;
  int res = 1;
  for (int i = 0; i < 26; i++) {
    if (path[src][i] && visited[i] == 0) {
      res += dfs(i, visited);
    }
  }
  return res;
}

int main() {
  int n;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    memset(path, 0, sizeof(path));
    memset(visited, 0, sizeof(visited));
    set<int> src;
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      path[*str.begin() - 'a'][*(str.end() - 1) - 'a']++;

      src.insert(*(str.end() - 1) - 'a');
      src.insert(*str.begin() - 'a');
    }

    if (src.size() != dfs(*src.begin(), visited)) {
      printf("NG\\n");
      continue;
    }

    bool isok = true;

    for (set<int>::iterator it = src.begin(); it != src.end(); it++) {
      int out = 0;
      for (int j = 0; j < 26; j++) {
        out += path[*it][j];
      }

      int in = 0;
      for (int j = 0; j < 26; j++) {
        in += path[j][*it];
      }

      if (out != in)
        isok = false;
    }

    printf("%s\\n", isok ? "OK" : "NG");
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int main() {
  while (true) {
    int n;
    vector<string> v;
    cin >> n;
    if (n == 0)
      break;
    REP(i, n) {
      string s;
      cin >> s;
      v.PB(s);
    }
    bool flg = false;
    REP(i, 26) {
      int hc = 0;
      int tc = 0;
      char a = (char)('a' + i);
      REP(j, v.size()) {
        string s = v[j];
        if (s[0] == a)
          hc++;
        if (s[s.size() - 1] == a)
          tc++;
      }
      if (hc != tc)
        flg = true;
    }
    char head = v[0][0];
    char tail = v[0][v[0].size() - 1];
    v.erase(v.begin());
    if (!flg) {
      while (v.size() > 1) {
        REP(i, v.size()) {
          if (tail == v[i][0]) {
            tail = v[i][v[i].size() - 1];
            v.erase(v.begin() + i);
            break;
          }
          if (i == v.size() - 1) {
            flg = true;
            break;
          }
        }
      }
    }
    if (flg)
      cout << "NG" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool graph[26][26];
bool visit[26];
int in[26], out[26];

void dfs(int s) {
  if (visit[s])
    return;
  visit[s] = true;
  for (int i = 0; i < 26; i++) {
    if (graph[s][i]) {
      dfs(i);
    }
  }
}

bool connect(int s) {
  dfs(s);
  for (int i = 0; i < 26; i++) {
    if (!visit[i])
      return false;
  }
  return true;
}

bool cycle() {
  for (int i = 0; i < 26; i++) {
    if (in[i] != out[i])
      return false;
  }
  return true;
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(graph, 0, sizeof(graph));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    memset(visit, 1, sizeof(visit));
    string s;
    for (int i = 0; i < n; i++) {
      cin >> s;
      graph[s[0] - 'a'][s[s.size() - 1] - 'a'] = 1;
      in[s[s.size() - 1] - 'a']++;
      out[s[0] - 'a']++;
      visit[s[0] - 'a'] = visit[s[s.size() - 1] - 'a'] = false;
    }

    if (connect(s[s.size() - 1] - 'a') && cycle())
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define repeat(n, i) for (int i = 0; i < (n); i++)
#define foreach(c, b)                                                          \\
  for (__typeof((c).begin()) b = (c).begin(); (b) != (c).end(); (b)++)

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    list<string> words;
    repeat(n, i) {
      string s;
      cin >> s;
      words.push_back(s);
    }

    list<string>::iterator wi = words.begin();
    string first = *wi;
    bool found_next = true;
    while (words.size() > 1 && found_next) {
      found_next = false;
      char last = wi->at((wi->size()) - 1);
      words.erase(wi);
      foreach (words, _wi) {
        if ((*_wi)[0] == last) {
          wi = _wi;
          found_next = true;
        }
      }
    }
    if (words.size() == 1 &&
        words.begin()->at((words.begin()->size()) - 1) == first[0])
      cout << "OK" << endl;
    //        if (words.empty()) cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
using namespace std;
const int MAX = 26;
int G[MAX][MAX], n, visit[MAX];
string str;
bool flg;

void dfs(int pos) {
  visit[pos] = 0;
  for (int i = 0; i < 26; i++) {
    if (G[pos][i] && visit[i]) {
      G[pos][i]--;
      dfs(i);
    }
  }
}

int main() {
  while (cin >> n, n) {
    map<char, int> in, out;
    fill(G[0], G[MAX], 0);
    fill(visit, visit + MAX, 0);
    for (int i = 0; i < n; i++) {
      cin >> str;
      in[str[0]]++;
      out[str[str.size() - 1]]++;
      visit[str[0] - 'a'] = visit[str[str.size() - 1] - 'a'] = 1;
      G[str[0] - 'a'][str[str.size() - 1] - 'a']++;
    }
    flg = false;
    for (char j = 'a'; j <= 'z'; j++) {
      if (in[j] != out[j])
        flg = true;
    }
    if (!flg) {
      dfs(str[str.size() - 1] - 'a');
      for (int i = 0; i < 26; i++) {
        if (visit[i])
          flg = true;
      }
      if (flg)
        cout << "NG" << endl;
      else
        cout << "OK" << endl;
    } else
      cout << "NG" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
const int MAX = 26;
int G[MAX][MAX], n;
string str;
bool flg;

void dfs(int pos, char ch) {
  if (pos == n - 1 && ch != str[0]) {
    flg = true;
    return;
  }
  if (pos > n - 1)
    return;
  for (int i = 0; i < 26; i++) {
    if (G[ch - 'a'][i]) {
      G[ch - 'a'][i]--;
      dfs(++pos, i + 'a');
    }
  }
}

int main() {
  while (cin >> n, n) {
    map<char, int> in, out;
    fill(G[0], G[MAX], 0);
    for (int i = 0; i < n; i++) {
      cin >> str;
      in[str[0]]++;
      out[str[str.size() - 1]]++;
      if (i != n - 1)
        G[str[0] - 'a'][str[str.size() - 1] - 'a']++;
    }
    flg = false;
    for (char j = 'a'; j <= 'z'; j++) {
      if (in[j] != out[j])
        flg = true;
    }
    if (!flg) {
      dfs(0, str[n - 1]);
      if (flg)
        cout << "NG" << endl;
      else
        cout << "OK" << endl;
    } else
      cout << "NG" << endl;
  }

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

#include <iostream>
using namespace std;

bool visit[26], g[26][26];

bool dfs(int v) {
  visit[v] = true;
  bool f = false;
  for (int i = 0; i < 26; i++)
    if (g[v][i]) {
      f = true;
      if (!visit[i])
        dfs(i);
    }
  return f;
}

int graph_num(void) {
  int res = 0;
  for (int i = 0; i < 26; i++)
    if (!visit[i] && dfs(i))
      res++;
  return res;
}

int main() {
  int i, n, pre[26], suf[26], h, t;
  string s;

  while (cin >> n && n) {
    for (i = 0; i < 26; i++) {
      visit[i] = false;
      pre[i] = suf[i] = 0;
      for (int j = 0; j < 26; j++)
        g[i][j] = false;
    }

    for (i = 0; i < n; i++) {
      cin >> s;
      h = s[0] - 'a', t = s[(int)s.size() - 1] - 'a';

      pre[h]++;
      suf[t]++;
      g[h][t] = g[t][h] = true;
    }

    if (graph_num() != 1)
      printf("NG\\n");
    else {
      for (i = 0; i < 26; i++)
        if (pre[i] != suf[i])
          break;

      if (i == 26)
        printf("OK\\n");
      else
        printf("NG\\n");
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int i, n, pre[26], suf[26];
  bool same[26];
  string s;

  while (cin >> n && n) {
    for (i = 0; i < 26; i++) {
      same[i] = false;
      pre[i] = suf[i] = 0;
    }

    for (i = 0; i < n; i++) {
      cin >> s;
      if (s.empty())
        continue;
      int h = s[0] - 'a', t = s[(int)s.size() - 1] - 'a';
      if (h == t)
        same[i] = true;
      else {
        pre[h]++;
        suf[t]++;
      }
    }

    for (i = 0; i < 26; i++) {
      if (same[i] && (!pre[i] || !suf[i]))
        break;
      if (pre[i] != suf[i])
        break;
    }

    if (i == 26)
      printf("OK\\n");
    else
      printf("NG\\n");
  }
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int f[10000], a[26][10000], y[10000], an[26], bn[26], n, z;

void ans(int d) {
  int i;
  z++;
  for (i = 0; i < an[y[d]]; i++) {
    if (f[a[y[d]][i]] == 0) {
      f[a[y[d]][i]] = 1;
      ans(a[y[d]][i]);
    }
  }
}

int main() {
  int i, j, sl;
  char c[33];
  while (cin >> n && n > 0) {
    for (i = 0; i < 10000; i++) {
      f[i] = 0;
      for (j = 0; j < 26; j++) {
        an[j] = bn[j] = 0;
        a[j][i] = 0;
      }
    }
    for (i = 0; i < n; i++) {
      cin >> c;
      sl = strlen(c);
      a[c[0] - 'a'][an[c[0] - 'a']] = i;
      an[c[0] - 'a']++;
      bn[c[sl - 1] - 'a']++;
      y[i] = c[sl - 1] - 'a';
    }
    for (i = 0; i < 26; i++)
      if (an[i] != bn[i])
        break;
    if (i < 26) {
      cout << "NG" << endl;
      continue;
    }
    z = 0;
    ans(0);
    if (z == n + 1)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int f[10000], a[26][10000], y[10000], an[26], n;

int ans(int d, int k) {
  int i, cf = 0;
  if (k == n)
    return 1;
  for (i = 0; i < an[y[d]]; i++) {
    if ((cf & (1 << y[a[y[d]][i]])) > 0)
      continue;
    cf = cf | (1 << (y[a[y[d]][i]]));
    if (f[a[y[d]][i]] == 0) {
      f[a[y[d]][i]] = 1;
      if (ans(a[y[d]][i], k + 1) == 1)
        return 1;
      f[a[y[d]][i]] = 0;
    }
  }
  return 0;
}

int main() {
  int i, j, sl;
  char c[33];
  while (cin >> n && n > 0) {
    for (i = 0; i < 10000; i++) {
      f[i] = 0;
      for (j = 0; j < 26; j++) {
        an[j] = 0;
        a[j][i] = 0;
      }
    }
    for (i = 0; i < n; i++) {
      cin >> c;
      sl = strlen(c);
      a[c[0] - 'a'][an[c[0] - 'a']] = i;
      an[c[0] - 'a']++;
      y[i] = c[sl - 1] - 'a';
    }
    if (ans(0, 0) == 1)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int n, flag;
string S, U;
int p[26][26], q[26], r[26];
// tanjyun na zentansaku to bakkutorakku!
void backtruck(int c, int a) {
  if (a == n - 1) {
    if (c == (U[0] - 'a')) {
      flag = 1;
    }
  }
  if (flag) {
    return;
  }
  for (int i = 0; i < 26; i++) {
    if (p[c][i] >= 1) {
      p[c][i]--;
      backtruck(i, a + 1);
      p[c][i]++;
    }
  }
}
int main() {
  while (true) {
    for (int i = 0; i < 26; i++) {
      q[i] = 0;
      r[i] = 0;
      for (int j = 0; j < 26; j++)
        p[i][j] = 0;
    }
    cin >> n;
    flag = 0;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> S;
      S = S.substr(0, 1) + S.substr(S.size() - 1, 1);
      p[S[0] - 'a'][S[1] - 'a']++;
      q[S[0] - 'a']++;
      r[S[1] - 'a']++;
    }
    for (int i = 0; i < 26; i++) {
      if (q[i] != r[i])
        goto E;
    }
    U = S;
    p[S[0] - 'a'][S[1] - 'a']--;
    backtruck(U[1] - 'a', 0);
    if (flag == true) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
    goto F;
  E:;
    cout << "NG" << endl;
  F:;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int n, flag;
string S, U;
int p[26][26];
// tanjyun na zentansaku to bakkutorakku!
void backtruck(int c, int a) {
  if (a == n - 1) {
    if (c == (U[0] - 'a')) {
      flag = 1;
    }
  }
  if (flag) {
    return;
  }
  for (int i = 0; i < 26; i++) {
    if (p[c][i] >= 1) {
      p[c][i]--;
      backtruck(i, a + 1);
      p[c][i]++;
    }
  }
}
int main() {
  while (true) {
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++)
        p[i][j] = 0;
    }
    cin >> n;
    flag = 0;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> S;
      S = S.substr(0, 1) + S.substr(S.size() - 1, 1);
      p[S[0] - 'a'][S[1] - 'a']++;
    }
    U = S;
    p[S[0] - 'a'][S[1] - 'a']--;
    backtruck(U[1] - 'a', 0);
    if (flag == true) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}
Problem p00226 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <string.h>
#pragma warning(disable : 4996)
using namespace std;
int a, b, hit, blow;
const int power[4] = {1, 10, 100, 1000};
int main() {
  while (true) {
    scanf("%d %d", &a, &b);
    if (a == 0 && b == 0)
      break;
    hit = 0;
    blow = 0;
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        if ((a / power[i]) % 10 == (b / power[j]) % 10) {
          if (i == j) {
            hit++;
          } else {
            blow++;
          }
        }
      }
    }
    printf("%d %d\\n", hit, blow);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
#pragma warning(disable : 4996)
using namespace std;
char a[4], b[4];
int hit, blow;
int main() {
  while (true) {
    scanf("%s %s", a, b);
    if (strlen(a) == 0 && strlen(b) == 0)
      break;
    hit = 0;
    blow = 0;
    for (int i = 0; i < 4; i++) {
      if (a[i] == b[i])
        hit++;
      for (int j = 0; j < 4; j++) {
        if (a[i] == b[j]) {
          blow++;
          hit--;
        }
      }
    }
    printf("%d %d\\n", hit, blow);
  }
  return 0;
}