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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n;
set<string> tbl;
bool conv(char s[30], char t[30]) {
  bool used[26] = {};
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    if (!used[c] && c) {
      used[c] = true;
      if (s[i] - 1 != t[i])
        return false;
    } else if (s[i] != t[i])
      return false;
  }
  return true;
}
int main() {
  //  string s;
  char s[30];
  while (cin >> s, s[0] != '#') {
    tbl.clear();
    n = strlen(s);
    vector<string> ans;
    for (int i = 0; i < 1 << n; ++i) {
      char t[30];
      for (int j = 0; j < n; ++j) {
        if ((i >> j) & 1 && s[j] < 'z')
          t[j] = s[j] + 1;
        else
          t[j] = s[j];
      }
      if (conv(t, s))
        tbl.insert(string(t, n));
    }

    for (set<string>::iterator it = tbl.begin(); it != tbl.end(); ++it)
      ans.push_back((*it));
    int m = ans.size();
    cout << m << endl;
    if (m <= 10) {
      for (int i = 0; i < m; ++i)
        cout << ans[i] << endl;
    } else {
      for (int i = 0; i < 5; ++i)
        cout << ans[i] << endl;
      for (int i = m - 5; i < m; ++i)
        cout << ans[i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

string conv(string s) {
  int n = s.length();
  bool used[26] = {};
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    if (!used[c] && c) {
      used[c] = true;
      s[i]--;
    }
  }
  return s;
}
int main() {
  string s;
  while (cin >> s, s != "#") {
    int n = s.length();
    vector<string> ans;
    for (int i = 0; i < 1 << n; ++i) {
      string t = s;
      for (int j = 0; j < n; ++j) {
        if ((i >> j) & 1 && t[j] < 'z') {
          t[j]++;
        }
      }
      if (conv(t) == s)
        ans.push_back(t);
    }

    sort(ans.begin(), ans.end());
    int m = ans.size();
    cout << m << endl;
    if (m <= 10) {
      for (int i = 0; i < m; ++i)
        cout << ans[i] << endl;
    } else {
      for (int i = 0; i < 5; ++i)
        cout << ans[i] << endl;
      for (int i = m - 5; i < m; ++i)
        cout << ans[i] << endl;
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

string s;
vector<string> ans;
void dfs(int idx, int S) {
  if (idx == s.size()) {
    ans.push_back(s);
    return;
  }
  int t = s[idx] - 'a';
  if (S >> t & 1) {
    dfs(idx + 1, S);
  }
  if (!(S >> (t + 1) & 1)) {
    if (t != 25) {
      s[idx]++;
      dfs(idx + 1, S | 1 << (t + 1));
      s[idx]--;
    }
  }
}

int main() {
  while (cin >> s) {
    if (s == "#")
      break;
    ans.clear();
    dfs(0, 1);
    sort(all(ans));
    cout << ans.size() << endl;
    if (ans.size() > 10) {
      rep(i, 5) cout << ans[i] << endl;
      rep(i, 5) cout << ans[ans.size() + i - 5] << endl;
    } else
      for (auto e : ans)
        cout << e << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

string s;
vector<string> ans;
void dfs(int idx, int S) {
  if (idx == s.size()) {
    ans.push_back(s);
    return;
  }
  int t = s[idx] - 'a';
  if (S >> t & 1) {
    dfs(idx + 1, S);
    if (t != 25) {
      s[idx]++;
      dfs(idx + 1, S | 1 << (t + 1));
      s[idx]--;
    }
  } else {
    if (t == 0) {
      dfs(idx + 1, S);
    }
    if (t != 25) {
      s[idx]++;
      dfs(idx + 1, S | 1 << (t + 1));
      s[idx]--;
    }
  }
}

int main() {
  while (cin >> s) {
    if (s == "#")
      break;
    ans.clear();
    dfs(0, 0);
    sort(all(ans));
    cout << ans.size() << endl;
    if (ans.size() >= 10) {
      rep(i, 5) cout << ans[i] << endl;
      reverse(all(ans));
      rep(i, 5) cout << ans[i] << endl;
    } else
      for (auto e : ans)
        cout << e << endl;
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

string S;

inline bool ok2(string T) {
  bool used[26];

  for (int i = 0; i < 26; i++)
    used[i] = false;

  for (int i = 0; i < S.size(); i++) {
    if (!used[T[i] - 97] && T[i] != 'a') {
      used[T[i] - 97] = true;
      T[i]--;
    }
  }

  return S == T;
}

int main() {
  while (true) {
    cin >> S;

    if (S == "#")
      break;

    vector<string> lists;

    for (int i = 0; i < (1 << S.size()); i++) {
      string T = S;
      bool ok = true;

      for (int j = 0; j < S.size(); j++) {
        if (i & (1 << j)) {
          if (S[j] == 'z') {
            ok = false;
            break;
          }

          T[j]++;
        }
      }

      if (ok) {
        if (ok2(T)) {
          lists.push_back(T);
        }
      }
    }

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

    printf("%u\\n", lists.size());

    if (lists.size() < 10) {
      for (int i = 0; i < lists.size(); i++) {
        printf("%s\\n", lists[i].c_str());
      }
    } else {
      for (int i = 0; i < 5; i++) {
        printf("%s\\n", lists[i].c_str());
      }

      for (int i = lists.size() - 5; i < lists.size(); i++) {
        printf("%s\\n", lists[i].c_str());
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

string S;

bool ok2(string T) {
  bool used[26];

  for (int i = 0; i < 26; i++)
    used[i] = false;

  for (int i = 0; i < S.size(); i++) {
    if (!used[T[i] - 97] && T[i] != 'a') {
      used[T[i] - 97] = true;
      T[i]--;
    }
  }

  return S == T;
}

int main() {
  while (true) {
    cin >> S;

    if (S == "#")
      break;

    vector<string> lists;

    for (int i = 0; i < (1 << S.size()); i++) {
      string T = S;
      bool ok = true;

      for (int j = 0; j < S.size(); j++) {
        if (i & (1 << j)) {
          if (S[j] == 'z') {
            ok = false;
            break;
          }

          T[j]++;
        }
      }

      if (ok) {
        if (ok2(T)) {
          lists.push_back(T);
        }
      }
    }

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

    printf("%u\\n", lists.size());

    if (lists.size() < 10) {
      for (int i = 0; i < lists.size(); i++) {
        printf("%s\\n", lists[i].c_str());
      }
    } else {
      for (int i = 0; i < 5; i++) {
        printf("%s\\n", lists[i].c_str());
      }

      for (int i = lists.size() - 5; i < lists.size(); i++) {
        printf("%s\\n", lists[i].c_str());
      }
    }
  }

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  for (string s; cin >> s && s != "#";) {
    int n = s.size();

    vector<string> res;
    string t = s;
    array<char, 26> first;
    for (int i = 0; i < (1 << n); i++) {
      bool ok = true;
      for (int j = 0; j < n; j++)
        if (i >> j & 1)
          ok &= s[j] != 'z';
      if (!ok)
        continue;

      for (int j = 0; j < n; j++)
        t[j] = s[j] + (i >> j & 1);
      first.fill(-1);
      for (int j = s.size(); j--;)
        first[t[j] - 'a'] = j;
      for (int j = 1; j < 26; j++)
        if (first[j] != -1)
          t[first[j]]--;

      if (t == s) {
        for (int j = 0; j < n; j++)
          t[j] = s[j] + (i >> j & 1);
        res.push_back(t);
      }
    }
    sort(begin(res), end(res));

    cout << res.size() << endl;
    if (res.size() > 10)
      res.erase(begin(res) + 5, end(res) - 5);
    for (string t : res)
      cout << t << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  // clock_t t0=clock();
  for (string s; cin >> s && s != "#";) {
    int n = s.size();

    vector<string> res;
    string t = s;
    array<int, 26> first;
    for (int i = 0; i < (1 << n); i++) {
      bool ok = true;
      for (int j = 0; j < n; j++)
        if (i >> j & 1)
          ok &= s[j] != 'z';
      if (!ok)
        continue;

      for (int j = 0; j < n; j++)
        t[j] = s[j] + (i >> j & 1);
      first.fill(-1);
      for (int j = s.size(); j--;)
        first[t[j] - 'a'] = j;
      for (int j = 1; j < 26; j++)
        if (first[j] != -1)
          t[first[j]]--;

      if (t == s) {
        for (int j = 0; j < n; j++)
          t[j] = s[j] + (i >> j & 1);
        res.push_back(t);
      }
    }
    sort(begin(res), end(res));

    cout << res.size() << endl;
    if (res.size() > 10)
      res.erase(begin(res) + 5, end(res) - 5);
    for (string t : res)
      cout << t << endl;
  }
  // printf("%f\\n",double(clock()-t0)/CLOCKS_PER_SEC);
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string s, t;
char idx[128];
vector<string> res;

void rec(int k) {
  if (k == t.size()) {
    res.push_back(t);
    return;
  }
  if (t[k] == 'a' || idx[t[k]] != -1) {
    rec(k + 1);
  }
  if (t[k] != 'z' && idx[t[k] + 1] == -1) {
    ++t[k];
    idx[t[k]] = k;
    rec(k + 1);
    idx[t[k]] = -1;
    --t[k];
  }
}

int main() {
  for (; cin >> s && s != "#";) {
    res.clear();
    t = s;
    memset(idx, -1, sizeof(idx));
    rec(0);
    sort(res.begin(), res.end());
    cout << res.size() << endl;
    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 <bits/stdc++.h>
using namespace std;

int main() {
  for (string s; cin >> s && s != "#";) {

    vector<string> res;

    for (int b = 0; b < (1 << s.size()); ++b)
      try {
        string t = s;
        for (int i = 0; i < t.size(); ++i) {
          if (b >> i & 1) {
            if (t[i] == 'z')
              throw 0;
            ++t[i];
          }
        }
        string u = t;
        for (char c = 'b'; c <= 'z'; ++c) {
          for (int i = 0; i < u.size(); ++i) {
            if (u[i] == c) {
              --u[i];
              break;
            }
          }
        }
        if (u == s) {
          res.push_back(t);
        }
      } catch (...) {
      }

    sort(res.begin(), res.end());
    cout << res.size() << endl;
    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: Memory Limit Exceeded

// AOJ 1195 Encryption System
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int c;
string buf;
vector<string> vs;

void check(string &s) {
  for (char ch = 'b'; ch <= 'z'; ++ch) {
    int pos = s.find(ch);
    if (pos != string::npos) {
      s[pos] = ch - 1;
    }
  }
}

void gen(string s, int idx) {
  for (char ch = 'b'; ch <= 'z'; ++ch) {
    int pos = s.find(ch);
    if (pos != string::npos) {
      if (s[pos] - 1 != buf[pos])
        return;
    }
  }
  if (s.length() == buf.length()) {
    ++c;
    vs.emplace_back(s);
    check(s);
    if (s != buf) {
      vs.erase(vs.end());
      --c;
    }
    if (vs.size() > 10) {
      vs.erase(vs.begin() + 5);
    }
    return;
  } else {
    char ch = buf[idx] + 1;
    gen(s + buf[idx], idx + 1);
    if (ch <= 'z')
      gen(s + ch, idx + 1);
  }
}

int main() {
  while (cin >> buf) {
    c = 0;
    if (buf == "#")
      break;
    gen("", 0);
    sort(vs.begin(), vs.end());
    auto vit = vs.begin();
    cout << c << endl;
    for (; vit != vs.end(); ++vit) {
      cout << *vit << endl;
    }
    vs.clear();
  }
  return 0;
}

Error: MLE

Fixed submission:

// AOJ 1195 Encryption System
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

string buf;
vector<string> vs;

void gen(string s, int idx) {
  for (char ch = 'b'; ch <= 'z'; ++ch) {
    int pos = s.find(ch);
    if (pos != string::npos) {
      if (s[pos] - 1 != buf[pos])
        return;
    }
  }
  if (s.length() == buf.length()) {
    vs.emplace_back(s);
    return;
  } else {
    char ch = buf[idx] + 1;
    gen(s + buf[idx], idx + 1);
    if (ch <= 'z')
      gen(s + ch, idx + 1);
  }
}

int main() {
  while (cin >> buf) {
    if (buf == "#")
      break;
    gen("", 0);
    sort(vs.begin(), vs.end());
    auto vit = vs.begin();
    cout << vs.size() << endl;
    if (vs.size() <= 10) {
      for (; vit != vs.begin() + vs.size(); ++vit) {
        cout << *vit << endl;
      }
    } else {
      for (; vit != vs.begin() + 5; ++vit) {
        cout << *vit << endl;
      }
      for (vit = vs.end() - 5; vit < vs.end(); ++vit) {
        cout << *vit << endl;
      }
    }
    vs.clear();
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <string>

using namespace std;

int strlength;

bool check(string str, int k) {
  int count[256] = {0}, d = 0;
  count['a']++;
  count['z' + 1]++;
  for (int i = 0; i < strlength; i++) {
    if (!count[str[i] + ((k & (1 << i)) ? 1 : 0)]++)
      d |= (1 << i);
  }
  return k == d;
}

int main() {
  string str;
  while (cin >> str, str[0] != '#') {
    set<string> ans;
    set<string>::iterator it;
    strlength = str.length();
    for (int k = 0; k < (1 << strlength); k++) {
      if (check(str, k)) {
        string str2 = str;
        for (int l = 0; l < strlength; l++) {
          if (k & (1 << l) && str[l] != 'z') {
            str2[l]++;
          }
        }
        ans.insert(str2);
      }
    }
    // ??????
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      for (it = ans.begin(); it != ans.end(); it++)
        cout << *it << endl;
    } else {
      int count = 0;
      for (it = ans.begin(); count < 5; it++, count++)
        cout << *it << endl;
      for (it = ans.end(); count > 0; it--, count--) {
      }
      for (; count < 5; it++, count++)
        cout << *it << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>

using namespace std;

int strlength;

bool check(string str, string str2, int k) {
  int count[256] = {0}, d = 0;
  count['a']++;
  count['z' + 1]++;
  //	for(char a='b'; a<='z'; a++){
  for (int i = 0; i < strlength; i++) {
    char a = str[i] + ((k & (1 << i)) ? 1 : 0);
    if (!count[a]) {
      // str2[i] = a-1;
      d |= (1 << i);
      count[a]++;
      //				break;
    }
  }
  //	}
  return k == d;
}

int main() {
  string str;
  while (cin >> str, str[0] != '#') {
    set<string> ans;
    set<string>::iterator it;
    strlength = str.length();
    for (int k = 0; k < (1 << strlength); k++) {
      string str2 = str;
      //			char *tmp = str2.c_str();
      if (check(str, str2, k)) {
        for (int l = 0; l < strlength; l++) {
          if (k & (1 << l) && str[l] != 'z') {
            str2[l]++;
          }
        }
        ans.insert(str2);
      }
    }
    // ??????
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      for (it = ans.begin(); it != ans.end(); it++)
        cout << *it << endl;
    } else {
      int count = 0;
      for (it = ans.begin(); count < 5; it++, count++)
        cout << *it << endl;
      for (it = ans.end(); count > 0; it--, count--) {
      }
      for (; count < 5; it++, count++)
        cout << *it << endl;
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
set<string> ans;
bool used['z' + 1];
string s, cp;
int n;

void dfs(int x, string t) {
  cp = t;
  memset(used, 0, sizeof(used));
  for (int i = 0; i < x; i++)
    if (cp[i] != 'a' && !used[cp[i]])
      used[cp[i]] = true, cp[i]--;
  if (x == n) {
    if (cp == s)
      ans.insert(t);
    return;
  } else if (cp != s.substr(0, x))
    return;
  dfs(x + 1, t + s[x]);
  if (s[x] != 'z')
    dfs(x + 1, t + (char)(s[x] + 1));
}

int main() {
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    n = s.size();
    dfs(0, "");
    set<string>::iterator ite;
    int len = ans.size(), cnt = 0;
    cout << len << endl;
    for (ite = ans.begin(); ite != ans.end(); ite++, cnt++)
      if (cnt <= 4 || cnt + 5 >= len)
        cout << (*ite) << endl;
    ans.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
set<string> ans;
string s;
int n;

void dfs(int x, string t) {
  if (x == n) {
    int used['z' + 1];
    string cp = t;
    memset(used, 0, sizeof(used));
    for (int i = 0; i < n; i++)
      if (t[i] != 'a' && !used[t[i]])
        used[t[i]] = true, t[i]--;
    if (s == t)
      ans.insert(cp);
    return;
  }
  dfs(x + 1, t + s[x]);
  dfs(x + 1, t + (char)(s[x] + 1));
}

int main() {
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    n = s.size();
    dfs(0, "");
    set<string>::iterator ite;
    int len = ans.size(), cnt = 0;
    cout << len << endl;
    for (ite = ans.begin(); ite != ans.end(); ite++) {
      if (cnt <= 4 || cnt + 5 >= len)
        cout << (*ite) << endl;
      cnt++;
    }
    ans.clear();
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<string> V;
string s;
int alph[26] = {};

bool Check(string b) {
  bool al[26] = {};
  for (int i = 0; i < b.length(); i++) {
    if (b[i] != 'a' && !al[b[i] - 'a']) {
      al[b[i] - 'a'] = true;
      b[i]--;
    }
  }
  if (b == s)
    return true;
  else
    return false;
}

void dfs(int a, string b) {
  if (a == s.length()) {
    if (!Check(b))
      return;
    V.push_back(b);
  } else {
    if (alph[b[a] - 'a'] == 1)
      dfs(a + 1, b);
    if (b[a] == 'a')
      dfs(a + 1, b);
    if (b[a] != 'z')
      b[a]++;
    if (alph[b[a] - 'a'] == 0) {
      alph[b[a] - 'a'] = 1;
      dfs(a + 1, b);
      alph[b[a] - 'a'] = 0;
    }
  }
}

int main() {
  while (1) {
    cin >> s;
    if (s[0] == '#')
      break;

    for (int i = 0; i < 26; i++)
      alph[i] = 0;
    V.clear();
    dfs(0, s);
    sort(V.begin(), V.end());

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

Error: TLE

Fixed submission:

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

vector<string> V;
string s;
int alph[26] = {};

bool Check(string b) {
  bool al[26] = {};
  for (int i = 0; i < b.length(); i++) {
    if (b[i] != 'a' && !al[b[i] - 'a']) {
      al[b[i] - 'a'] = true;
      b[i]--;
    }
  }
  if (b == s)
    return true;
  else
    return false;
}

bool C(int a, string b) {
  if (a == 0)
    return true;
  for (int i = 0; i < a - 1; i++) {
    if (b[i] == b[a])
      return false;
  }
  return true;
}
void dfs(int a, string b) {
  if (a == s.length()) {
    if (!Check(b))
      return;
    V.push_back(b);
  } else {
    dfs(a + 1, b);
    if (C(a, s)) {
      if (b[a] != 'z') {
        b[a]++;
        dfs(a + 1, b);
      }
    }
  }
}

int main() {
  while (1) {
    cin >> s;
    if (s[0] == '#')
      break;

    V.clear();
    dfs(0, s);
    sort(V.begin(), V.end());

    cout << V.size() << endl;
    if (V.size() < 11)
      for (int i = 0; i < V.size(); i++)
        cout << V[i] << endl;
    else {
      for (int i = 0; i < 5; i++)
        cout << V[i] << endl;
      for (int i = V.size() - 5; i < V.size(); i++)
        cout << V[i] << endl;
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

signed main() {

  while (1) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    int n = (int)s.size();
    vector<string> ans;
    for (int bit = 0; bit < (1 << n); bit++) {

      int st[26] = {};
      bool ok = true;
      string p = "";
      for (int i = 0; i < n; i++) {
        char t = s[i];
        if (bit & (1 << i) && t != 'z') {
          t = t + 1;
        }

        p = p + t;

        if (t != 'a' && st[(int)(t - 'a')] == 0) {
          st[(int)(t - 'a')] = 1;
          t = t - 1;
        }
        if (t != s[i]) {
          ok = false;
          break;
        }
      }

      if (ok)
        ans.push_back(p);
    }

    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    int m = (int)ans.size();
    cout << m << endl;
    for (int i = 0; i < m; i++) {
      if (i < 5 || i >= m - 5)
        cout << ans[i] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

bool f(string t, string s) {
  int n = (int)t.size();
  int st[26] = {};
  for (int i = 0; i < n; i++) {
    if (t[i] == 'a')
      continue;
    if (st[(int)(t[i] - 'a')] == 0) {
      st[(int)(t[i] - 'a')] = 1;
      t[i] = t[i] - 1;
    }

    if (s[i] != t[i])
      return false;
  }

  return s == t;
}

signed main() {

  while (1) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    int n = (int)s.size();
    vector<string> ans;
    for (int bit = 0; bit < (1 << n); bit++) {
      string t = s;
      for (int j = 0; j < n; j++) {
        if (bit & (1 << j)) {
          if (t[j] != 'z')
            t[j] = t[j] + 1;
        }
      }

      if (f(t, s))
        ans.push_back(t);
    }

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