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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string S[505];
int G[30][30];
int main() {
  // freopen("data.in","r",stdin);
  while (cin >> n) {
    if (n == 0)
      break;

    for (int i = 1; i <= n; i++)
      cin >> S[i];

    memset(G, 0, sizeof(G));
    bool ok = true;
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) {
        bool t = true;

        for (int k = 0; (k < S[i].size()) && (k < S[j].size()); k++) {
          if (S[i][k] != S[j][k]) {
            t = false;
            G[S[j][k] - 'a'][S[i][k] - 'a'] = 1;
            break;
          }
        }
        if (t && (S[i].size() > S[j].size()))
          ok = false;
      }

    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        if (i != j) {
          if (G[i][j] == 1 && G[j][i] == 1)
            ok = false;
        }
    if (ok)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define pi acos(-1)
#define INF 0x7fffffff
#define clr(x) memset(x, 0, sizeof(x));
#define clrto(x, siz, y)                                                       \\
  for (int xx = 0; xx <= siz; xx++)                                            \\
    x[xx] = y;
#define clrset(x, siz)                                                         \\
  for (int xx = 0; xx <= siz; xx++)                                            \\
    x[xx] = xx;
#define clrvec(x, siz)                                                         \\
  for (int xx = 0; x <= siz; xx++)                                             \\
    x[xx].clear();
#define fop                                                                    \\
  freopen("in.txt", "r", stdin);                                               \\
  freopen("out.txt", "w", stdout);
#define myprogram By_135678942570
#define clrcpy(x, siz, y)                                                      \\
  for (int xx = 0; xx < siz; xx++)                                             \\
    x[xx] = y[xx];
using namespace std;
int cd[1001];
map<int, int> mp[10];
int xx = 0;
int vis[10011];
struct card {
  int num;
  char s;
} C[100];
bool cmp(card a, card b) {
  if (a.num < 2 && b.num < 2 || a.num >= 2 && b.num >= 2)
    return a.num > b.num;
  return a.num < 2;
}
void dfs(int cc, int now, int rst, int need, int cont, int sum, int add) {
  if (rst == 0) {
    int sz = mp[cc].size();
    mp[cc][sum] = sz + 1 + add;
    //   printf("%d %d %d %d
    //   %d\\n",sum%13,sum%(13*13)/13,sum%(13*13*13)/(13*13),sum%(13*13*13*13)/(13*13*13),sum/(13*13*13*13));
    //   xx++;
    return;
  }
  for (int i = 2; i < now; i++) {
    if (vis[i])
      continue;
    if (i == now - 1 && need && rst == 1 && cont ||
        need && rst == 1 && cont && now == 14 && i == 5)
      continue;
    dfs(cc, i, rst - 1, need, cont && i == now - 1 || i == 5 && now == 14,
        sum * 13 + (i % 13), add);
  }
}
int cntshun = 10;
int cnttong = 0;
int cnt = 0;
int sum[10001];
void ini() {
  for (int i = 2; i < 10; i++)
    cd[i + '0'] = i;
  cd['T'] = 10;
  cd['A'] = 1;
  cd['J'] = 11;
  cd['Q'] = 12;
  cd['K'] = 0;
  clr(vis);
  for (int i = 2; i <= 14; i++)
    dfs(5, i, 4, 1, 1, (i % 13), 0);
  cnt += mp[5].size();
  sum[0] = cnt;
  for (int i = 2; i <= 14; i++) {
    vis[i] = 1;
    dfs(4, 15, 3, 0, 0, 13 * (i % 13) + (i % 13), cnt);
    vis[i] = 0;
  }
  cnt += mp[4].size();
  sum[1] = cnt;
  for (int i = 2; i <= 14; i++) {
    vis[i] = 1;
    for (int j = 2; j < i; j++) {
      vis[j] = 1;
      dfs(3, 15, 1, 0, 0,
          (13 * (13 * (13 * (i % 13) + (i % 13)) + (j % 13)) + (j % 13)), cnt);
      vis[j] = 0;
    }
    vis[i] = 0;
  }
  cnt += mp[3].size();
  sum[2] = cnt;
  for (int i = 2; i <= 14; i++) {
    vis[i] = 1;
    dfs(6, 15, 2, 0, 0, 13 * (13 * (i % 13) + (i % 13)) + (i % 13), cnt);
    vis[i] = 0;
  }
  cnt += mp[6].size();
  sum[3] = cnt;
  cnt += 14 - 5 + 1;
  sum[4] = cnt;
  cnt += (cnttong = mp[5].size());
  sum[5] = cnt;
  for (int i = 2; i <= 14; i++)
    for (int j = 2; j <= 14; j++)
      if (i != j) {
        int sz = mp[7].size();
        mp[7]
          [13 * (13 * (13 * (13 * (i % 13) + (i % 13)) + (i % 13)) + (j % 13)) +
           (j % 13)] = cnt + 1 + sz;
      }
  cnt += mp[7].size();
  sum[6] = cnt;
  for (int i = 2; i <= 14; i++) {
    vis[i] = 1;
    dfs(2, 15, 1, 0, 0,
        13 * (13 * (13 * (i % 13) + (i % 13)) + (i % 13)) + (i % 13), cnt);
    vis[i] = 0;
  }
  cnt += mp[2].size();
  sum[7] = cnt;
  cnt += cntshun;
}
int getrank(char *s1, char *s2, char *s3, char *s4, char *s5) {
  int flag1 = 1, flag2 = 1;
  int times[20] = {0};
  int maxn = 0;
  int doublepair = 0;
  int maxi = 0;
  int singlepair = 0;
  for (int i = 0; i < 5; i++) {
    char a, aa[3];
    if (i == 0)
      aa[0] = s1[1], aa[1] = s1[0];
    if (i == 1)
      aa[0] = s2[1], aa[1] = s2[0];
    if (i == 2)
      aa[0] = s3[1], aa[1] = s3[0];
    if (i == 3)
      aa[0] = s4[1], aa[1] = s4[0];
    if (i == 4)
      aa[0] = s5[1], aa[1] = s5[0];
    a = aa[0], C[i].s = aa[1];
    printf("%s\\n", aa);
    C[i].num = cd[a];
    times[C[i].num]++;
    if (times[C[i].num] == 2 && maxn == 2)
      doublepair = 1;
    else {
      if (maxn < times[C[i].num]) {
        maxn = times[C[i].num];
        if (maxn == 3 && maxi == C[i].num && !doublepair)
          singlepair = 0;
        maxi = C[i].num;
      }
      if (times[C[i].num] == 2)
        singlepair = 1;
    }
    if (i != 0 && C[i].s != C[0].s)
      flag2 = 0;
  }
  sort(C, C + 5, cmp);
  if (C[0].num >= 2 && C[0].num < 5)
    flag1 = 0;
  for (int i = 1; i < 5; i++)
    if (!(C[i].num == 5 && C[i - 1].num == 1) &&
        C[i].num != (C[i - 1].num - 1 + 13) % 13)
      flag1 = 0;
  if (flag1 && flag2) {
    if (C[0].num == 1 && C[4].num == 2)
      return (sum[7] + 1);
    else if (C[0].num < 2)
      return (sum[7] + 9 + C[0].num);
    else
      return (sum[7] + C[0].num - 4);
  } else if (maxn == 4) {
    int temp;
    for (int i = 0; i < 5; i++)
      if (maxi != C[i].num)
        temp = C[i].num;
    return (mp[2][13 * (13 * (13 * (13 * maxi + maxi) + maxi) + maxi) + temp]);
  } else if (maxn == 3 && singlepair) {
    int temp;
    for (int i = 0; i < 5; i++)
      if (maxi != C[i].num)
        temp = C[i].num;
    return (mp[7][13 * (13 * (13 * (13 * maxi + maxi) + maxi) + temp) + temp]);
  } else if (flag2) {
    return (sum[4] +
            mp[5][13 * (13 * (13 * (13 * C[0].num + C[1].num) + C[2].num) +
                        C[3].num) +
                  C[4].num]);
  } else if (flag1) {
    if (C[0].num == 1 && C[4].num == 2)
      return (sum[3] + 1);
    else if (C[0].num < 2)
      return (sum[3] + 9 + C[0].num);
    else
      return (sum[3] + C[0].num - 4);
  } else if (maxn == 3) {
    int cnt = 0;
    int a[2] = {0};
    for (int i = 0; i < 5; i++)
      if (maxi != C[i].num)
        a[cnt++] = C[i].num;
    return (mp[6][13 * (13 * (13 * (13 * maxi + maxi) + maxi) + a[0]) + a[1]]);
  } else if (doublepair) {
    int a, b;
    int temp;
    for (int i = 0; i < 5; i++)
      if (times[C[i].num] == 2)
        b = C[i].num;
      else
        temp = C[i].num;
    for (int i = 4; i >= 0; i--)
      if (times[C[i].num] == 2)
        a = C[i].num;
    return (mp[3][13 * (13 * (13 * (13 * a + a) + b) + b) + temp]);
  } else if (singlepair) {
    int cnt = 0;
    int a[3] = {0};
    for (int i = 0; i < 5; i++)
      if (maxi != C[i].num)
        a[cnt++] = C[i].num;
    return (mp[4][13 * (13 * (13 * (13 * maxi + maxi) + a[0]) + a[1]) + a[2]]);
  } else
    return (mp[5][13 * (13 * (13 * (13 * C[0].num + C[1].num) + C[2].num) +
                        C[3].num) +
                  C[4].num]);
}
map<string, int> vvv;
char getcolor(int p) {
  if (p == 0)
    return 'S';
  if (p == 1)
    return 'H';
  if (p == 2)
    return 'D';
  return 'C';
}
char getid(int p) {
  if (p < 10)
    return p + '0';
  if (p == 10)
    return 'T';
  if (p == 11)
    return 'J';
  if (p == 12)
    return 'Q';
  return 'K';
}
int main() {
  ini();
  printf("%d\\n", getrank("HA", "HK", "HQ", "HJ", "HT"));
  while (1) {
    vvv.clear();
    char m1[5], m2[5];
    scanf("%s%s", m1, m2);
    vvv[m1] = 1, vvv[m2] = 1;
    if (m1[0] == '#')
      break;
    char o1[5], o2[5];
    scanf("%s%s", o1, o2);
    vvv[o1] = 1, vvv[o2] = 1;
    char r[5][5] = {0};
    scanf("%s%s%s", r[0], r[1], r[2]);
    vvv[r[0]] = 1;
    vvv[r[1]] = 1;
    vvv[r[2]] = 1;
    int all = 0;
    int cnt = 0;
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 13; j++) {
        r[3][0] = getcolor(i);
        r[3][1] = getid(i);
        if (vvv[r[3]] == 0)
          for (int i1 = 0; i1 < 4; i1++)
            for (int j1 = 0; j1 < 13; j1++) {
              r[4][0] = getcolor(i1);
              r[4][1] = getid(j1);
              if (vvv[r[4]] == 0) {
                int maxn1 = 0;
                int maxn2 = 0;
                for (int a = 0; a < 5; a++)
                  for (int b = a + 1; b < 5; b++)
                    for (int c = b + 1; c < 5; c++) {
                      int r1 = getrank(m1, m2, r[a], r[b], r[c]);
                      int r2 = getrank(o1, o2, r[a], r[b], r[c]);
                      maxn1 = max(maxn1, r1);
                      maxn2 = max(maxn2, r2);
                    }
                all++;
                if (maxn1 > maxn2)
                  cnt++;
              }
            }
      }
    printf("%.10f\\n", cnt * 1.0 / all);
  }
}