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

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 555;
int n;
char str[MAXN][22];
int vis[255][255];
int in[255];
queue<int> q;

bool topsort() {
  while (!q.empty())
    q.pop();
  for (int i = 0; i < 255; i++)
    if (in[i] == 0)
      q.push(i);
  int num = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    num++;
    for (int i = 0; i < 255; i++)
      if (vis[u][i] == 1) {
        in[i]--;
        if (in[i] == 0)
          q.push(i);
      }
  }
  return num == 255;
}

bool check() {
  memset(vis, 0, sizeof vis);
  memset(in, 0, sizeof in);
  for (int i = 2; i <= n; i++) {
    for (int j = 0; str[i][j] || str[i - 1][j]; j++) {
      if (str[i][j] == str[i - 1][j])
        continue;
      if (!str[i][j])
        return false;
      int x = (int)(str[i - 1][j]);
      int y = (int)(str[i][j]);
      if (vis[y][x])
        return false;
      if (!vis[x][y]) {
        vis[x][y] = 1;
        in[y]++;
      }
      break;
    }
  }
  return topsort();
}

int main() {
  // freopen("data.txt","r",stdin);
  while (cin >> n && n) {
    memset(str, 0, sizeof str);
    for (int i = 1; i <= n; i++)
      scanf("%s", str[i]);
    //        for(int i=1;i<=n;i++)
    //        {
    //            int len=strlen(str[i]);
    //            for(int j=0;j<len;j++)
    //                if(str[i][j]>= 'a' && str[i][j]<='z')
    //                    continue;
    //                else
    //                    while(1){}
    //        }
    if (check())
      puts("yes");
    else
      puts("no");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 555;
int n;
char str[MAXN][22];
int vis[255][255];
int in[255];
queue<int> q;

bool topsort() {
  while (!q.empty())
    q.pop();
  for (int i = 0; i < 255; i++)
    if (in[i] == 0)
      q.push(i);
  int num = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    num++;
    for (int i = 0; i < 255; i++)
      if (vis[u][i] == 1) {
        in[i]--;
        if (in[i] == 0)
          q.push(i);
      }
  }
  return num == 255;
}

bool check() {
  memset(vis, 0, sizeof vis);
  memset(in, 0, sizeof in);
  for (int i = 2; i <= n; i++) {
    for (int j = 0; str[i][j] || str[i - 1][j]; j++) {
      if (str[i][j] == str[i - 1][j])
        continue;
      if (!str[i][j])
        return false;
      int x = (int)(str[i - 1][j] - 'a');
      int y = (int)(str[i][j] - 'a');
      if (x > 30 || y > 30)
        while (1) {
        }
      if (vis[y][x])
        return false;
      if (!vis[x][y]) {
        vis[x][y] = 1;
        in[y]++;
      }
      break;
    }
  }
  return topsort();
}

int main() {
  // freopen("data.txt","r",stdin);
  while (scanf("%d", &n) == 1 && n) {
    memset(str, 0, sizeof str);
    for (int i = 1; i <= n; i++)
      scanf("%s", str[i]);
    if (check())
      puts("yes");
    else
      puts("no");
  }
  return 0;
}
Problem p01646 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1e9;
const int AtoZ = 26;

bool check(int n, int b, int e, vector<string> &str, vector<vector<int>> &ord) {
  char now = str[b][n];
  int s = b;
  for (int i = b; i <= e; i++) {
    if (str[i][n] != now) {
      if (str[i][n] == '0')
        return false;
      if (now != '0') {
        ord[now - 'a'][str[i][n] - 'a'] = 0;
        if (!check(n + 1, s, i - 1, str, ord))
          return false;
      }
      s = i;
      now = str[i][n];
    }
  }
  if (now != '0') {
    if (!check(n + 1, s, e, str, ord))
      return false;
  }
  return true;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> str(n);
    vector<vector<int>> ord(AtoZ, vector<int>(AtoZ, inf));
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      str[i] += "0";
    }
    if (!check(0, 0, n - 1, str, ord)) {
      cout << "no" << endl;
      continue;
    }

    for (int k = 0; k < AtoZ; k++) {
      for (int i = 0; i < AtoZ; i++) {
        for (int j = 0; j < AtoZ; j++) {
          ord[i][j] = min(ord[i][j], ord[i][k] + ord[k][j]);
        }
      }
    }
    bool flag = true;
    for (int i = 0; i < AtoZ; i++) {
      for (int j = i + 1; j < AtoZ; j++) {
        if (ord[i][j] == 0 && ord[j][i] == 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1e9;
const int AtoZ = 'z' - 'a';

bool check(int n, int b, int e, vector<string> &str, vector<vector<int>> &ord) {
  char now = str[b][n];
  int s = b;
  for (int i = b; i <= e; i++) {
    if (str[i][n] != now) {
      if (str[i][n] == '0')
        return false;
      if (now != '0') {
        ord[now - 'a'][str[i][n] - 'a'] = 0;
        if (!check(n + 1, s, i - 1, str, ord))
          return false;
        s = i;
        now = str[i][n];
      }
    }
  }
  if (now != '0') {
    if (!check(n + 1, s, e, str, ord))
      return false;
  }
  return true;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> str(n);
    vector<vector<int>> ord(AtoZ, vector<int>(AtoZ, inf));
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      str[i] += "0";
    }
    if (!check(0, 0, n - 1, str, ord)) {
      cout << "no" << endl;
      continue;
    }

    for (int k = 0; k < AtoZ; k++) {
      for (int i = 0; i < AtoZ; i++) {
        for (int j = 0; j < AtoZ; j++) {
          ord[i][j] = min(ord[i][j], ord[i][k] + ord[k][j]);
        }
      }
    }
    bool flag = true;
    for (int i = 0; i < AtoZ; i++) {
      for (int j = i + 1; j < AtoZ; j++) {
        if (ord[i][j] == 0 && ord[j][i] == 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p01646 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

bool dfs(int pos, int start, const vector<vector<int>> &graph,
         vector<bool> &visited) {
  if (visited[pos])
    return false;

  visited[pos] = true;
  for (int v : graph[pos]) {
    if (v == start)
      return true;
    if (dfs(v, start, graph, visited))
      return true;
  }
  return false;
}

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

  vector<string> v(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
  }
  vector<vector<int>> graph(26);
  for (int i = 0; i < N - 1; ++i) {
    const string &s1 = v[i];
    const string &s2 = v[i + 1];
    if (s1.size() > s2.size() && s1.substr(0, s2.size()) == s2) {
      cout << "no" << endl;
      return true;
    }
    for (int j = 0; j < s1.size() && j < s2.size(); ++j) {
      if (s1[j] != s2[j]) {
        graph[s1[j] - 'a'].push_back(s2[j] - 'a');
        break;
      }
    }
  }
  for (int i = 0; i < 26; ++i) {
    vector<bool> visited(26, false);
    if (dfs(i, i, graph, visited)) {
      cout << "no" << endl;
      return true;
    }
  }
  cout << "yes" << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  while (solve())
    ;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

bool dfs(int pos, int start, const vector<vector<int>> &graph,
         vector<bool> &visited) {
  if (visited[pos])
    return false;

  visited[pos] = true;
  for (int v : graph[pos]) {
    if (v == start)
      return true;
    if (dfs(v, start, graph, visited))
      return true;
  }
  return false;
}

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

  vector<string> v(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
  }
  vector<vector<int>> graph(26);
  for (int i = 0; i < N - 1; ++i) {
    const string &s1 = v[i];
    const string &s2 = v[i + 1];
    if (s1.size() > s2.size() && s1.substr(0, s2.size()) == s2) {
      cout << "no" << endl;
      return true;
    }
    for (int j = 0; j < s1.size() && j < s2.size(); ++j) {
      if (s1[j] != s2[j]) {
        graph[s1[j] - 'a'].push_back(s2[j] - 'a');
        break;
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    vector<bool> visited(26, false);
    if (dfs(i, i, graph, visited)) {
      cout << "no" << endl;
      return true;
    }
  }
  cout << "yes" << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

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

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

vector<int> edge[150];
bool come[150];
int dfs(int x, int k, int p = 1) {
  if (k == x && p != 0)
    return -1;
  if (come[x])
    return 0;
  come[x] = true;
  for (int i = 0; i < edge[x].size(); i++) {
    if (dfs(edge[x][i], k) == -1)
      return -1;
  }
  return 0;
}

bool ok() {
  for (int i = 'a'; i <= 'z'; i++) {
    fill(come, come + 150, false);
    if (dfs(i, i, 0) == -1)
      return false;
  }
  return true;
}

int main() {
  int n;
  string str, last;
  while (cin >> n, n) {

    bool fail = false;
    for (int i = 'a'; i <= 'z'; i++)
      edge[i].clear();
    for (int i = 0; i < n; i++) {
      cin >> str;
      if (i) {
        for (int i = 0; i < last.size(); i++) {
          if (i == str.size()) {
            fail = true;
            break;
          }
          if (str[i] != last[i]) {
            edge[last[i]].push_back(str[i]);
            break;
          }
        }
      }
      last = str;
    }

    cout << ((!fail && ok()) ? "yes" : "no") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

vector<int> edge[100];

int dfs(int x, int d = 0) {
  if (d > 30)
    return -1;
  for (int i = 0; i < edge[x].size(); i++) {
    if (dfs(edge[x][i], d + 1) == -1)
      return -1;
  }
  return 0;
}

bool ok() {
  for (int i = 'a'; i <= 'z'; i++) {
    if (dfs(i) == -1)
      return false;
  }
  return true;
}

int main() {
  int n;
  string str, last;
  while (cin >> n, n) {
    for (int i = 'a'; i <= 'z'; i++)
      edge[i].clear();
    for (int i = 0; i < n; i++) {
      cin >> str;
      if (i) {
        for (int i = 0; i < last.size(); i++) {
          if (i == str.size()) {
            cout << "no" << endl;
            goto fail;
          }
          if (str[i] != last[i]) {
            edge[last[i]].push_back(str[i]);
            break;
          }
        }
      }
      last = str;
    }
    cout << (ok() ? "yes" : "no") << endl;
  fail:;
  }
}
Problem p01646 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
int order[128][128];
vector<string> t;

//[from, to] not [from, to)
bool check(int x, int from, int to) {
  for (int i = from; i <= to; i++) {
    if (t[i].size() <= x)
      continue;

    for (int j = i + 1; j <= to; j++) {
      if (t[j].size() <= x)
        return false;

      char a = t[i][x];
      char b = t[j][x];

      if (a != b && order[a][b] != 0 && order[a][b] == 1) {
        return false;
      }
      order[a][b] = -1;
      order[b][a] = +1;
    }
  }

  int i = from;
  while (i <= to && t[i].size() <= x)
    i++;

  for (; i <= to; i++) {
    int j = i;
    for (; j <= to && t[i][x] == t[j][x]; j++)
      ;
    if (!check(x + 1, i, j - 1))
      return false;
    i = j - 1;
  }

  return true;
}

int main() {
  while (cin >> n, n) {
    t.clear();

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      t.push_back(s);
    }

    memset(order, 0, sizeof(order));

    cout << (check(0, 0, n - 1) ? "yes" : "no") << endl;
  }
}

Error: TLE

Fixed submission:

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

int n;
int order[128];
vector<string> t;

bool cmp(string a, string b) {
  for (int i = 0; i < min(a.size(), b.size()); i++) {
    if (order[a[i]] < order[b[i]]) {
      return true;
    }
    if (order[a[i]] > order[b[i]]) {
      return false;
    }
  }
  return a.size() < b.size();
}

bool check() {
  vector<string> tb = t;
  sort(tb.begin(), tb.end(), cmp);
  return t == tb;
}

int main() {
  while (cin >> n, n) {
    t.clear();

    bool flg[128] = {0};
    vector<char> alpha;

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      t.push_back(s);

      for (int j = 0; j < s.size(); j++) {
        if (flg[s[j]])
          continue;
        flg[s[j]] = true;
        alpha.push_back(s[j]);
      }
    }

    sort(alpha.begin(), alpha.end());

    bool yes = false;

    do {
      for (int i = 0; i < alpha.size(); i++) {
        order[alpha[i]] = i;
      }
      if (check()) {
        yes = true;
        break;
      }
    } while (next_permutation(alpha.begin(), alpha.end()));

    cout << (yes ? "yes" : "no") << endl;
  }
}
Problem p01646 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;
char str[510][20];
int g[30][30];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      scanf("%s", str[i]);
    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        g[i][j] = 0;
    for (int i = 0; i < 26; i++)
      g[i][i] = 1;
    bool ok = true;
    for (int i = 0; i < a; i++) {
      for (int j = i + 1; j < a; j++) {
        int k;
        for (k = 0; str[i][k] && str[j][k]; k++) {
          if (str[i][k] != str[j][k]) {
            g[str[i][k] - 'a'][str[j][k] - 'a'] = 1;
            break;
          }
        }
        if (str[i][k] && !str[j][k])
          ok = false;
      }
    }
    for (int k = 0; k < 26; k++)
      for (int i = 0; i < 26; i++)
        for (int j = 0; j < 26; j++)
          g[i][j] |= (g[i][k] & g[k][j]);

    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        if (i != j && g[i][j] && g[j][i])
          ok = false;
    if (ok)
      printf("yes\\n");
    else
      printf("no\\n");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
char str[510][20];
int g[30][30];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      scanf("%s", str[i]);
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        g[i][j] = 0;
    for (int i = 0; i < a; i++)
      g[i][i] = 1;
    bool ok = true;
    for (int i = 0; i < a; i++) {
      for (int j = i + 1; j < a; j++) {
        int k;
        for (k = 0; str[i][k] && str[j][k]; k++) {
          if (str[i][k] != str[j][k]) {
            g[str[i][k] - 'a'][str[j][k] - 'a'] = 1;
            break;
          }
        }
        if (str[i][k] && !str[j][k])
          ok = false;
      }
    }
    for (int k = 0; k < 26; k++)
      for (int i = 0; i < 26; i++)
        for (int j = 0; j < 26; j++)
          g[i][j] |= (g[i][k] & g[k][j]);

    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        if (i != j && g[i][j] && g[j][i])
          ok = false;
    if (ok)
      printf("yes\\n");
    else
      printf("no\\n");
  }
}
Problem p01646 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N ('z' - 'a')
using namespace std;
typedef long long ll;
int n, ans;
string str[501];
ll mp[N + 1];

void mk_mp(int pos, int i, int j) {
  while (i <= j && pos >= str[i].size())
    i++;
  if (i >= j)
    return;
  int ni = -1, ch = str[i][pos] - 'a', c = 1;

  while (i < j && ans) {
    int a = str[++i][pos] - 'a', f = (ch == a);
    if (f && ni == -1)
      ni = i - 1;
    if (!f && ni >= 0)
      mk_mp(pos + 1, ni, i - 1), ni = -2;

    if (!f && c)
      mk_mp(pos, i, j), mp[ch] |= ((1LL << a) | mp[a]), c = 0;
    if (f && !c)
      ans = 0;
    if (f && str[i - 1].size() > str[i].size() && str[i][pos + 1] == 0)
      ans = 0;
  }
  if (ni >= 0)
    mk_mp(pos + 1, ni, j);
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i <= N; i++)
      mp[i] = 0;
    for (int i = 0; i < n; i++)
      cin >> str[i];
    ans = 1;
    mk_mp(0, 0, n - 1);
    for (int i = 0; i < N; i++)
      if ((mp[i] & (1 << i)))
        ans = 0;
    cout << (ans ? "yes" : "no") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, used[1 << 7], ans;
string str[501];
vector<int> mp[1 << 7];

void mk_mp(int pos, int i, int j) {
  int ni = -1, ch = str[i][pos];
  while (i != j) {
    int a = str[++i][pos];
    if (ch == a && ni == -1)
      ni = i - 1;
    if (ch == a && ni == -2)
      ans = 0;
    if (ch == a && str[i - 1].size() > str[i].size())
      ans = 0;
    if (ch != a && ni >= 0)
      mk_mp(pos + 1, ni, i - 1), ni = -2;
    if (ch != a) {
      mk_mp(pos, i, j);
      mp[ch].push_back(a);
      mp[ch].insert(mp[ch].end(), mp[a].begin(), mp[a].end());
      return;
    }
  }
}

bool check(int pos) {
  if (used[pos] || !ans)
    return 0;
  used[pos] = 1;
  int res = 1, s = mp[pos].size();
  for (int i = 0; i < s; i++)
    if (i == s - 1 || mp[pos][i] != mp[pos][i + 1])
      res *= check(mp[pos][i]);
  used[pos] = 0;
  return res;
}

int main() {

  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 'a'; i <= 'z'; i++)
      mp[i].clear();
    for (int i = 0; i < n; i++)
      cin >> str[i];
    ans = 1;
    mk_mp(0, 0, n - 1);
    memset(used, 0, sizeof(used));
    for (int i = 'a'; i <= 'z'; i++) {
      sort(mp[i].begin(), mp[i].end());
      ans *= check(i);
    }

    cout << (ans ? "yes" : "no") << endl;
  }
  return 0;
}
Problem p01646 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

typedef pair<char, char> P;
typedef pair<int, int> Pii;

const int MAX = 30;
vector<Pii> E;

P comp(string a, string b) {
  for (int i = 0; i < (int)min(a.length(), b.length()); i++)
    if (a[i] != b[i])
      return P(a[i], b[i]);

  if (a.length() > b.length())
    return P(a[b.length()], '?');
  if (a.length() < b.length())
    return P('?', b[a.length()]);
  return P('?', '?');
}

bool add_edge(const vector<string> &v) {
  E.clear();

  for (int i = 0; i + 1 < (int)v.size(); i++) {
    P p = comp(v[i], v[i + 1]);
    if (p == P('?', '?'))
      continue;
    if (p.second == '?')
      return false;
    if (p.first != '?')
      E.push_back(P(p.first - 'a', p.second - 'a'));
  }
  return true;
}

bool hasCircle(int N) {

  vector<int> d(MAX, 0);
  for (int i = 0; i <= N; i++) {
    bool update = false;
    for (int j = 0; j < (int)E.size(); j++)
      if (d[E[j].second] < d[E[j].first] + 1) {
        d[E[j].second] = d[E[j].first] + 1;
        update = true;
      }
    if (i == N && update)
      return true;
    if (!update)
      break;
  }
  return false;
}

bool solve(const vector<string> &v) {
  if (!add_edge(v))
    return false;
  return !hasCircle(v.size());
}

int main() {

  int N;
  while (cin >> N && N) {
    vector<string> v(N);
    for (int i = 0; i < N; i++)
      cin >> v[i];
    cout << (solve(v) ? "yes" : "no") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

typedef pair<char, char> P;
typedef pair<int, int> Pii;

const int MAX = 30;
vector<Pii> E;

P comp(string a, string b) {
  for (int i = 0; i < (int)min(a.length(), b.length()); i++)
    if (a[i] != b[i])
      return P(a[i], b[i]);

  if (a.length() > b.length())
    return P(a[b.length()], '?');
  if (a.length() < b.length())
    return P('?', b[a.length()]);
  return P('?', '?');
}

bool add_edge(const vector<string> &v) {
  E.clear();

  for (int i = 0; i + 1 < (int)v.size(); i++) {
    P p = comp(v[i], v[i + 1]);
    if (p == P('?', '?'))
      continue;
    if (p.second == '?')
      return false;
    if (p.first != '?')
      E.push_back(P(p.first - 'a', p.second - 'a'));
  }
  return true;
}

bool hasCircle(int N) {

  vector<int> d(N, 0);
  for (int i = 0; i <= N; i++) {
    bool update = false;
    for (int j = 0; j < (int)E.size(); j++)
      if (d[E[j].second] < d[E[j].first] + 1) {
        d[E[j].second] = d[E[j].first] + 1;
        update = true;
      }
    if (i == N && update)
      return true;
    if (!update)
      break;
  }
  return false;
}

bool solve(const vector<string> &v) {
  if (!add_edge(v))
    return false;
  return !hasCircle(v.size());
}

int main() {

  int N;
  while (cin >> N && N) {
    vector<string> v(N);
    for (int i = 0; i < N; i++)
      cin >> v[i];
    cout << (solve(v) ? "yes" : "no") << endl;
  }
  return 0;
}
Problem p01646 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
bool f;
bool e[26][26];
void dfs(int x, int d) {
  // cout << x << ":" << d << endl;
  if (d > 26) {
    f = false;
    return;
  }
  if (!f)
    return;
  for (int i = 0; i < 26; i++) {
    if (e[x][i])
      dfs(i, d + 1);
  }
}
int main() {
  int n;
  while (cin >> n, n) {
    int i, j, k;
    vector<string> v;
    string s;
    memset(e, false, sizeof(e));
    for (i = 0; i < n; i++) {
      cin >> s;
      v.push_back(s);
    }
    f = true;
    for (i = 0; i < n; i++) {
      for (j = i + 1; j < n; j++) {
        if (v[i].find(v[j]) == 0 && v[i].size() > v[j].size())
          f = false;
        int x = min(v[i].size(), v[j].size());
        for (k = 0; k < x; k++) {
          if (v[i][k] == v[j][k])
            continue;
          e[v[i][k] - 'a'][v[j][k] - 'a'] = true;
          break;
        }
      }
    }
    for (i = 0; i < 26; i++) {
      dfs(i, 0);
    }
    if (f)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
string s[500];
int n;
int m;
map<char, vector<P>> li;
bool f;

void check(vector<char> x) {
  int i, j, k = 0;

  for (map<char, vector<P>>::iterator it = li.begin(); it != li.end(); ++it) {
    // cout << (it->second).size() << ":" << endl;
    for (j = 0; j < (it->second).size(); j++) {
      // cout << j << endl;
      s[(it->second)[j].first][(it->second)[j].second] = x[k];
    }
    k++;
  }

  f = true;
  for (i = 0; i < n - 1; i++) {
    if (s[i] > s[i + 1]) {
      f = false;
      break;
    }
  }
}

int main() {
  while (cin >> n, n) {
    int i, j, k, c;

    li.clear();

    for (i = 0; i < n; i++) {
      s[i] = "";
      cin >> s[i];
      for (j = 0; j < s[i].size(); j++) {
        li[s[i][j]].push_back(P(i, j));
      }
    }

    m = li.size();

    f = false;

    vector<char> v;
    for (i = 0; i < m; i++)
      v.push_back(i + '0');

    do {
      check(v);
      if (f)
        break;
    } while (next_permutation(v.begin(), v.end()));

    if (f)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}