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

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

using namespace std;

vector<string> res;
int flag[50], flag2[50], flag3[50], len;
string str;
void Enc(int x) {
  if (x == -1) {
    for (int i = 0; i < 26; i++)
      if (flag[i] >= 1 && flag3[i] == 0)
        return;
    res.push_back(str);
  } else {
    if (str[x] < 'z' && flag3[str[x] + 1 - 'a'] == 0) {
      str[x]++;
      flag2[str[x] - 'a'] = 1;
      flag3[str[x] - 'a'] = 1;
      Enc(x - 1);
      flag3[str[x] - 'a'] = 0;
      flag2[str[x] - 'a'] = 0;
      str[x]--;
    }
    if (flag2[str[x] - 'a'] >= 1)
      return;
    if (str[x] > 'a') {
      flag[str[x] - 'a']++;
      Enc(x - 1);
      flag[str[x] - 'a']--;
    } else
      Enc(x - 1);
  }
}

int main(void) {
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    res.clear();
    len = str.size();
    memset(flag, 0, sizeof(flag));
    memset(flag2, 0, sizeof(flag2));
    memset(flag3, 0, sizeof(flag3));
    Enc(len - 1);
    printf("%d\\n", res.size());
    sort(res.begin(), res.end());
    if (res.size() <= 10)
      for (int i = 0; i < res.size(); i++)
        cout << res[i] << endl;
    else {
      for (int i = 0; i < 5; i++)
        cout << res[i] << endl;
      for (int i = res.size() - 5; i < res.size(); i++)
        cout << res[i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

vector<string> res;
int used[50];

int main(void) {
  while (1) {
    string str;
    cin >> str;
    if (str == "#")
      break;
    res.clear();
    int len = str.size();
    for (int i = 0; i < (1 << len); i++) {
      string str2 = str;
      int flag2 = 0;
      for (int j = 0; j < len; j++) {
        if ((i >> j & 1)) {
          if (str2[j] == 'z')
            flag2 = 1;
          else
            str2[j] = str2[j] + 1;
        }
      }
      if (flag2 == 0) {
        string str3 = str2;
        for (int j = 1; j < 26; j++) {
          for (int k = 0; k < len; k++) {
            if (str3[k] == 'a' + j) {
              str3[k]--;
              break;
            }
          }
        }
        // cout << str2 << " " << str3 << endl;
        if (str3 == str)
          res.push_back(str2);
      }
    }
    printf("%d\\n", res.size());
    sort(res.begin(), res.end());
    if (res.size() < 10)
      for (int i = 0; i < res.size(); i++)
        cout << res[i] << endl;
    else {
      for (int i = 0; i < 5; i++)
        cout << res[i] << endl;
      for (int i = res.size() - 5; i < res.size(); i++)
        cout << res[i] << endl;
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

vector<string> res;

void search(string s, char c) {
  if (c == 'a') {
    res.psb(s);
    return;
  }

  for (int i = 0; i < (int)s.size(); i++) {
    if (s[i] == c)
      return;
    if (s[i] == c - 1) {
      string t = s;
      t[i] = c;
      search(t, c - 1);
    }
  }

  search(s, c - 1);
}

int main() {
  string s, t;
  int i;
  while (1) {
    cin >> s;
    if (s == "#")
      break;

    res.clear();
    search(s, 'z');

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

int n;
string s, t;
vector<string> res;
int cnt;

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

int main() {
  int i, j;
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    n = (int)s.size();

    cnt = 0;
    res.clear();
    int N = (int)pow(2.0, (double)n);
    for (i = 0; i < N; i++) {
      t = s;
      for (j = 0; j < n; j++)
        if (t[j] < 'z' && (i & (1 << j)))
          t[j]++;
      if (check()) {
        cnt++;
        res.psb(t);
        /*
                if (res.size() > 10) {
                  sort(res.begin(), res.end());
                  res.erase(res.begin()+5);
                }
        */
      }
    }

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

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

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

string S;
vector<string> decodes;

bool match(string decode, int p) {
  REP(i, 'b', 'z' + 1) {
    REP(j, 0, p) {
      if (decode[j] == i) {
        decode[j]--;
        break;
      }
    }
  }
  bool ret = true;
  REP(i, 0, p) if (decode[i] != S[i]) ret = false;
  return ret;
}

void dfs(int n, string s) {
  if (n >= s.length()) {
    if (match(s, n)) {
      decodes.push_back(s);
    }
    return;
  }

  dfs(n + 1, s);

  if (s[n] < 'z') {
    s[n]++;
    if (match(s, n)) {
      dfs(n + 1, s);
    }
    s[n]--;
  }
}

int main(void) {
  while (cin >> S, S != "#") {
    decodes.clear();
    string next = S;
    dfs(0, next);
    cout << decodes.size() << endl;
    if (decodes.size() <= 10) {
      REP(i, 0, decodes.size()) cout << decodes[i] << endl;
    } else {
      REP(i, 0, 5) cout << decodes[i] << endl;
      REP(i, decodes.size() - 5, decodes.size()) cout << decodes[i] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

string S;
vector<string> decodes;

bool match(string decode) {
  REP(i, 'b', 'z' + 1) {
    REP(j, 0, decode.length()) {
      if (decode[j] == i) {
        decode[j]--;
        break;
      }
    }
  }
  return decode == S;
}

void dfs(int n, string s) {
  if (n >= s.length()) {
    if (match(s)) {
      decodes.push_back(s);
    }
    return;
  }

  string next = s;
  dfs(n + 1, next);

  if (next[n] < 'z') {
    next[n]++;
    dfs(n + 1, next);
  }
}

int main(void) {
  while (cin >> S, S != "#") {
    decodes.clear();
    dfs(0, S);
    cout << decodes.size() << endl;
    if (decodes.size() <= 10) {
      REP(i, 0, decodes.size()) cout << decodes[i] << endl;
    } else {
      REP(i, 0, 5) cout << decodes[i] << endl;
      REP(i, decodes.size() - 5, decodes.size()) cout << decodes[i] << endl;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
string n;
int ns;
string t[500000];
int pos;
int use[30];

bool che(string x) {
  rep(i, 26) use[i] = 0;

  rep(i, ns) {
    int tmp = x[i] - 'a';
    if (!use[tmp] && tmp > 0) {
      x[i]--;
      use[tmp] = 1;
    }
  }
  return x == n;
}

void dfs(int now, string x) {
  if (ns == now) {

    if (che(x))
      t[pos++] = x;
    return;
  }

  dfs(now + 1, x + n[now]);

  if (n[now] != 'z') {
    char a = n[now];
    a++;
    dfs(now + 1, x + a);
  }
}

int main() {
  while (cin >> n, n != "#") {

    ns = n.size();
    dfs(0, "");
    cout << pos << endl;
    if (pos <= 10) {
      rep(i, pos) cout << t[i] << endl;
    }

    else {
      rep(i, 5) cout << t[i] << endl;
      for (int i = pos - 5; i < pos; i++)
        cout << t[i] << endl;
    }
    rep(i, pos) t[i] = "";
    pos = 0;
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
string n;
int ns;
string t[500000];
int pos;
int use[30];

bool che(string x) {
  rep(i, 26) use[i] = 0;

  rep(i, ns) {
    int tmp = x[i] - 'a';
    if (!use[tmp] && tmp > 0) {
      x[i]--;
      use[tmp] = 1;
    }
  }
  return x == n;
}

void dfs(int now, string x) {
  if (ns == now) {

    if (che(x))
      t[pos++] = x;
    return;
  }

  dfs(now + 1, x + n[now]);

  if (n[now] != 'z') {
    char a = 'a';
    rep(i, (n[now] - 'a') + 1) a++;
    dfs(now + 1, x + a);
  }
}

int main() {
  while (cin >> n, n != "#") {

    ns = n.size();
    dfs(0, "");
    cout << pos << endl;
    if (pos <= 10) {
      rep(i, pos) cout << t[i] << endl;
    }

    else {
      rep(i, 5) cout << t[i] << endl;
      for (int i = pos - 5; i < pos; i++)
        cout << t[i] << endl;
    }
    rep(i, pos) t[i] = "";
    pos = 0;
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#include <set>
#include <utility>
using namespace std;

int cnt;
set<string> ans;
int tmp;

int rec(string s, int pos, bool u[26]) {
  if (pos == s.length()) {
    ans.insert(s);
    if (ans.size() > 10) {
      set<string>::iterator ite = ans.begin();
      for (int i = 0; i < 5; i++)
        ite++;
      ans.erase(ite);
    }
    tmp++;
    return 0;
  }
  bool *used;
  used = new bool[26];
  for (int i = 0; i < 26; i++)
    used[i] = u[i];
  if (s[pos] == 'a') {
    used[0] = true;
    rec(s, pos + 1, used);
    used[0] = false;
    if (used[1] != true) {
      used[1] = true;
      s[pos]++;
      rec(s, pos + 1, used);
    }
  } else if (s[pos] == 'z') {
    if (used[25] == false) {
      return 0;
    } else {
      rec(s, pos + 1, used);
    }
  } else if (used[s[pos] - 'a'] == true) {
    rec(s, pos + 1, used);
    if (used[s[pos] - 'a' + 1] != true) {
      used[s[pos] - 'a' + 1] = true;
      s[pos]++;
      rec(s, pos + 1, used);
    }
  } else {
    if (used[s[pos] - 'a' + 1] != true) {
      used[s[pos] - 'a' + 1] = true;
      s[pos]++;
      rec(s, pos + 1, used);
    }
  }
  delete[] used;
  return 0;
}

int main() {
  string s;
  while (cin >> s) {
    ans.clear();
    cnt = 0;
    tmp = 0;
    if (s == "#")
      break;
    bool used[26];
    memset(used, 0, sizeof(used));
    rec(s, 0, used);
    cout << tmp << endl;
    for (string i : ans) {
      if (cnt < 5) {
        cout << i << endl;
      } else if (cnt > ans.size() - 6) {
        cout << i << endl;
      }
      cnt++;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <set>
#include <utility>
using namespace std;

int cnt;
set<string> ans;

int rec(string s, int pos, bool u[26]) {
  if (pos == s.length()) {
    ans.insert(s);
    return 0;
  }
  bool *used;
  used = new bool[26];
  for (int i = 0; i < 26; i++)
    used[i] = u[i];
  if (s[pos] == 'a') {
    used[0] = true;
    rec(s, pos + 1, used);
    used[0] = false;
    used[1] = true;
    s[pos]++;
    rec(s, pos + 1, used);
  } else if (s[pos] == 'z') {
    if (used[25] == false) {
      return 0;
    } else {
      rec(s, pos + 1, used);
    }
  } else if (used[s[pos] - 'a'] == true) {
    rec(s, pos + 1, used);
    used[s[pos] - 'a' + 1] = true;
    s[pos]++;
    rec(s, pos + 1, used);
  } else {
    used[s[pos] - 'a' + 1] = true;
    s[pos]++;
    rec(s, pos + 1, used);
  }
  delete[] used;
  return 0;
}

int main() {
  string s;
  while (cin >> s) {
    ans.clear();
    cnt = 0;
    if (s == "#")
      break;
    bool used[26];
    memset(used, 0, sizeof(used));
    rec(s, 0, used);
    cout << ans.size() << endl;
    for (string i : ans) {
      if (cnt < 5) {
        cout << i << endl;
      } else if (cnt > ans.size() - 6) {
        cout << i << endl;
      }
      cnt++;
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <set>
#include <string>
using namespace std;
bool used[26] = {};
set<string> ans;

void dec(string &s, int d = 0) {
  if (d == s.length()) {
    ans.insert(s);
    return;
  }
  if (s[d] == 'z' or s[d] == 'a') {
    if (s[d] == 'z') {
      if (used[s[d] - 'a'])
        dec(s, d + 1);
      else
        return;
    } else {
      dec(s, d + 1);
      if (used[s[d] - 'a' + 1] == false) {
        s[d]++;
        used[s[d] - 'a'] = true;
        dec(s, d + 1);
        used[s[d] - 'a'] = false;
        s[d]--;
      }
    }
  } else {
    if (used[s[d] - 'a' + 1] == false) {
      s[d]++;
      used[s[d] - 'a'] = true;
      dec(s, d + 1);
      used[s[d] - 'a'] = false;
      s[d]--;
    }
    if (used[s[d] - 'a'] == true) {
      dec(s, d + 1);
    }
  }
}

int main() {
  string s;
  while (std::cin >> s, s != "#") {
    if (s == "z") {
      std::cout << 0 << std::endl;
      continue;
    }
    ans.clear();
    int n = s.length();
    dec(s);
    std::cout << ans.size() << std::endl;
    if (ans.size() <= 10) {
      for (auto a : ans)
        std::cout << a << std::endl;
    } else {
      auto a = ans.begin();
      for (int i = 0; i < 5; i++, a++)
        std::cout << (*a) << std::endl;
      a = ans.end();
      for (int i = 0; i < 5; i++)
        a--;
      for (; a != ans.end(); a++)
        std::cout << (*a) << std::endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
using namespace std;
bool used[26] = {};
set<string> ans;

void dec(string &s, int d = 0) {
  if (d == s.length()) {
    ans.insert(s);
    return;
  }
  if (s[d] == 'z' or s[d] == 'a') {
    if (s[d] == 'z') {
      dec(s, d + 1);
    } else {
      dec(s, d + 1);
      used[s[d] - 'a' + 1] = true;
      s[d]++;
      dec(s, d + 1);
      used[s[d] - 'a'] = false;
      s[d]--;
    }
  } else {
    if (used[s[d] - 'a' + 1] == false) {
      used[s[d] - 'a' + 1] = true;
      s[d]++;
      dec(s, d + 1);
      used[s[d] - 'a'] = false;
      s[d]--;
    }
    if (used[s[d] - 'a'] == true) {
      dec(s, d + 1);
    }
  }
}

int main() {
  string s;
  while (std::cin >> s, s != "#") {
    if (s == "z") {
      std::cout << 0 << std::endl;
      continue;
    }
    ans.clear();
    int n = s.length();
    dec(s);
    std::cout << ans.size() << std::endl;
    if (ans.size() <= 10) {
      for (auto a : ans)
        std::cout << a << std::endl;
    } else {
      auto a = ans.begin();
      for (int i = 0; i < 5; i++, a++)
        std::cout << (*a) << std::endl;
      a = ans.end();
      for (int i = 0; i < 5; i++)
        a--;
      for (; a != ans.end(); a++)
        std::cout << (*a) << std::endl;
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1195&lang=jp
// Wの変換後の文字がSと一致する必要条件として、
// Wのn文字目までの変換後の文字がSのn文字目まで一致することが挙げられる。
// よってn=1,2,\\dotsと順番にやればいい。

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

vector<string> V[30];

char c(char a) { return (char)((int)a + 1); }

char d(int n) { return (char)(n + (int)'a'); }

int main() {
  string S;
  while (cin >> S && S != "#") {
    for (auto i = 0; i < 30; i++) {
      V[i].clear();
    }
    int N = S.size();
    V[0].push_back("");
    for (auto i = 0; i < N; i++) {
      for (auto j = 0; j < V[i].size(); j++) {
        string T = V[i][j];
        string U[2];
        U[0] = T + S[i];
        U[1] = T + c(S[i]);
        for (auto k = 0; k < 2; k++) {
          string W = U[k];
          for (auto l = 0; l < 25; l++) {
            auto x = W.find(d(l + 1));
            if (x < N) {
              W[x] = d(l);
            }
          }
          if (W == S.substr(0, i + 1)) {
            V[i + 1].push_back(U[k]);
          }
        }
      }
    }
    sort(V[N].begin(), V[N].end());
    auto k = V[N].size();
    cout << k << endl;
    if (k <= 10) {
      for (auto i = 0; i < k; i++) {
        cout << V[N][i] << endl;
      }
    } else {
      for (auto i = 0; i < 5; i++) {
        cout << V[N][i] << endl;
      }
      for (auto i = k - 5; i < k; i++) {
        cout << V[N][i] << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

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

vector<string> V;

char c(char a) { return (char)((int)a + 1); }

char d(int n) { return (char)(n + (int)'a'); }

int main() {
  string S;
  while (cin >> S && S != "#") {
    V.clear();
    int N = S.size();
    for (auto i = 0; i < (1 << N); i++) {
      string T = S;
      for (auto j = 0; j < N; j++) {
        if (i >> j & 1) {
          T[j] = c(T[j]);
        }
      }
      string U = T;
      for (auto j = 0; j < 25; j++) {
        auto x = T.find(d(j + 1));
        if (x < N) {
          T[x] = d(j);
        }
      }
      if (S == T) {
        V.push_back(U);
      }
    }
    sort(V.begin(), V.end());
    auto k = V.size();
    cout << k << endl;
    if (k <= 10) {
      for (auto i = 0; i < k; i++) {
        cout << V[i] << endl;
      }
    } else {
      for (auto i = 0; i < 5; i++) {
        cout << V[i] << endl;
      }
      for (auto i = k - 5; i < k; i++) {
        cout << V[i] << endl;
      }
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool used[26];
string s;
vector<string> ans;

void dfs(int n) {
  if (n == s.size()) {
    ans.push_back(s);
    return;
  }

  if (used[(s[n] - 'a' - 1 + 26) % 26]) {
    dfs(n + 1);
  }
  if (!used[s[n] - 'a']) {
    used[s[n] - 'a'] = true;
    s[n]++;
    dfs(n + 1);
    s[n]--;
    used[s[n] - 'a'] = false;
  }
}

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

  while (cin >> s, s != "#") {
    ans.clear();
    used[25] = true;
    dfs(0);

    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;
      }
    }
  }
}

Error: TLE

Fixed submission:

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

string sim(string s) {
  int n = s.size();
  int p[26];
  fill(p, p + 26, -1);
  for (int i = 0; i < n; i++) {
    if (s[i] != 'a' && p[s[i] - 'a'] == -1) {
      p[s[i] - 'a'] = i;
    }
  }
  for (int i = 0; i < 26; i++) {
    if (p[i] != -1)
      s[p[i]]--;
  }
  return s;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  while (cin >> s, s != "#") {
    int n = s.size();
    int c = 0;
    vector<string> ans;
    for (int i = 0; i < 1 << n; i++) {
      string ss = s;
      for (int j = 0; j < n; j++) {
        if (i & (1 << j))
          ss[j]++;
      }

      if (sim(ss) == s) {
        c++;
        ans.push_back(ss);
      }
    }

    sort(ans.begin(), ans.end());
    cout << c << 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;
      }
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < (int)N; i++)
#define p(S) cout << (S) << endl
#define ck(n, a, b) (a <= (n) && (n) <= b)
using namespace std;
const int inf = 1e9;

vector<string> ans;
string str;
bool used[26];
void esdfs(string s) {
  unsigned int num = s.size();
  if (num == str.size()) {
    ans.push_back(s);
    return;
  }
  if (str[num] == 'a' || used[str[num] - 'a']) {
    esdfs(s + str[num]);
  }
  if (str[num] != 'z' && !used[str[num] - 'a' + 1]) {
    used[str[num] - 'a' + 1] = true;
    esdfs(s + (char)(str[num] + 1));
    used[str[num] - 'a' + 1] = false;
  }
  return;
}
int main() {
  while (cin >> str) {
    ans.clear();
    REP(i, 0, 26) used[i] = false;
    if (str == "#")
      break;
    esdfs("");
    p(ans.size());
    REP(i, 0, min<int>(ans.size(), 5)) { p(ans[i]); }
    REP(i, max<int>(ans.size() - 5, 5), ans.size()) { p(ans[i]); }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < (int)N; i++)
#define p(S) cout << (S) << endl
using namespace std;
const int inf = 1e9;

vector<string> ans;
int numans;
string str;
void es(string s) {
  string tmps = s;
  REP(i, 'b', 'z' + 1) {
    REP(j, 0, tmps.size()) {
      if (tmps[j] == (char)i) {
        tmps[j]--;
        if (str[j] != tmps[j])
          return;
        break;
      }
    }
  }
  if (tmps == str) {
    if (ans.size() < 10)
      ans.push_back(s);
    numans++;
  }
  return;
}
int main() {
  while (cin >> str) {
    ans.clear();
    numans = 0;
    if (str == "#")
      break;
    int lb = 0;
    REP(mask, 1, (1 << str.size())) {
      lb++;
      if (ans.size() == 5) {
        break;
      }
      string tmps = str;
      REP(i, 0, str.size()) if (mask & (1 << i)) tmps[tmps.size() - 1 - i]++;
      es(tmps);
    }

    for (int mask = (1 << str.size()) - 1; mask > lb; mask--) {
      string tmps = str;
      REP(i, 0, str.size()) if (mask & (1 << i)) tmps[tmps.size() - 1 - i]++;
      es(tmps);
    }

    p(numans);
    REP(i, 0, min<int>(ans.size(), 5)) { p(ans[i]); }
    for (int i = ans.size() - 1; i >= 5; i--) {
      p(ans[i]);
    }
  }
  return 0;
}