De Bruijn 序列生成
#include "stdafx.h" #include <vector> using namespace std; static vector<char> alpha; void seq_printer(unsigned int* a, unsigned int* a_end) { for (unsigned int* i = a; i < a_end; ++i) { printf("%c", alpha[*i]); } } void debruijn(unsigned int t, unsigned int p, const unsigned int k, const unsigned int n, unsigned int* a) { if (t > n) { // we want only necklaces, not pre-necklaces or Lyndon words if (n % p == 0) { seq_printer(a + 1, a + p + 1); } } else { a[t] = a[t - p]; debruijn(t + 1, p, k, n, a); for (unsigned int j = a[t - p] + 1; j < k; ++j) { a[t] = j; debruijn(t + 1, t, k, n, a); } } } int _tmain(int argc, _TCHAR* argv[]) { alpha.push_back('0'); alpha.push_back('1'); //alpha.push_back('c'); int N = 3; unsigned int* a = new unsigned int[N + 1]; a[0] = 0; debruijn(1, 1, alpha.size(), N,a); if (N > 0) printf("%c", alpha[0]); delete[] a; }