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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
set<int> dp[1 << 11];

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < (1 << 10); i++) {
      dp[i].clear();
    }
    string str;
    cin >> str;
    int bits = 0;
    bool flag = true;
    int v2 = -1;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == 'u') {
        bits = 0;
        v2 = -1;
      } else {
        if (v2 == -1) {
          v2 = str[i] - '0';
          bits = (1 << v2);
          continue;
        }
        int v = str[i] - '0';
        if ((bits >> v) & 1)
          flag = false;
        dp[bits].insert(1 << v);
        bits |= (1 << v);
      }
    }
    for (int i = 0; i < (1 << 10); i++) {
      if (!flag)
        break;
      for (int j = 0; j < i; j++) {
        if ((i & j) == j) {
          set<int>::iterator it = dp[j].begin();
          for (; it != dp[j].end(); it++) {
            set<int>::iterator it2 = dp[i - j].begin();
            for (; it2 != dp[i - j].end(); it2++) {
              int v = (*it) | (*it2);
              dp[i].insert(v);
            }
          }
        }
      }
      set<int>::iterator it = dp[i].begin();
      for (; it != dp[i].end(); it++) {
        bool ok = false;
        for (int k = 0; k < 10; k++) {
          if ((!(i >> k & 1)) && ((*it) >> k & 1))
            ok = true;
        }
        if (!ok)
          flag = false;
      }
    }
    printf("%s\\n", flag ? "SAFE" : "UNSAFE");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
set<int> dp[1 << 11];

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < (1 << 10); i++) {
      dp[i].clear();
    }
    string str;
    cin >> str;
    int bits = 0;
    bool flag = true;
    int v2 = -1;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == 'u') {
        bits = 0;
        v2 = -1;
      } else {
        if (v2 == -1) {
          v2 = str[i] - '0';
          bits = (1 << v2);
          continue;
        }
        int v = str[i] - '0';
        if ((bits >> v) & 1)
          flag = false;
        dp[bits].insert(1 << v);
        bits |= (1 << v);
      }
    }
    for (int i = 0; i < (1 << 10); i++) {
      for (int j = 0; j < i; j++) {
        if ((i & j) == j) {
          set<int>::iterator it = dp[j].begin();
          for (; it != dp[j].end(); it++) {
            set<int>::iterator it2 = dp[i - j].begin();
            for (; it2 != dp[i - j].end(); it2++) {
              int v = (*it) | (*it2);
              dp[i].insert(v);
            }
          }
        }
      }
      set<int>::iterator it = dp[i].begin();
      for (; it != dp[i].end(); it++) {
        bool ok = false;
        for (int k = 0; k < 10; k++) {
          if ((!(i >> k & 1)) && ((*it) >> k & 1))
            ok = true;
        }
        if (!ok)
          flag = false;
      }
    }
    printf("%s\\n", flag ? "SAFE" : "UNSAFE");
  }
  return 0;
}