Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

enum { A = 0, Z = 25 };
int main() {
  string s_str;
  while (cin >> s_str && s_str != "#") {
    int n = s_str.length();
    vector<int> s(n), t;
    for (int i = 0; i < n; i++)
      s[i] = s_str[i] - 'a';
    vector<vector<int>> res;
    int cnt = 0;
    int seen, past, newl;
    for (int mask = 0; mask < (1 << n); mask++) {
      t = s;
      seen = past = newl = 0;
      bool invalid = false;
      for (int i = 0; i < n; i++) {
        if (s[i] != A && seen & (1 << s[i])) {
          if (!(newl & (1 << s[i]))) {
            invalid = true;
            break;
          }
        }
        if (s[i] == Z) {
          if (mask & (1 << (n - 1 - i))) {
            invalid = true;
            break;
          } else {
            if (!(newl & (1 << Z))) {
              invalid = true;
              break;
            }
          }
        } else {
          if (mask & (1 << (n - 1 - i))) {
            if (past & (1 << s[i])) {
              invalid = true;
              break;
            } else {
              t[i]++;
              past |= 1 << s[i];
              newl |= 1 << t[i];
            }
          } else if (s[i] != A) {
            if (!(past & (1 << s[i])) && !(newl & (1 << s[i]))) {
              invalid = true;
              break;
            }
          }
        }
        seen |= 1 << s[i];
      }
      if (invalid)
        continue;
      cnt++;
      res.emplace_back(t);
      if (res.size() == 11)
        res.erase(res.begin() + 5);
    }
    cout << cnt << endl;
    for (auto v : res) {
      for (int i : v)
        cout << (char)(i + 'a');
      cout << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

string encode(string s) {
  for (char tgt = 'b'; tgt <= 'z'; tgt++) {
    for (char &ci : s)
      if (ci == tgt) {
        --ci;
        break;
      }
  }
  return s;
}
string hoge(int i) {
  string s = "a";
  s[0] += i;
  return s;
}
#include <map>
void test() {
#define rep(i) for (int i = 0; i < 26; i++)
  map<string, vector<string>> m;
  rep(i) rep(j) rep(k) {
    string s = hoge(i) + hoge(j) + hoge(k);
    m[encode(s)].emplace_back(s);
  }
  for (auto msv : m) {
    cout << "[*] " << msv.first << endl;
    for (auto s : msv.second)
      cout << s << " ";
    cout << endl;
  }
}

int main() {
  string s;
  while (cin >> s && s != "#") {
    int n = s.length();
    vector<string> res;
    int cnt = 0;
    for (int mask = 0; mask < (1 << n); mask++) {
      string t = s;
      bool valid = true;
      vector<bool> seen(26, 0), past(26, 0), left(26, 0);
      for (int i = 0; i < n; i++) {
        if (s[i] == 'z') {
          // z は変更できない
          // 変更後の左側に z がないとそのまま残せない
          if (mask & (1 << (n - 1 - i))) {
            valid = false;
          } else {
            if (!left['z' - 'a'])
              valid = false;
          }
        } else {
          // z 以外の変更は高々 1 回
          // a 以外は，変更済み or 変更後の左側に同じ文字があるときのみ，
          // 変更しなくてよい
          if (mask & (1 << (n - 1 - i))) {
            if (past[s[i] - 'a']) {
              valid = false;
            } else {
              t[i]++;
              past[s[i] - 'a'] = true;
            }
          } else if (s[i] != 'a') {
            if (!past[s[i] - 'a'] && !left[s[i] - 'a']) {
              valid = false;
            }
          }
        }
        seen[s[i] - 'a'] = true;
        left[t[i] - 'a'] = true;
      }
      if (!valid)
        continue;
      cnt++;
      res.emplace_back(t);
      if (res.size() == 11)
        res.erase(res.begin() + 5);
    }
    cout << cnt << endl;
    for (string s : res)
      cout << s << endl;
  }
  return 0;
}