2013年8月7日

UVa 129 - Krypton Factor

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

int n, L, now;
bool solved;

bool ok(string s) {
  int len = s.length();
  for (int l = 1; l <= len; l++) {
    for (int i = 0; i + l + l <= len; i++) {
      string s1 = s.substr(i, l), s2 = s.substr(i + l, l);
      if (s1 == s2) {
        return false;
      }
    }
  }
  return true;
}

void find(string s) {
  now++;
  if (solved) {
    return;
  }
  if (now == n) {
    int len = s.length();
    for (int i = 0; s[i]; i++) {
      putchar(s[i]);
      if (i != len - 1) {
        if (i == 63) {
          puts("");
        } else if ((i % 4) == 3) {
          putchar(' ');
        }
      }
    }
    printf("\n%d\n", len);
    solved = true;
    return;
  }
  for (int i = 0; i < L && !solved; i++) {
    string next = s;
    next.insert(next.end(), (i + 'A'));
    if (ok(next)) {
      find(next);
    }
  }
}

int main() {
  while (scanf("%d%d", &n, &L) && n) {
    now = -1;
    solved = false;
    find("");
  }
  return 0;
}

沒有留言:

張貼留言