1  using System;
   2  
   3  class CrackingSequence {
   4  
   5      public CrackingSequence(int N) {
   6          N_ = N;
   7          int tenN = System.Convert.ToInt32(Math.Pow(10, N_));
   8          used_ = new bool[tenN];
   9          sequence_ = new char[tenN + N - 1];
  10          }
  11  
  12      public void execute(){
  13          char[] digits = new char[10];
  14          for (int i = 0; i < 10; i++) digits[i] = Convert.ToChar(i);
  15  
  16          // Initialize first sequence:
  17          // (By seeding with 9 instead of 0, there is no need to backtrack!)
  18          for (int j = 0; j < N_; j++) sequence_[j] = digits[9];
  19          used_[getCombination(N_ - 1)] = true;
  20  
  21          for (int k = N_; k < sequence_.Length; k++) {
  22              int n = 0;
  23              for (; n < 10; n++) {  // Find a suitable k-th digit
  24                  sequence_[k] = digits[n];
  25                  int combination = getCombination(k);
  26                  if (!used_[combination]) {
  27                      used_[combination] = true;
  28                      break;
  29                      }
  30                  }
  31  
  32              if(n == 10) throw new Exception("Must backtrack");
  33              }
  34          }
  35  
  36  
  37      private int getCombination(int end) {
  38          int result = 0;
  39          for (int start = end - N_ + 1; start <= end; start++) 
  40              result = 10 * result + sequence_[start];
  41          return result;
  42          }
  43  
  44      private int N_;
  45      private bool[] used_;
  46      public char[] sequence_ { get; private set; }
  47  }