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

#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
  for (;;) {
    string sstr;
    getline(cin, sstr);
    if (sstr == "#")
      break;
    const char *str = sstr.c_str();
    int cnt = 0;
    const int n = sstr.length();
    int nhs = 0, nts = 0, ntsp = 0;
    char hs[5][21], ts[5][21];
    for (int i = 0; i < 1 << n; i++) {
      char tmp[21], ans[21];
      ans[n] = 0;
      bool invalid = false;
      int ii = i;
      // bit reversal
      {
        register unsigned int x = i;
        x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
        x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
        x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
        x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
        ii = ((x >> 16) | (x << 16));
        ii >>= (32 - n);
      }
      for (int k = 0; k < n; k++, ii >>= 1) {
        tmp[k] = str[k] + (ii & 1); //(((ii & bb)) ? 1 : 0);
        ans[k] = tmp[k];
        if (tmp[k] > 'z') {
          invalid = true;
          break;
        }
      }
      if (invalid)
        continue;
      int hist[256];
      for (char c = 'b'; c <= 'z'; c++) {
        hist[c] = 0;
      }
      for (char *p = tmp; p != tmp + n; p++) {
        hist[*p]++;
      }
      for (char c = 'b'; c <= 'z'; c++) {
        if (hist[c] == 0)
          continue;
        for (char *p = tmp; p != tmp + n; p++) {
          if (*p == c) {
            (*p)--;
            break;
          }
        }
        // for(int k = 0; k < n; k++) {
        //   if(tmp[k] == c) {
        //     tmp[k]--;
        //     break;
        //   }
        // }
      }
      bool ok = true;
      for (int k = 0; k < n; k++) {
        if ((str[k] != tmp[k])) {
          ok = false;
          break;
        }
      }
      if (ok) {
        cnt++;
        if (nhs < 5) {
          for (int k = 0; k < n; k++) {
            hs[nhs][k] = ans[k];
          }
          hs[nhs][n] = 0;
          nhs++;
        } else {
          if (nts < 5)
            nts++;
          for (int k = 0; k < n; k++) {
            ts[ntsp][k] = ans[k];
          }
          ts[ntsp][n] = 0;
          ntsp = (ntsp + 1) % 5;
        }
      }
    }
    cout << cnt << endl;
    for (int i = 0; i < nhs; i++) {
      cout << hs[i] << endl;
    }
    for (int i = 0; i < nts; i++) {
      cout << ts[(ntsp - nts + i + 5) % 5] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
  for (;;) {
    string sstr;
    getline(cin, sstr);
    if (sstr == "#")
      break;
    const char *str = sstr.c_str();
    int cnt = 0;
    const int n = sstr.length();
    int nhs = 0, nts = 0, ntsp = 0;
    char hs[5][21], ts[5][21];
    for (int i = 0; i < 1 << n; i++) {
      char tmp[21], ans[21];
      ans[n] = 0;
      bool invalid = false;
      int ii;
      // bit reversal
      {
        register unsigned int x = i;
        x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
        x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
        x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
        x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
        ii = ((x >> 16) | (x << 16));
        ii >>= (32 - n);
      }
      for (int k = 0; k < n; k++, ii >>= 1) {
        tmp[k] = str[k] + (ii & 1); //(((ii & bb)) ? 1 : 0);
        ans[k] = tmp[k];
        if (tmp[k] > 'z') {
          invalid = true;
          break;
        }
      }
      if (invalid)
        continue;
      for (char c = 'b'; c <= 'z'; c++) {
        for (char *p = tmp; p != tmp + n; p++) {
          if (*p == c) {
            (*p)--;
            break;
          }
        }
        // for(int k = 0; k < n; k++) {
        //   if(tmp[k] == c) {
        //     tmp[k]--;
        //     break;
        //   }
        // }
      }
      bool ok = true;
      for (int k = 0; k < n; k++) {
        if ((str[k] != tmp[k])) {
          ok = false;
          break;
        }
      }
      if (ok) {
        cnt++;
        if (nhs < 5) {
          for (int k = 0; k < n; k++) {
            hs[nhs][k] = ans[k];
          }
          hs[nhs][n] = 0;
          nhs++;
        } else {
          if (nts < 5)
            nts++;
          for (int k = 0; k < n; k++) {
            ts[ntsp][k] = ans[k];
          }
          ts[ntsp][n] = 0;
          ntsp = (ntsp + 1) % 5;
        }
      }
    }
    cout << cnt << endl;
    for (int i = 0; i < nhs; i++) {
      cout << hs[i] << endl;
    }
    for (int i = 0; i < nts; i++) {
      cout << ts[(ntsp - nts + i + 5) % 5] << endl;
    }
  }
  return 0;
}