alexjercan/bugnet huggingface dataset:
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
// typedef pair<int, int> P;

/** Problem0546 : Lining up the cards **/

vector<string> card;
bool used[10];
set<string> ans;
int N, K;

void solve(int n, string s) {
  if (n == K)
    ans.insert(s);
  else {
    rep(i, N) {
      if (!used[i]) {
        used[i] = true;
        solve(n + 1, s + card[i]);
        used[i] = false;
      }
    }
  }
}

int main() {
  while (cin >> N, N) {
    ans.clear();
    card.clear();
    fill(used, used + 10, false);

    cin >> K;
    rep(i, N) {
      string s;
      cin >> s;
      card.push_back(s);
    }
    solve(0, "");
    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
// typedef pair<int, int> P;

/** Problem0546 : Lining up the cards **/

vector<string> card;
set<string> ans;
int N, K;

int main() {
  while (cin >> N, N) {
    cin >> K;
    rep(i, N) {
      string s;
      cin >> s;
      card.push_back(s);
    }
    sort(card.begin(), card.end());
    set<string> ans;
    do {
      string s;
      rep(i, K) { s += card[i]; }
      // cerr << s << endl;
      ans.insert(s);
    } while (next_permutation(card.begin(), card.end()));

    // set<string>::iterator it = ans.begin();
    // for (; it!=ans.end(); it++) cerr<<*it<<endl;
    cout << ans.size() << endl;
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <set>
#include <string>
using namespace std;

int n, k;
string a[11];
char t[5];
set<string> ss;
string s;

int main() {
  while (scanf(" %d%d", &n, &k), n | k) {
    ss.clear();
    for (int i = 0; i < n; ++i) {
      scanf(" %s ", t);
      a[i] = t;
    }
    sort(a, a + n);
    do {
      s.clear();
      for (int i = 0; i < k; ++i) {
        s += a[i];
      }
      ss.insert(s);
    } while (next_permutation(a, a + n));
    printf("%d\\n", (int)ss.size());
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
using namespace std;

int n, k;
string a[11];
string b[4];
char t[5];
int main() {
  int x, sum;
  while (1) {
    scanf(" %d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    set<string> ss;
    for (int i = 0; i < n; ++i) {
      scanf(" %s ", t);
      a[i] = t;
    }
    sort(a, a + n);
    do {
      string s;
      for (int i = 0; i < k; ++i) {
        s += a[i];
      }
      ss.insert(s);
    } while (next_permutation(a, a + n));
    printf("%d\\n", (int)ss.size());
  }
}
Problem p00469 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, k;
int card[10];
bool b[10];
set<string> ans;
void solve(string sum, int rest) {
  if (!rest) {
    ans.insert(sum);
    return;
  }
  for (int i = 0; i < n; i++) {
    if (!b[i]) {
      b[i] = true;
      solve(sum + to_string(card[i]), rest - 1);
      b[i] = false;
    }
  }
}
int main() {
  while (scanf("%d%d", &n, &k), n) {
    ans.clear();
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; i++)
      scanf("%d", &card[i]);
    solve("\\0", k);
    printf("%d\\n", ans.size());
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int n, k, cnt = 0;
char card[10];
bool ans[100000000];
void solve(int a, vector<char> vec, vector<bool> b) {
  vec.push_back(card[a]);
  b[a] = true;
  if (vec.size() == k) {
    ostringstream oss;
    for (char i : vec)
      oss << (int)i;
    int n = stoi(oss.str());
    if (ans[n] != true)
      cnt++;
    ans[n] = true;
    return;
  }
  for (int i = 0; i < n; i++) {
    if (!b[i])
      solve(i, vec, b);
  }
}
int main() {
  memset(ans, -1, sizeof(ans));
  scanf("%d%d", &n, &k);
  if (!(n | k))
    return 0;
  for (int i = 0; i < n; i++)
    scanf("%d", &card[i]);
  vector<char> vec;
  vector<bool> b(10);
  for (int i = 0; i < n; i++)
    solve(i, vec, b);
  printf("%d\\n", cnt);
  main();
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 11
#define SIZE 6000

int N, K;
char A[MAXN][10];
int used[MAXN];
char num[100];
int size;
int arr[SIZE];

void rec(int k) {
  int i;
  int length = strlen(num);
  if (k == K) {
    arr[size++] = atoi(num);
    return;
  }
  for (i = 0; i < N; ++i) {
    if (!used[i]) {
      used[i] = 1;
      strcat(num, A[i]);
      rec(k + 1);
      used[i] = 0;
      num[length] = '\\0';
    }
  }
}

void bubbleSort() {
  int i, j, t;
  for (i = 0; i < size; ++i) {
    for (j = 1; j < size - i; ++j) {
      if (arr[j - 1] > arr[j]) {
        t = arr[j - 1];
        arr[j - 1] = arr[j];
        arr[j] = t;
      }
    }
  }
}

int main() {
  int i, ans;
  while (1) {
    scanf("%d", &N);
    scanf("%d", &K);
    if (N == 0 && K == 0)
      break;
    for (i = 0; i < N; ++i) {
      scanf("%s", A[i]);
    }

    for (i = 0; i < N; ++i) {
      used[i] = 0;
    }
    strcpy(num, "");
    size = 0;
    rec(0);
    bubbleSort();

    ans = 0;
    for (i = 0; i < size; ++i) {
      if (i == 0 || arr[i - 1] != arr[i]) {
        ++ans;
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 11
#define SIZE 400

int N, K;
char A[MAXN][10];
int used[MAXN];
char num[100];
int size;
int arr[SIZE];

void rec(int k) {
  int i;
  int length = strlen(num);
  if (k == K) {
    arr[size++] = atoi(num);
    return;
  }
  for (i = 0; i < N; ++i) {
    if (!used[i]) {
      used[i] = 1;
      strcat(num, A[i]);
      rec(k + 1);
      used[i] = 0;
      num[length] = '\\0';
    }
  }
}

void bubbleSort() {
  int i, j, t;
  for (i = 0; i < size; ++i) {
    for (j = 1; j < size - i; ++j) {
      if (arr[j - 1] > arr[j]) {
        t = arr[j - 1];
        arr[j - 1] = arr[j];
        arr[j] = t;
      }
    }
  }
}

int main() {
  int i, ans;
  while (1) {
    scanf("%d", &N);
    scanf("%d", &K);
    if (N == 0 && K == 0)
      break;
    for (i = 0; i < N; ++i) {
      scanf("%s", A[i]);
    }

    for (i = 0; i < N; ++i) {
      used[i] = 0;
    }
    strcpy(num, "");
    size = 0;
    rec(0);

    bubbleSort();

    ans = 0;
    for (i = 0; i < size; ++i) {
      if (i == 0 || arr[i - 1] != arr[i]) {
        ++ans;
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#define MAX 30
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int k;
    cin >> k;
    vector<string> vec;
    vector<int> ind;
    set<string> st;
    bool isDone[10001];
    for (int i = 0; i < 10001; i++) {
      isDone[i] = false;
    }

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      vec.push_back(s);
      ind.push_back(i);
    }
    sort(vec.begin(), vec.end());
    //		cout<<"---"<<endl;
    do {
      int hash = 0;
      for (int i = 0; i < k; i++) {
        hash = hash * 10 + ind[i];
      }
      if (isDone[hash])
        continue;
      isDone[hash] = true;
      string t = "";
      for (int i = 0; i < k; i++) {
        t += vec[ind[i]];
      }
      st.insert(t);
      //			cout<<"------"<<endl;
    } while (next_permutation(ind.begin(), ind.end()));
    cout << st.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#define MAX 30
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int k;
    cin >> k;
    vector<string> vec;
    set<string> st;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      vec.push_back(s);
    }
    sort(vec.begin(), vec.end());
    //		cout<<"---"<<endl;
    do {
      string t = "";
      for (int i = 0; i < k; i++) {
        t += vec[i];
      }
      st.insert(t);
      //			cout<<"------"<<endl;
    } while (next_permutation(vec.begin(), vec.end()));
    cout << st.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 11

int n, k;
int list[MAX_N];

int solve() {

  int ans = 0;
  vector<int> v;
  do {
    int score = 0;
    rep(i, k) {
      int x = list[i];
      if (x < 10)
        score = score * 10 + x;
      else
        score = score * 100 + x;
    }
    if (!binary_search(v.begin(), v.end(), score)) {
      ans++;
      v.push_back(score);
      sort(v.begin(), v.end());
    }
  } while (next_permutation(list, list + n));
  return ans;
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    rep(i, n) cin >> list[i];
    sort(list, list + n);

    cout << solve() << '\\n';
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 11
#define MAX_ANS 99999999

int n, k;
int list[MAX_N];
int checked[MAX_ANS];

int solve() {
  rep(i, MAX_ANS) checked[i] = false;

  int ans = 0;
  do {
    int score = 0;
    rep(i, k) {
      int x = list[i];
      if (x < 10)
        score = score * 10 + x;
      else
        score = score * 100 + x;
    }
    if (!checked[score]) {
      ans++;
      checked[score] = true;
    }
  } while (next_permutation(list, list + n));
  return ans;
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    rep(i, n) cin >> list[i];
    sort(list, list + n);

    cout << solve() << '\\n';
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <string>
using namespace std;
int f[10], n, k;
string v[10];
set<string> u;
void F(int d, string s) {
  if (d == k)
    u.insert(s);
  else
    for (int i = 0; i < n; ++i)
      if (!f[i]) {
        f[i] = 1;
        F(d + 1, s + v[i]);
        f[i] = 0;
      }
}
int main() {
  int i;
  string s;
  while (cin >> n >> k, n) {
    for (i = 0; i < n; ++i)
      cin >> s, v[i] = s;
    u.clear();
    F(0, "");
    cout << u.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, k, i, a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  string s, v[10];
  while (cin >> n >> k, n) {
    set<string> u;
    for (i = 0; i < n; ++i)
      cin >> s, v[i] = s;
    do {
      for (s = "", i = 0; i < k; ++i)
        s += v[a[i]];
      u.insert(s);
    } while (next_permutation(a, a + n));
    cout << u.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

int main() {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    string s;
    vector<string> data;
    for (int i = 0; i < n; i++) {
      cin >> s;
      data.push_back(s);
    }
    sort(data.begin(), data.end());
    set<int> res;
    do {
      int t = 0;
      for (int i = 0; i < k; i++) {
        for (int j = 0; j < data[i].size(); j++) {
          t *= 10;
          t += data[i][j] - '0';
        }
      }
      res.insert(t);
    } while (next_permutation(data.begin(), data.end()));

    cout << res.size() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}

int main() {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    string s;
    vector<string> data;
    for (int i = 0; i < n; i++) {
      cin >> s;
      data.push_back(s);
    }
    sort(data.begin(), data.end());
    set<int> res;
    do {
      string t = "";
      for (int i = 0; i < k; i++) {
        t += data[i];
      }
      int tt = toInt(t);
      res.insert(tt);
    } while (next_permutation(data.begin(), data.end()));

    cout << res.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

vector<int> ary, aryh;

template <class BifirectionalIterator>
bool next_partial_permutation(BifirectionalIterator first,
                              BifirectionalIterator last, int k) {
  reverse(first + k, last);
  return next_permutation(first, last);
}

int myp(int x, int y) {
  int a;
  a = 1;
  for (int i = 0; i < y; i++)
    a *= x;
  return a;
}

int main() {
  int N, K, tmp, ket, hog;
  set<int> s;
  set<int>::iterator it;
  while (1) {
    cin >> N >> K;
    ary.clear();
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> tmp;
      ary.push_back(tmp);
    }
    s.clear();

    sort(ary.begin(), ary.end());
    do {

      ket = hog = 0;
      for (int i = 0; i < K; i++) {
        hog += ary[i] * myp(10, ket);
        //?????????
        if (ary[i] > 9) {
          ket += 2;
        } else {
          ket++;
        }
      }
      s.insert(hog);

    } while (next_partial_permutation(ary.begin(), ary.end(), K));
    //
    /*for(it=s.begin();it!=s.end();it++){
            cout<<*it<<" ";
    }
    cout<<endl;*/
    //
    cout << s.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

vector<int> ary;

int myp(int x, int y) {
  int a;
  a = 1;
  for (int i = 0; i < y; i++)
    a *= x;
  return a;
}

int main() {
  int N, K, tmp, ket, hog;
  set<int> s;
  set<int>::iterator it;
  while (1) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> tmp;
      ary.push_back(tmp);
    }
    s.clear();

    sort(ary.begin(), ary.end());
    do {
      ket = hog = 0;
      for (int i = 0; i < K; i++) {
        hog += ary[i] * myp(10, ket);
        //?????????
        if (ary[i] > 9) {
          ket += 2;
        } else {
          ket++;
        }
      }
      s.insert(hog);

    } while (next_permutation(ary.begin(), ary.end()));
    cout << s.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, save3[10], roop2, card[10], res, save2;
bool save[100000000];
void solve() {
  roop--;
  for (int i = 0; i < n; i++) {
    if (save3[i] == 1)
      continue;
    if (roop == 0) {
      save2 = res;
      if (card[i] / 10 >= 1) {
        res = res * 100;
      } else {
        res = res * 10;
      }
      res += card[i];
      if (save[res] == 0) {
        ans++;
        save[res] = 1;
      }
      res = save2;
    } else {
      if (card[i] / 10 >= 1) {
        res = res * 100;
      } else {
        res = res * 10;
      }
      res += card[i];
      save3[i] = 1;
      solve();
      save3[i] = 0;
      roop++;
      res -= card[i];
      if (card[i] / 10 >= 1) {
        res = res / 100;
      } else {
        res = res / 10;
      }
    }
  }
}
int main() {
  while (true) {
    cin >> n >> k;
    roop2 = 0;
    if (n == 0 && k == 0) {
      return 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> card[i];
    }
    for (int i = 0; i < n; i++) {
      roop = k - 1;
      res = card[i];
      save3[i] = 1;
      solve();
      save3[i] = 0;
    }
    cout << ans << endl;
    ans = 0;
    for (int i = 0; i < 10000000; i++) {
      save[i] = 0;
    }
  }
}

Error: 127
/tmp/444405d6-4c38-490d-bbc2-b9187e0beec3.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, save3[10], roop2, card[10], res, save2;
bool save[100000000];
void solve() {
  roop--;
  for (int i = 0; i < n; i++) {
    if (save3[i] == 1)
      continue;
    if (roop == 0) {
      save2 = res;
      if (card[i] / 10 >= 1) {
        res = res * 100;
      } else {
        res = res * 10;
      }
      res += card[i];
      if (save[res] == 0) {
        ans++;
        save[res] = 1;
      }
      res = save2;
    } else {
      if (card[i] / 10 >= 1) {
        res = res * 100;
      } else {
        res = res * 10;
      }
      res += card[i];
      save3[i] = 1;
      solve();
      save3[i] = 0;
      roop++;
      res -= card[i];
      if (res % 100 == 0)
        res = res / 100;
      else if (res % 10 == 0)
        res = res / 10;
    }
  }
}
int main() {
  while (true) {
    cin >> n >> k;
    roop2 = 0;
    if (n == 0 && k == 0) {
      return 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> card[i];
    }
    for (int i = 0; i < n; i++) {
      roop = k - 1;
      res = card[i];
      save3[i] = 1;
      solve();
      save3[i] = 0;
    }
    cout << ans << endl;
    ans = 0;
    for (int i = 0; i < 10000000; i++) {
      save[i] = 0;
    }
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int n, k;
string a[11];
set<string> all;
bool flag[11];

void solve(int num, string s) {
  if (num == k) {
    all.insert(s);
    return;
  }
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    flag[i] = true;
    solve(num + 1, s + a[i]);
    flag[i] = false;
  }
}

int main() {
  while (cin >> n >> k, n + k) {
    all.clear();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < 11; i++)
      flag[i] = false;
    solve(0, "");
    cout << all.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n, k;
string a[11];
set<string> all;
bool flag[11];

void solve(int num, string s) {
  if (num == k) {
    all.insert(s);
  }
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    flag[i] = true;
    solve(num + 1, s + a[i]);
    flag[i] = false;
  }
}

int main() {
  while (cin >> n >> k, n + k) {
    all.clear();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < 11; i++)
      flag[i] = false;
    solve(0, "");
    cout << all.size() << endl;
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int n, k, ans;
vector<string> V;
map<string, int> M;

void init() {
  V.clear();
  M.clear();
  ans = 0;
}

void solve(string s, int rev, int used) {
  // cout << "s " << s << endl;
  if (rev == k) {
    if (M[s] == 0) {
      M[s] = 1;
      ans++;
    }
    return;
  }

  for (int i = 0; i < V.size(); i++)
    if (((1 << i) & used) == 0)
      solve(s + V[i], rev + 1, used + (1 << i));
}

int main() {
  while (cin >> n >> k && n + k) {
    init();
    for (int i = 0; i < n; i++) {
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("", 0, 0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int n, k, ans;
vector<string> V;
vector<string> VV;

void init() {
  V.clear();
  VV.clear();
  ans = 0;
}

void solve(string s, int rev, int used) {
  // cout << "s " << s << endl;
  if (rev == k) {
    sort(VV.begin(), VV.end());
    if (!binary_search(VV.begin(), VV.end(), s)) {
      ans++;
      VV.push_back(s);
    }
    return;
  }

  for (int i = 0; i < V.size(); i++)
    if (((1 << i) & used) == 0)
      solve(s + V[i], rev + 1, used + (1 << i));
}

int main() {
  while (cin >> n >> k && n + k) {
    init();
    for (int i = 0; i < n; i++) {
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("", 0, 0);
    cout << ans << endl;
  }
  return 0;
}