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

#include <iostream>
#include <string>

using namespace std;

int main() {
  while (1) {
    int n, k, c = 0;
    string a[10], val[10000], t;
    bool no;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    if (k == 2) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (i == j)
            continue;
          no = true;
          t = a[i] + a[j];
          for (int k = 0; k < c; k++)
            if (val[k] == t) {
              no = false;
              k = c;
            }
          if (no) {
            val[c] = t;
            c++;
          }
        }
      }
    }
    if (k == 3) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          for (int k = 0; k < n; k++) {
            if (i == j || j == k || i == k)
              continue;
            no = true;
            t = a[i] + a[j] + a[k];
            for (int l = 0; l < c; l++)
              if (val[l] == t) {
                no = false;
                l = c;
              }
            if (no) {
              val[c] = t;
              c++;
            }
          }
        }
      }
    }
    if (k == 4) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          for (int k = 0; k < n; k++) {
            for (int l = 0; l < n; l++) {
              if (i == j || j == k || k == l || i == k || i == l || j == l)
                continue;
              no = true;
              t = a[i] + a[j] + a[k] + a[l];
              for (int m = 0; m < c; m++)
                if (val[m] == t) {
                  no = false;
                  m = c;
                }
              if (no) {
                val[c] = t;
                c++;
              }
            }
          }
        }
      }
    }
    cout << c << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  while (1) {
    int n, k, c = 0;
    string a[10], val[10000];
    bool no;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    if (k == 2) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (i == j)
            continue;
          no = true;
          for (int k = 0; k < c; k++)
            if (val[k] == a[i] + a[j])
              no = false;
          if (no) {
            val[c] = a[i] + a[j];
            c++;
          }
        }
      }
    }
    if (k == 3) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          for (int k = 0; k < n; k++) {
            if (i == j || j == k || i == k)
              continue;
            no = true;
            for (int l = 0; l < c; l++)
              if (val[l] == a[i] + a[j] + a[k])
                no = false;
            if (no) {
              val[c] = a[i] + a[j] + a[k];
              c++;
            }
          }
        }
      }
    }
    if (k == 4) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          for (int k = 0; k < n; k++) {
            for (int l = 0; l < n; l++) {
              if (i == j || j == k || k == l || i == k || i == l || j == l)
                continue;
              no = true;
              for (int m = 0; m < c; m++)
                if (val[m] == a[i] + a[j] + a[k] + a[l])
                  no = false;
              if (no) {
                val[c] = a[i] + a[j] + a[k] + a[l];
                c++;
              }
            }
          }
        }
      }
    }
    cout << c << '\\n';
  }
  return 0;
}