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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;

bool used[26];

string s;

vector<string> rec(string t, int i) {
  vector<string> res, res1;
  if (i == t.size()) {
    res.PB(t);
  } else {
    if (t[i] == 'z') {
      if (!used['y' - 'a'])
        return res;
      res = rec(t, i + 1);
    } else if (t[i] == 'a') {
      res = rec(t, i + 1);
      if (!used[0]) {
        used[0] = true;
        t[i] = 'b';
        res1 = rec(t, i + 1);
        res.insert(res.begin(), ALL(res1));
        used[0] = false;
      }
    } else {
      if (used[t[i] - 'a' - 1])
        res = rec(t, i + 1);
      if (!used[t[i] - 'a']) {
        string tt = t;
        tt[i] = t[i] + 1;
        used[t[i] - 'a'] = true;
        res1 = rec(tt, i + 1);
        used[t[i] - 'a'] = false;
        res.insert(res.begin(), ALL(res1));
      }
    }
  }
  return res;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    vector<string> ans;
    memset(used, false, sizeof(used));
    ans = rec(s, 0);
    sort(ALL(ans));
    cout << ans.size() << endl;
    if (ans.size() < 10) {
      REP(i, ans.size()) cout << ans[i] << endl;
    } else {
      REP(i, 5) cout << ans[i] << endl;
      REP(i, 5) cout << ans[ans.size() - 5 + i] << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;

bool ok(string s, string t) {
  string ss = s;
  bool f[26];
  memset(f, false, sizeof(f));
  REP(i, s.size()) {
    if (s[i] == 'a')
      continue;
    if (!f[s[i] - 'a']) {
      ss[i] = (char)((int)s[i] - 1);
      f[s[i] - 'a'] = true;
    }
  }
  return ss == t;
}

vector<string> rec(string s, int i, string t) {
  vector<string> res;
  if (i == t.size()) {
    if (ok(s, t))
      res.PB(s);
    return res;
  }
  vector<string> res1;
  res1 = rec(s, i + 1, t);
  res = res1;
  if (s[i] != 'z') {
    s[i] += 1;
    res1 = rec(s, i + 1, t);
    res.insert(res.begin(), res1.begin(), res1.end());
  }
  return res;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    vector<string> ans;
    ans = rec(s, 0, s);
    sort(ALL(ans));
    cout << ans.size() << endl;
    REP(i, min((int)ans.size(), 5)) { cout << ans[i] << endl; }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string s;
  while (cin >> s, s[0] != '#') {
    vector<string> res;
    for (int i = 0; i < (1 << s.size()); i++) {
      string p = s;
      bool flg = true;
      bool found[26] = {0};
      found[0] = true;
      for (int j = 0; j < s.size(); j++) {
        if (i & (1 << j)) {
          if (p[j] == 'z') {
            flg = false;
            break;
          } else {
            p[j]++;
            if (found[p[j] - 'a']) {
              flg = false;
              break;
            }
            found[p[j] - 'a'] = true;
          }
        } else {
          if (!found[p[j] - 'a']) {
            flg = false;
            break;
          }
          found[p[j] - 'a'] = true;
        }
      }

      if (flg) {
        res.push_back(p);
      }
    }
    cout << res.size() << endl;
    sort(res.begin(), res.end());
    if (res.size() > 10) {
      for (int i = 0; i < 5; i++)
        cout << res[i] << endl;
      for (int i = 0; i < 5; i++)
        cout << res[res.size() - 5 + i] << endl;
    } else {
      for (int i = 0; i < res.size(); i++)
        cout << res[i] << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int main() {
  string s;
  while (cin >> s, s[0] != '#') {
    vector<string> res;
    for (int i = 0; i < (1 << s.size()); i++) {
      string p = s;
      bool flg = true;
      for (int j = 0; j < s.size(); j++) {
        if (i & (1 << j)) {
          if (p[j] == 'z') {
            flg = false;
            break;
          } else {
            p[j]++;
          }
        }
      }

      bool found[26] = {0};
      found[0] = true;
      string tmp = p;
      for (int j = 0; j < s.size(); j++) {
        if (!found[tmp[j] - 'a']) {
          found[tmp[j] - 'a'] = true;
          tmp[j]--;
        }
      }

      if (flg && s == tmp) {
        res.push_back(p);
      }
    }
    cout << res.size() << endl;
    sort(res.begin(), res.end());
    if (res.size() > 10) {
      for (int i = 0; i < 5; i++)
        cout << res[i] << endl;
      for (int i = 0; i < 5; i++)
        cout << res[res.size() - 6 + i] << endl;
    } else {
      for (int i = 0; i < res.size(); i++)
        cout << res[i] << endl;
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

string s;
vector<string> ans;
void rec(int idx, vector<bool> state, string sum) {
  if (idx == s.size()) {
    ans.push_back(sum);
    return;
  }
  char c = s[idx];
  if (state[c - 'a']) {
    rec(idx + 1, state, sum + c);
  }
  if (c + 1 <= 'z' && !state[c + 1 - 'a']) {
    state[c + 1 - 'a'] = true;
    rec(idx + 1, state, sum + string(1, (c + 1)));
  }
}

int main() {
  while (cin >> s && s != "#") {
    ans.clear();
    vector<bool> state(26, false);
    state[0] = true;
    rec(0, state, "");
    cout << ans.size() << endl;
    sort(ans.begin(), ans.end());
    if (ans.size() <= 10) {
      REP(i, ans.size()) cout << ans[i] << endl;
    } else {
      REP(i, 5) cout << ans[i] << endl;
      REP(i, 5) cout << ans[ans.size() - 5 + i] << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int INF = (int)((unsigned int)-1 >> 1);

string input;
set<string> s;
bool c[20];
int n;

bool check(string str) {
  for (int i = 1; i < 26; i++) {
    for (int j = 0; j < n; j++) {
      if (str[j] == 'a' + i) {
        str[j] -= 1;
        break;
      }
    }
  }
  return str == input;
}

void dfs(string str, int pos) {
  if (pos == n) {
    if (check(str) && s.count(str) == 0) {
      s.insert(str);
    }
  } else {
    dfs(str, pos + 1);
    if (!c[(int)(str[pos])]) {
      c[(int)(str[pos])] = true;
      str[pos]++;
      dfs(str, pos + 1);
      c[(int)(str[pos]) - 1] = false;
    }
  }
}

int main() {
  while (1) {
    cin >> input;
    if (input == "#")
      break;
    n = input.size();
    fill(c, c + n, false);
    dfs(input, 0);
    printf("%d\\n", (int)s.size());
    vector<string> ans(s.begin(), s.end());
    if (ans.size() > 10) {
      for (int i = 0; i < 5; i++) {
        cout << ans[i] << endl;
      }
      for (int i = ans.size() - 5; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    }
    s.clear();
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
int main() {
  string S;
  bool used[26];
  while (cin >> S, S != "#") {
    int N = S.size();
    int bit = 0;
    for (int i = 0; i < N; i++)
      if (S[i] == 'z')
        bit |= (1 << i);
    vector<string> ans;
    for (int i = 0; i < (1 << N); i++) {
      if (i & bit)
        continue;
      memset(used, 0, sizeof(used));
      used[0] = 1;
      bool invalid = 0;
      for (int j = 0; j < N; j++) {
        char ch = S[j];
        if (i & (1 << j))
          ch++;
        if (!used[ch - 'a']) {
          used[ch - 'a'] = 1;
          ch--;
        }
        if (ch != S[j]) {
          invalid = 1;
          break;
        }
      }
      if (invalid)
        continue;
      string T = S;
      for (int j = 0; j < N; j++) {
        if (i & (1 << j))
          T[j]++;
      }
      ans.push_back(T);
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
    } else {
      for (int i = 0; i < 5; i++)
        cout << ans[i] << endl;
      for (int i = 0; i < 5; i++)
        cout << ans[ans.size() - 5 + i] << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
void encrypt(string &S) {
  for (int i = 1; i < 26; i++) {
    for (int j = 0; j < S.size(); j++) {
      if (S[j] == 'a' + i) {
        S[j]--;
        break;
      }
    }
  }
}
int main() {
  string S;
  while (cin >> S, S != "#") {
    int N = S.size();
    int bit = 0;
    for (int i = 0; i < N; i++)
      if (S[i] == 'z')
        bit |= (1 << i);
    vector<string> ans;
    string T;
    for (int i = 0; i < (1 << N); i++) {
      if (i & bit)
        continue;
      T = S;
      for (int j = 0; j < N; j++) {
        if (i & (1 << j))
          T[j]++;
      }
      encrypt(T);
      if (T == S)
        ans.push_back(T);
    }
    cout << ans.size() << endl;
    sort(ans.begin(), ans.end());
    if (ans.size() <= 10) {
      for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
    } else {
      for (int i = 0; i < 5; i++)
        cout << ans[i] << endl;
      for (int i = 4; i >= 0; i--)
        cout << ans[ans.size() - i - 1] << endl;
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string s;
vector<string> vs;
bool f[26];

void dfs(string t, int now) {
  if (now == s.size()) {
    string r = t;
    memset(f, false, sizeof(f));
    rep(i, 0, r.size()) {
      if (f[r[i] - 'a'] == false && r[i] != 'a') {
        r[i] = r[i] - 1;
      }
      f[t[i] - 'a'] |= true;
    }
    if (r == s)
      vs.push_back(t);
    return;
  }
  int d[26] = {};
  rep(i, 0, now) d[t[i] - 'a']++;
  if (t[now] != 'z') {
    string tmp = t;
    tmp[now] = tmp[now] + 1;
    dfs(tmp, now + 1);
  }
  if (t[now] == 'a' || d[t[now] - 'a'] != 0)
    dfs(t, now + 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    vs.clear();
    dfs(s, 0);
    int ans = vs.size();
    sort(vs.begin(), vs.end());
    cout << ans << endl;
    if (ans <= 10) {
      rep(i, 0, ans) { cout << vs[i] << endl; }
    } else {
      rep(i, 0, 5) { cout << vs[i] << endl; }
      rep(i, ans - 5, ans) { cout << vs[i] << endl; }
    }
  }
}


Error: TLE

Fixed submission:

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

string s;
// vector<string> vs;
string vs[1 << 20];
bool f[26];
int idx;

void dfs(string t, int now) {
  if (now == s.size()) {
    string r = t;
    memset(f, false, sizeof(f));
    rep(i, 0, r.size()) {
      if (f[r[i] - 'a'] == false && r[i] != 'a') {
        r[i] = r[i] - 1;
      }
      f[t[i] - 'a'] |= true;
    }
    if (r == s)
      vs[idx++] = t; // vs.push_back(t);
    return;
  }
  if (t[now] != 'z') {
    string tmp = t;
    tmp[now] = tmp[now] + 1;
    dfs(tmp, now + 1);
  }
  dfs(t, now + 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    // vs.clear();
    idx = 0;
    dfs(s, 0);
    // int ans = vs.size();
    int ans = idx;
    // sort(vs.begin(), vs.end());
    sort(vs, vs + idx);
    cout << ans << endl;
    if (ans <= 10) {
      rep(i, 0, ans) { cout << vs[i] << endl; }
    } else {
      rep(i, 0, 5) { cout << vs[i] << endl; }
      rep(i, ans - 5, ans) { cout << vs[i] << endl; }
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

char s[128];

char t[128];
bool tr[128];
vector<string> Ans;

void dfs(int i) {
  if (!s[i]) {
    t[i] = '\\0';
    Ans.push_back(t);
    return;
  }

  t[i] = s[i];
  if (!tr[t[i]])
    dfs(i + 1);

  if (s[i] != 'z') {
    t[i] = s[i] + 1;
    if (tr[t[i]]) {
      tr[t[i]] = false;
      dfs(i + 1);
      tr[t[i]] = true;
    }
  }
}

int main() {
  while (cin >> s, s[0] != '#') {
    Ans.clear();
    for (char c = 'b'; c <= 'z'; c++)
      tr[c] = true;

    dfs(0);

    cout << Ans.size() << endl;
    if (Ans.size() <= 10) {
      rep(i, Ans.size()) cout << Ans[i] << endl;
    } else {
      rep(i, 5) cout << Ans[i] << endl;
      rep(i, 5) cout << Ans[Ans.size() - 5 + i] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int main() {
  for (char s[128]; scanf("%s", s), s[0] != '#';) {
    int n = strlen(s);
    vector<string> Ans;
    rep(S, 1 << n) {
      char t[128];
      bool ok = true;
      rep(i, n) {
        t[i] = s[i] + (S & (1 << (n - i - 1)) ? 1 : 0);
        if (t[i] > 'z')
          ok = false;
      }
      if (!ok)
        continue;

      char u[128];
      strcpy(u, t);
      for (char c = 'b'; c <= 'z'; c++) {
        rep(i, n) if (u[i] == c) {
          u[i]--;
          break;
        }
      }
      if (strcmp(s, u) == 0)
        Ans.push_back(t);
    }

    cout << Ans.size() << endl;
    if (Ans.size() <= 10) {
      rep(i, Ans.size()) cout << Ans[i] << endl;
    } else {
      rep(i, 5) cout << Ans[i] << endl;
      rep(i, 5) cout << Ans[Ans.size() - 5 + i] << endl;
    }
  }

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

#include <iostream>
#include <vector>

using namespace std;

vector<string> ans;
bool ap[30];
string s;
int sum;

void dfs(int n, char c, string now) {
  // cout<<"dfs(int "<<n<<",char "<<c<<",string "<<now<<")"<<endl;
  if (n == s.size()) {
    sum++;
    ans.push_back(now);
    return;
  }
  if (ap[c - 'a'] == true || c == 'a') {
    bool bac = ap[c - 'a'];
    ap[c - 'a'] = true;
    dfs(n + 1, s[n + 1], now + c);
    ap[c - 'a'] = bac;
  }
  if (ap[c - 'a' + 1] == false && c != 'z') {
    bool bac = ap[c - 'a' + 1];
    ap[c - 'a' + 1] = true;
    dfs(n + 1, s[n + 1], now + (char)(c + 1));
    ap[c - 'a' + 1] = bac;
  }
}

int main() {
  while (1) {
    while (!ans.empty())
      ans.pop_back();
    sum = 0;
    cin >> s;
    if (s == "#")
      break;
    dfs(0, s[0], "");
    cout << sum << endl;

    if (sum <= 10) {
      for (int i = 0; i < sum; i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << ans[i] << endl;
      }
      for (int i = ans.size() - 5; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int leng;

bool comp(const string &s1, const string &s2) {
  for (int i = 0; i < leng; i++) {
    if (s1[i] > s2[i])
      return false;
    if (s2[i] > s1[i])
      return true;
  }
  return true;
}

bool alpha(int code) { return code >= 97 && code < 123; }

bool func(string s, int i) {
  bool decoded[26] = {false};
  int F;
  F = 0;
  decoded[0] = true;
  for (int p = 0; p < leng; p++) {
    if (alpha(s[p]) && !decoded[s[p] - 97]) {
      decoded[s[p] - 97] = true;
      F += 1 << p;
    }
  }
  return F == i;
}

int main() {
  string str, str1, str2;
  vector<string> S;
  int res;

  while (true) {
    cin >> str;
    if (str.find("#") != string::npos)
      break;

    leng = str.size();
    res = 0;
    for (int i = 0; i < 1 << leng; i++) {
      str1 = str;
      for (int j = 0; j < leng; j++) {
        if ((i & (1 << j)) != 0) {
          str1[j]++;
        }
      }
      if (func(str1, i)) {
        res++;
        S.push_back(str1);
        sort(S.begin(), S.end(), comp);
        if (S.size() > 10)
          S.erase(S.begin() + 5);
      }
    }

    if (S.empty()) {
      cout << "0" << endl;
    } else {
      cout << res << endl;
      vector<string>::iterator it = S.begin();
      for (int i = 0; i < S.size(); i++) {
        cout << *it << endl;
        it++;
      }
    }
    S.clear();
  }

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


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

using namespace std;

vector<string> ans;
string rs;

void dfs(int n, string s) {

  if (rs.size() == n) {
    string t = s;
    bool c[256];
    memset(c, 0, sizeof c);
    for (int i = 0; i < rs.size(); i++) {
      if (t[i] != 'a' && !c[t[i]]) {
        c[t[i]] = true;
        t[i]--;
      }
    }

    if (rs == t)
      ans.push_back(s);
    return;
  }

  string t = s;
  bool c[256];
  memset(c, 0, sizeof c);
  for (int i = 0; i < t.size(); i++) {
    if (t[i] != 'a' && !c[t[i]]) {
      c[t[i]] = true;
      t[i]--;
    }
  }
  if (t != rs.substr(0, n))
    return;

  s += 'a';
  for (char c = rs[n]; c <= rs[n] + 1; c++) {
    if (c >= 'a' && c <= 'z') {
      s[n] = c;
      dfs(n + 1, s);
    }
  }
}

int main() {

  while (cin >> rs && rs != "#") {

    ans.clear();
    dfs(0, "");

    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << ans[i] << endl;
      }
      for (int i = ans.size() - 5; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:


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

using namespace std;

vector<string> ans;
string rs;

void dfs(int n, string s) {

  if (rs.size() == n) {
    string t = s;
    /*
    for (char c = 'b'; c <= 'z'; c++){
            for (int i = 0; i < rs.size(); i++){
                    if (t[i] == c){
                    t[i] = c - 1;
                    break;
                    }
            }
    }
    */
    bool c[256];
    memset(c, 0, sizeof c);
    for (int i = 0; i < rs.size(); i++) {
      if (t[i] != 'a' && !c[t[i]]) {
        c[t[i]] = true;
        t[i]--;
      }
    }

    if (rs == t)
      ans.push_back(s);
    return;
  }

  s += 'a';
  for (char c = rs[n]; c <= rs[n] + 1; c++) {
    if (c >= 'a' && c <= 'z') {
      s[n] = c;
      dfs(n + 1, s);
    }
  }
}

int main() {

  while (cin >> rs && rs != "#") {

    ans.clear();
    dfs(0, "");

    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << ans[i] << endl;
      }
      for (int i = ans.size() - 5; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    }
  }

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

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

vector<string> v;
string t;
bool used[26];

void dfs(string s) {
  int n = s.size();
  if (n == t.size()) {
    v.PB(s);
    return;
  }
  if (t[n] == 'a' || used[t[n] - 'a']) {
    dfs(s + t[n]);
  }
  if (t[n] != 'z' && !used[t[n] - 'a' + 1]) {
    used[t[n] - 'a' + 1] = true;
    dfs(s + (char)(t[n] + 1));
    used[t[n] - 'a' + 1] = false;
  }
}

signed main(void) {
  while (true) {
    cin >> t;
    if (t == "#")
      break;
    v.clear();
    REP(i, 26) used[i] = false;

    dfs("");
    if (v.size() <= 10) {
      cout << v.size() << endl;
      for (string i : v) {
        cout << i << endl;
      }
    } else {
      cout << v.size() << endl;
      REP(i, 5) cout << v[i] << endl;
      FOR(i, v.size() - 5, v.size()) cout << v[i] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

signed main(void) {
  while (true) {
    string s;
    cin >> s;
    if (s == "#")
      break;

    const int n = s.size();
    vector<string> v;
    REP(i, 1 << n) {
      string t = "";
      REP(j, n) {
        if (s[j] == 'z')
          t[j] += 'z';
        else if (i >> j & 1) {
          t.PB((char)(s[j] + 1));
        } else {
          t.PB((char)s[j]);
        }
        // cout << t << " i:" << j << " s[j]:" << s[j] << endl;
      }
      // cout << "t:" << t << endl;
      string tmp = t;
      for (char c = 'b'; c <= 'z'; ++c) {
        int itr = t.find(c);
        //  cout << itr << " " << t[itr] << endl;
        if (itr != -1)
          t[itr] = c - 1;
      }
      if (t == s) {
        v.PB(tmp);
      }
    }

    sort(ALL(v));
    if (v.size() <= 10) {
      cout << v.size() << endl;
      for (string i : v) {
        cout << i << endl;
      }
    } else {
      cout << v.size() << endl;
      REP(i, 5) cout << v[i] << endl;
      FOR(i, v.size() - 5, v.size()) cout << v[i] << endl;
    }
  }

  return 0;
}