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

#include <bits/stdc++.h>

using namespace std;

vector<string> ans;
string s;
vector<bool> state(26, false);

int dfs(int idx, string sum) {
  if (idx == s.size()) {
    ans.emplace_back(sum);
    return 0;
  }
  char c = s[idx];
  if (state[c - 'a']) {
    dfs(idx + 1, sum + c);
  }
  if (c + 1 <= 'z' && !state[c + 1 - 'a']) {
    state[c + 1 - 'a'] = true;
    dfs(idx + 1, sum + (char)(c + 1));
    state[c + 1 - 'a'] = false;
  }
  return 0;
}

int main() {
  while (cin >> s, s != "#") {
    ans.clear();
    state[0] = true;
    dfs(0, "");
    cout << ans.size() << endl;
    if (ans.size() < 10) {
      for (auto it = ans.begin(); it != ans.end(); it++) {
        cout << *it << 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;

string encrypt(string s) {
  int a[26] = {0};
  for (int unsigned i = 0; i < s.size(); i++) {
    if (s[i] != 'a' && a[s[i] - 'b'] == 0) {
      a[s[i] - 'b'] = 1;
      s[i]--;
    }
  }
  return s;
}

int main() {
  while (1) {
    set<string> sets;
    string s;
    cin >> s;
    if (s == "#")
      break;
    for (int i = 0; i < pow(2, s.size()); i++) {
      int d = i;
      string s1(s.c_str(), s.length());
      for (int j = 0; j < s1.size(); j++) {
        if (s1[j] == 'z')
          continue;
        s1[j] = (char)(s1[j] + d % 2);
        d /= 2;
      }
      if (encrypt(s1) == s) {
        sets.insert(s1);
      }
    }
    cout << sets.size() << endl;
    for (auto it = sets.begin(); it != sets.end(); it++) {
      cout << *it << endl;
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

string s;
vector<string> ans;
int cnt;

bool f;
void encode(string now, char c) {
  if (c == 'z' + 1) {
    if (now == s.substr(0, now.size()))
      f = true;
    return;
  }
  bool rep = true;
  for (int i = 0; i < now.size(); i++) {
    if (now[i] == c) {
      now[i] = c - 1;
      if (now[i] == s[i])
        encode(now, c + 1);
      now[i] = c;
      rep = false;
      break;
    }
  }
  if (rep)
    encode(now, c + 1);
}

void rec(string now) {

  f = false;
  encode(now, 'b');
  if (!f)
    return;

  if (now.size() == s.size()) {
    cnt++;
    ans.push_back(now);
    if (ans.size() > 10)
      ans.erase(ans.begin() + 5);
    return;
  }

  char r = s[now.size()];
  rec(now + r);
  if (s[now.size()] != 'z')
    rec(now + (char)(r + 1));
}

int main() {

  while (cin >> s, s != "#") {
    cnt = 0;
    rec("");
    cout << cnt << endl;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
    ans.clear();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

string s;
vector<string> ans;
int cnt;

bool f;
void encode(string now, char c) {
  if (c == 'z' + 1) {
    if (now == s)
      f = true;
    return;
  }
  bool rep = true;
  for (int i = 0; i < now.size(); i++) {
    if (now[i] == c) {
      now[i] = c - 1;
      if (now[i] == s[i])
        encode(now, c + 1);
      now[i] = c;
      rep = false;
      break;
    }
  }
  if (rep)
    encode(now, c + 1);
}

void rec(string now) {
  if (now.size() == s.size()) {
    f = false;
    encode(now, 'b');
    if (f) {
      cnt++;
      ans.push_back(now);
      if (ans.size() > 10)
        ans.erase(ans.begin() + 5);
    }
    return;
  }

  char r = s[now.size()];
  rec(now + r);
  if (s[now.size()] != 'z')
    rec(now + (char)(r + 1));
}

int main() {

  while (cin >> s, s != "#") {
    cnt = 0;
    rec("");
    cout << cnt << endl;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
    ans.clear();
  }
}
Problem p00776 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

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

using namespace std;

int main() {
  while (1) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    int l = s.size();
    vector<string> st;
    REP(i, 1 << l) {
      string cp = s;
      bool ok = true;
      REP(j, l) {
        if ((i >> j) & 1) {
          if (cp[j] == 'z') {
            ok = false;
            break;
          }
          cp[j]++;
        }
      }
      if (!ok) {
        i += i & (-i);
        --i;
        continue;
      }
      vector<bool> isc(26, false);
      auto cpen = cp;
      REP(j, l) {
        if (!isc[cp[j] - 'a'] && cp[j] != 'a') {
          if (((i >> j) & 1) == 0) {
            ok = false;
            break;
          }
          isc[cp[j] - 'a'] = true;
          --cpen[j];
        }
        if (cpen[j] != s[j]) {
          ok = false;
          break;
        }
      }
      if (!ok)
        continue;
      st.push_back(cp);
    }
    sort(begin(st), end(st));
    auto itr = unique(begin(st), end(st));
    cout << (itr - begin(st)) << endl;
    if ((itr - begin(st)) < 10) {
      REP(i, itr - begin(st)) cout << st[i] << endl;
    } else {
      REP(i, 5) cout << st[i] << endl;
      REP(i, 5) cout << st[itr - begin(st) - 5 + i] << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

int main() {
  while (1) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    int l = s.size();
    vector<string> st;
    REP(i, 1 << l) {
      string cp = s;
      bool ok = true;
      REP(j, l) {
        if ((i >> j) & 1) {
          if (cp[j] == 'z') {
            ok = false;
            break;
          }
          cp[j]++;
        }
      }
      if (!ok) {
        i += i & (-i);
        --i;
        continue;
      }
      vector<bool> isc(26, false);
      REP(j, l) {
        if (!isc[cp[j] - 'a'] && cp[j] != 'a') {
          if (((i >> j) & 1) == 0) {
            ok = false;
            break;
          }
          isc[cp[j] - 'a'] = true;
        }
      }
      if (!ok)
        continue;
      st.push_back(cp);
    }
    sort(begin(st), end(st));
    auto itr = unique(begin(st), end(st));
    cout << (itr - begin(st)) << endl;
    if ((itr - begin(st)) < 10) {
      REP(i, itr - begin(st)) cout << st[i] << endl;
    } else {
      REP(i, 5) cout << st[i] << endl;
      REP(i, 5) cout << st[itr - begin(st) - 5 + i] << endl;
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;

string str;
bool change[30];
vector<string> en;
vector<string> fr;

void solve() {
  int n = str.size();
  int ans = 0;
  fr.clear();
  en.clear();
  string s, tmp;
  rep(i, (1 << n)) {
    int I = i;
    s = str;
    memset(change, 0, 30);
    bool ok = true;
    rep(j, n) {
      if (I & 1) {
        if (s[j] == 'z') {
          ok = false;
          break;
        }
        s[j] += 1;
      }
      I >>= 1;
    }
    if (!ok)
      continue;
    tmp = s;
    rep(j, n) {
      if (change[s[j] - 'a'] == false && s[j] != 'a') {
        change[s[j] - 'a'] = true;
        s[j]--;
      }
      if (str[j] != s[j]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans++;
      fr.push_back(tmp);
      en.push_back(tmp);
      sort(fr.begin(), fr.end());
      sort(en.begin(), en.end(), greater<string>());
      if (fr.size() > 10) {
        fr.pop_back();
        en.pop_back();
      }
    }
  }
  cout << ans << endl;
  if (ans <= 10) {
    rep(i, ans) { cout << fr[i] << endl; }
  } else {
    rep(i, 5) cout << fr[i] << endl;
    rep(i, 5) cout << en[4 - i] << endl;
  }
}

int main() {
  while (cin >> str) {
    if (str == "#")
      break;
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;

string str;
bool change[30];
priority_queue<string, vector<string>> en;
priority_queue<string, vector<string>, greater<string>> fr;

void solve() {
  int n = str.size();
  rep(i, n) if (str[i] == 'z') {
    puts("0");
    return;
  }
  int ans = 0;
  while (fr.size())
    fr.pop();
  while (en.size())
    en.pop();
  string s, tmp;
  rep(i, (1 << n)) {
    int I = i;
    s = str;
    memset(change, 0, 30);
    rep(j, n) {
      if (I & 1)
        s[j] += 1;
      I >>= 1;
    }
    tmp = s;
    rep(j, n) {
      if (s[j] == 'a')
        continue;
      if (change[s[j] - 'a'])
        continue;
      change[s[j] - 'a'] = true;
      s[j]--;
    }
    if (str == s) {
      ans++;
      fr.push(tmp);
      en.push(tmp);
      if (fr.size() > 10) {
        fr.pop();
        en.pop();
      }
    }
  }
  cout << ans << endl;
  if (ans <= 10) {
    rep(i, ans) {
      string tmp = fr.top();
      fr.pop();
      cout << tmp << endl;
    }
  } else {
    string tmp;
    rep(i, 5) {
      tmp = fr.top();
      fr.pop();
      cout << tmp << endl;
    }
    string x[5];
    rep(i, 5) {
      x[i] = en.top();
      en.pop();
    }
    rep(i, 5) cout << x[4 - i] << endl;
  }
}

int main() {
  while (cin >> str) {
    if (str == "#")
      break;
    solve();
  }
}
Problem p00776 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

vector<string> ans;
string str;

void solve(int idx, string s, string used) {
  // cout << idx << " " << s << " " << used << endl;
  if (idx == str.size()) {
    ans.push_back(s);
    return;
  }
  if (str[idx] == 'a') {
    solve(idx + 1, s + str[idx], used);
    if (used.find(str[idx]) == -1)
      solve(idx + 1, s + (char)(str[idx] + 1), used + str[idx]);
    return;
  }
  if (str[idx] != 'z' && used.find(str[idx]) == -1) {
    solve(idx + 1, s + (char)(str[idx] + 1), used + str[idx]);
  }
  if (s.find(str[idx]) != -1) {
    solve(idx + 1, s + str[idx], used);
  }

  return;
}

int main() {
  while (cin >> str, str != "#") {
    string non = "";
    solve(0, non, non);
    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    cout << ans.size() << endl;
    if (ans.size() <= 5) {
      for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < ans.size(); i++) {
        if (i < 5 || ans.size() - 5 <= i)
          cout << ans[i] << endl;
      }
    }
    ans.clear();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<string> ans;
string str;

void solve(int idx, string s, string used) {
  if (idx == str.size()) {
    ans.push_back(s);
    return;
  }
  if (str[idx] < 'z') {
    if (str[idx] == 'a') {
      solve(idx + 1, s + str[idx], used);
      solve(idx + 1, s + (char)(str[idx] + 1), used + str[idx]);
      return;
    }
    if (used.find(str[idx]) == -1) {
      solve(idx + 1, s + (char)(str[idx] + 1), used + str[idx]);
    }
    if (s.find(str[idx]) != -1) {
      solve(idx + 1, s + str[idx], used);
    }
  }

  return;
}

int main() {
  while (cin >> str, str != "#") {
    string non = "";
    solve(0, non, non);
    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    cout << ans.size() << endl;
    if (ans.size() <= 5) {
      for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < ans.size(); i++) {
        if (i < 5 || ans.size() - 5 <= i)
          cout << ans[i] << endl;
      }
    }
    ans.clear();
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (string S;;) {
    cin >> S;
    if (S == "#") {
      break;
    }
    const int sz = S.size();
    const int maximum = 1 << sz;
    set<string, greater<string>> f;
    set<string> t;
    int ans = 0;
    for (int i = 0; i < maximum; i++) {
      string s = S;
      for (int j = 0; j < sz; j++) {
        if (i & (1 << j)) {
          s[j]++;
        }
      }
      string tmp = s;
      bool used[26]{true};
      bool ok = true;
      for (int j = 0; j < sz; j++) {
        if (not used[tmp[j] - 'a']) {
          used[tmp[j] - 'a'] = true, tmp[j]--;
        }
        if (tmp[j] != S[j]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        ans++, f.insert(s), t.insert(s);
        if (f.size() > 10) {
          f.erase(f.begin()), t.erase(t.begin());
        }
      }
    }
    cout << ans << endl;
    if (ans <= 10) {
      for (auto it = f.rbegin(); it != f.rend(); it++) {
        cout << *it << "\\n";
      }
    } else {
      vector<string> ans;
      auto it = f.rbegin();
      for (int i = 0; i < 5; i++, it++) {
        ans.push_back(*it);
      }
      it = t.rbegin();
      for (int i = 0; i < 5; i++, it++) {
        ans.push_back(*it);
      }
      sort(ans.begin(), ans.end());
      for (const string &s : ans) {
        cout << s << "\\n";
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (string S;;) {
    cin >> S;
    if (S == "#") {
      break;
    }
    const int sz = S.size();
    const int maximum = 1 << sz;
    set<string, greater<string>> f;
    set<string> t;
    int ans = 0;
    for (int i = 0; i < maximum; i++) {
      string s = S;
      for (int j = 0; j < sz; j++) {
        if (i & (1 << j)) {
          s[j]++;
        }
      }
      string tmp = s;
      bool used[26]{true};
      for (int j = 0; j < sz; j++) {
        if (not used[tmp[j] - 'a']) {
          used[tmp[j] - 'a'] = true, tmp[j]--;
        }
      }
      if (tmp == S) {
        ans++, f.insert(s), t.insert(s);
        if (f.size() > 10) {
          f.erase(f.begin()), t.erase(t.begin());
        }
      }
    }
    cout << ans << endl;
    if (ans < 10) {
      for (const string &s : f) {
        cout << s << "\\n";
      }
    } else {
      vector<string> ans;
      auto it = f.rbegin();
      for (int i = 0; i < 5; i++, it++) {
        ans.push_back(*it);
      }
      it = t.rbegin();
      for (int i = 0; i < 5; i++, it++) {
        ans.push_back(*it);
      }
      sort(ans.begin(), ans.end());
      for (const string &s : ans) {
        cout << s << "\\n";
      }
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

bool f[26];
vector<string> ans;
string str;
void dfs(int pos) {
  if (pos >= str.size()) {
    ans.push_back(str);
  } else {
    char c = str[pos];
    int cid = (int)(c - 'a');
    if (f[(cid + 25) % 26]) {
      dfs(pos + 1);
    }
    if (!f[cid]) {
      ++str[pos];
      f[cid] = true;
      dfs(pos + 1);
      f[cid] = false;
      --str[pos];
    }
  }
}
int main() {
  while (cin >> str, str != "#") {
    ans.clear();
    fill(&f[0], &f[0] + 26, false);
    f[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: MLE

Fixed submission:

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

using namespace std;

bool f[26];
vector<string> ans;
string str;
void dfs(int pos) {
  if (pos >= str.size()) {
    ans.push_back(str);
  } else {
    char c = str[pos];
    int cid = (int)(c - 'a');
    if (f[(cid + 25) % 26]) {
      dfs(pos + 1);
    }
    if (!f[cid]) {
      ++str[pos];
      dfs(pos + 1);
      --str[pos];
    }
  }
}
int main() {
  while (cin >> str, str != "#") {
    ans.clear();
    fill(&f[0], &f[0] + 26, false);
    f[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;
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

set<string> st;
string s;
int n;

void dfs(int i, string ns) {
  if (i == n) {
    st.insert(ns);
    return;
  }
  int nc = s[i] - 'a';
  bool used[30];
  memset(used, 0, sizeof(used));
  rep(j, i) { used[ns[j] - 'a'] = true; }
  if (s[i] != 'z' && !used[nc + 1])
    dfs(i + 1, ns + (char)(s[i] + 1));
  if (s[i] == 'a' || used[nc])
    dfs(i + 1, ns + s[i]);
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> s;
    n = s.length();
    if (s == "#")
      break;
    st.clear();
    dfs(0, "");
    cout << st.size() << endl;
    int cnt = 0;
    each(i, st) {
      if (cnt < 5 || st.size() - cnt <= 5)
        cout << i << endl;
      cnt++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <string>

#define MAX_L 20
#define MAX_ANS 50000

using namespace std;

string str;
int L;
string ans[MAX_ANS];

string encrypt(string s) {
  string a = s;
  for (char c = 'b'; c <= 'z'; c++) {
    for (int i = 0; i < s.length(); i++) {
      if (a[i] == c) {
        a[i]--;
        break;
      }
    }
  }
  return a;
}

void solve() {
  int count = 0;
  L = str.length();
  for (int i = 0; i < pow(2, L); i++) {
    string tmp, tmp2;
    for (int j = 0; j < L; j++) {
      tmp += str[j] + ((i >> j) & 1);
    }
    tmp2 = encrypt(tmp);
    if (tmp2 == str) {
      ans[count] = tmp;
      count++;
    }
  }
  sort(ans, ans + count);
  cout << count << endl;
  if (count > 10) {
    for (int i = 0; i < 5; i++)
      cout << ans[i] << endl;
    for (int i = count - 5; i < count; i++)
      cout << ans[i] << endl;
  } else {
    for (int i = 0; i < count; i++)
      cout << ans[i] << endl;
  }
  return;
}

int main() {
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    solve();
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9

using vd = vector<double>;
using vvd = vector<vd>;

int main() {
  string s;
  while (cin >> s, s != "#") {
    vs ans;
    rep(c, (1 << s.size())) { // 2^20
      vector<bool> cng(26, false);
      cng[25] = true;
      vector<bool> n(26, true);
      string as = s, cs = s;
      rep(a, s.size()) { // 20
        if (c & (1 << a)) {
          if (cng[cs[a] - 'a'])
            goto end;
          cng[cs[a] - 'a'] = true;
          cs[a]++;
        }
        as[a] = cs[a];
        if (n[as[a] - 'a'] && as[a] != 'a') {
          n[as[a] - 'a'] = false;
          as[a]--;
          if (s[a] != as[a]) {
            goto end;
          }
        }
      }
      ans.pb(cs);
    end:;
    }
    sort(all(ans));
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      for (auto a : ans)
        cout << a << endl;
    } else {
      rep(i, 5) cout << ans[i] << endl;
      range(i, ans.size() - 5, ans.size()) cout << ans[i] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9

using vd = vector<double>;
using vvd = vector<vd>;

int main() {
  string s;
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> s, s != "#") {
    vs ans;
    rep(c, (1 << s.size())) { // 2^20
      vi n(26, 0);
      string as = s, cs = s;
      rep(a, s.size()) { // 20
        if (c & (1 << a)) {
          if (cs[a] == 'z')
            goto end;
          cs[a]++;
        }
        as[a] = cs[a];
        if (n[as[a] - 'a'] == 0 && as[a] != 'a') {
          n[as[a] - 'a'] = 1;
          as[a]--;
        }
      }
      if (as == s) {
        ans.pb(cs);
      }

    end:;
    }
    sort(all(ans));
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      for (auto a : ans)
        cout << a << endl;
    } else {
      rep(i, 5) cout << ans[i] << endl;
      range(i, ans.size() - 5, ans.size()) cout << ans[i] << endl;
    }
  }
  return 0;
}