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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
const int mod = 1e9 + 7;
ll power(ll a, ll b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  char c[20];
  while (scanf("%s", c)) {
    if (c[0] == '#')
      break;
    string s = string(c);
    vector<string> v;
    rep(i, (1 << s.size())) {
      bool invalid = false;
      string t = s;
      rep(j, s.size()) {
        if (i & (1 << j)) {
          if (t[j] == 'z') {
            invalid = true;
            break;
          }
          t[j]++;
        }
      }
      if (invalid)
        continue;
      string u = t;
      int table[26] = {0};
      rep(i, u.size()) {
        if (u[i] == 'a')
          continue;
        if (table[u[i] - 'a'])
          continue;
        table[u[i] - 'a'] = 1;
        u[i]--;
      }
      if (u != s)
        continue;
      v.push_back(t);
    }
    printf("%d\\n", (int)v.size());
    sort(v.begin(), v.end());
    if (v.size() <= 10) {
      rep(i, v.size()) printf("%s\\n", v[i].c_str());
    } else {
      rep(i, 5) printf("%s\\n", v[i].c_str());
      rep(i, 5) printf("%s\\n", v[v.size() - 5 + i].c_str());
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
const int mod = 1e9 + 7;
ll power(ll a, ll b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  char c[20];
  while (scanf("%s", c)) {
    if (c[0] == '#')
      break;
    string s = string(c);
    if (s[0] == '#')
      break;
    vector<string> v;
    // cout << s.size() << endl;
    rep(i, (1 << s.size())) {
      string t = s;
      rep(j, s.size()) {
        if (i & (1 << j)) {
          if (t[j] == 'z')
            continue;
          t[j]++;
        }
      }
      // cout << "t " << t << endl;
      string u = t;
      int table[26] = {0};
      rep(j, u.size()) {
        if (u[j] == 'a')
          continue;
        if (table[u[j] - 'a'] == 0) {
          table[u[j] - 'a'] = 1;
          u[j]--;
        }
      }
      if (u == s)
        v.push_back(t);
    }
    printf("%d\\n", (int)v.size());
    sort(v.begin(), v.end());
    if (v.size() <= 10) {
      rep(i, v.size()) printf("%s\\n", v[i].c_str());
    } else {
      rep(i, 5) printf("%s\\n", v[i].c_str());
      rep(i, 5) printf("%s\\n", v[v.size() - 1 - i].c_str());
    }
  }
}