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

#include <algorithm>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
char in[20];
string str[210];
set<pair<long long, long long>> lis[210];
char ch[2];
inline pair<long long, long long> conv(string s) {
  long long r1 = 0;
  long long r2 = 0;
  for (int i = 0; i < s.size(); i++) {
    if (i < 8) {
      r1 += ((long long)(s[i] - 'a' + 1)) << (i * 5);
    } else
      r2 += ((long long)(s[i] - 'a' + 1)) << ((i - 8) * 5);
  }
  return make_pair(r1, r2);
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    int b;
    scanf("%d", &b);
    for (int i = 0; i < a; i++) {
      scanf("%s", in);
      str[i] = in;
    }
    std::sort(str, str + a);
    for (int i = 0; i < a; i++)
      lis[i].clear();
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < str[i].size(); j++) {
        string tmp = str[i].substr(0, j) + str[i].substr(j + 1);
        lis[i].insert(conv(tmp));
      }
      for (int j = 0; j <= str[i].size(); j++) {
        for (int k = 0; k < 26; k++) {
          string tmp = str[i].substr(0, j);
          ch[0] = 'a' + k;
          tmp.append(ch);
          tmp.append(str[i].substr(j));
          lis[i].insert(conv(tmp));
        }
      }
      for (int j = 0; j < str[i].size(); j++) {
        char rev = str[i][j];
        for (int k = 0; k < 26; k++) {
          str[i][j] = 'a' + k;
          lis[i].insert(conv(str[i]));
        }
        str[i][j] = rev;
      }
      for (int j = 0; j < str[i].size() - 1; j++) {
        swap(str[i][j], str[i][j + 1]);
        lis[i].insert(conv(str[i]));
        swap(str[i][j], str[i][j + 1]);
      }
      //	std::sort(lis[i].begin(),lis[i].end());
    }
    int ret = 0;
    for (int i = 0; i < a; i++) {
      for (int j = i + 1; j < a; j++) {
        bool ok = false;
        if (b == 1) {
          if (lis[i].count(conv(str[j])))
            ok = true;
        } else {
          for (set<pair<long long, long long>>::iterator it = lis[j].begin();
               it != lis[j].end(); it++) {
            if (lis[i].count(*it)) {
              ok = true;
              break;
            }
          }
        }
        if (ok) {
          ret++;
          printf("%s,%s\\n", str[i].c_str(), str[j].c_str());
        }
      }
    }
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
char in[20];
string str[210];
set<string> lis[210];
char ch[2];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    int b;
    scanf("%d", &b);
    for (int i = 0; i < a; i++) {
      scanf("%s", in);
      str[i] = in;
    }
    std::sort(str, str + a);
    for (int i = 0; i < a; i++)
      lis[i].clear();
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < str[i].size(); j++) {
        string tmp = str[i].substr(0, j) + str[i].substr(j + 1);
        lis[i].insert(tmp);
      }
      for (int j = 0; j <= str[i].size(); j++) {
        for (int k = 0; k < 26; k++) {
          string tmp = str[i].substr(0, j);
          ch[0] = 'a' + k;
          tmp.append(ch);
          tmp.append(str[i].substr(j));
          lis[i].insert(tmp);
        }
      }
      for (int j = 0; j < str[i].size(); j++) {
        char rev = str[i][j];
        for (int k = 0; k < 26; k++) {
          str[i][j] = 'a' + k;
          lis[i].insert(str[i]);
        }
        str[i][j] = rev;
      }
      for (int j = 0; j < str[i].size() - 1; j++) {
        swap(str[i][j], str[i][j + 1]);
        lis[i].insert(str[i]);
        swap(str[i][j], str[i][j + 1]);
      }
      //	std::sort(lis[i].begin(),lis[i].end());
    }
    int ret = 0;
    for (int i = 0; i < a; i++) {
      for (int j = i + 1; j < a; j++) {
        bool ok = false;
        if (b == 1) {
          if (lis[i].count(str[j]))
            ok = true;
        } else {
          for (set<string>::iterator it = lis[j].begin(); it != lis[j].end();
               it++) {
            if (lis[i].count(*it)) {
              ok = true;
              break;
            }
          }
        }
        if (ok) {
          ret++;
          printf("%s,%s\\n", str[i].c_str(), str[j].c_str());
        }
      }
    }
    printf("%d\\n", ret);
  }
}