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

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int n, k;
bool used[11] = {false};
str s[11];
bool memo[11][11];
set<str> ans;
void f(int num, str st) {
  if (num == k) {
    ans.insert(st);
    return;
  } else {
    rep(i, 0, n) {
      if (!used[i]) {
        used[i] = true;
        f(num + 1, st + s[i]);
        used[i] = false;
      }
    }
    return;
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    ans.clear();
    cin >> n;
    cin >> k;
    if (n == 0 && k == 0)
      break;
    rep(i, 0, n) cin >> s[i];
    f(0, "");
    cout << ans.size() << endl;
  }
  /*for(str k:ans){
      cout << k << endl;
  }*/
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int n, k;
bool used[11] = {false};
str s[11];
bool memo[11][11];
set<str> ans;
void f(int num, str st, int nm) {
  // if(memo[num][nm])return;
  if (num == k) {
    ans.insert(st);
    return;
  } else {
    // memo[num][nm]=true;
    if (nm >= n)
      return;
    rep(i, 0, n) {
      if (!used[i]) {
        used[i] = true;
        f(num + 1, st + s[i], nm + 1);
        used[i] = false;
        f(num, st, nm + 1);
      }
    }
    return;
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    ans.clear();
    cin >> n;
    cin >> k;
    if (n == 0 && k == 0)
      break;
    rep(i, 0, n) cin >> s[i];
    // rep(i,0,11)rep(j,0,11)memo[i][j]=false;
    f(0, "", 0);
    cout << ans.size() << endl;
  }
  /*for(str k:ans){
      cout << k << endl;
  }*/
  return 0;
}