// File: /workspace/github_cpp_makefile_star2_O2/razimantv[P]DancingLinks/spangram/spangram.cpp
// Function: main
int main(int argc, char* argv[]) {
  std::unordered_set<std::string> exclude; // Words to exclude
  std::string filename = "words.txt";      // Default dictionary file
  int maxsel = -1;                         // Maximum number of words to select

  // Parse command line arguments
  //    Number: Maximum number of words to select
  //    Dot in the argument: Filename
  //    Otherwise: Words to exclude
  for (int i = 1; i < argc; i++) {
    auto cur = std::string(argv[i]);
    if (isdigit(cur[0])) {
      maxsel = std::stoi(cur);
    } else if (cur.find('.') != std::string::npos) {
      filename = cur;
    } else {
      exclude.insert(cur);
    }
  }

  // Create trie from all the words in the dictionary with >= 4 characters
  TrieNode trie;
  {
    std::ifstream dictionary(filename);
    for (std::string word; dictionary >> word;)
      if (word.size() >= 4 and not exclude.count(word)) trie.insert(word);
  }

  // Read the grid from the input file
  Grid grid;
  {
    for (std::string line; std::cin >> line;) {
      std::transform(line.begin(), line.end(), line.begin(),
                     [](unsigned char c) { return std::tolower(c); });
      grid.push_back(line);
    }
  }
  int rows = grid.size(), cols = grid[0].size();

  // Find all placements of words in the grid
  std::vector<std::vector<bool>> visited(rows, std::vector<bool>(cols, false));
  Path path;
  std::vector<Placement> placements;
  for (int i = 0; i < rows; ++i)
    for (int j = 0; j < cols; ++j)
      find_placements(grid, rows, cols, trie, i, j, visited, path, placements);

  // Set up the adjacency matrix for dancing links
  // We want to cover the cells of the grid with words
  // Essential constraints: Every cell is covered by exactly one word
  // Non-essential constraints: The centre of a 2x2 square of characters can
  //    have at most one diagonal line passing through it (paths cannot cross)
  int R = placements.size(), C1 = rows * cols, C2 = (rows - 1) * (cols - 1),
      C = C1 + C2;
  char* mat = new (std::nothrow) char[R * C];
  if (mat == NULL) {
    printf("Memory allocation error\n");
    return 1;
  }
  memset(mat, 0, R * C);

  // Fill the adjacency matrix with the placements
  for (int i = 0; i < R; ++i) {
    const auto& [word, path] = placements[i];
    int l = path.size();
    for (const auto& [x, y] : path) mat[i * C + x * cols + y] = 1;
    for (int k = 1; k < l; ++k) {
      int x1 = path[k - 1].first, y1 = path[k - 1].second, x2 = path[k].first,
          y2 = path[k].second, dx = x2 - x1, dy = y2 - y1;
      if (abs(dx) == 1 and abs(dy) == 1)
        mat[i * C + C1 + std::min(x1, x2) * (cols - 1) + std::min(y1, y2)] = 1;
    }
  }

  // Now just solve it with dancing links
  DancingLinks D;
  D.Create(R, C1, C2, mat);
  std::vector<std::vector<int>> all;
  std::vector<int> cur;
  D.solve(all, cur, false, maxsel);

  // Print all solutions
  for (auto& sol : all) {
    std::vector<Placement> solution_placements;
    for (int i : sol) solution_placements.push_back(placements[i]);
    print_solution(grid, rows, cols, solution_placements);
    std::cout << "\n";
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rsi, %r14
movl %edi, %ebx
leaq 0x148(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x3613(%rip), %rsi      # 0x6064
leaq 0xe0(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x3256
movslq %ebx, %rbx
pushq $-0x1
popq %rax
movl %eax, 0x14(%rsp)
pushq $0x1
popq %rbp
leaq 0x150(%rsp), %r15
leaq 0x360(%rsp), %r12
pushq $0x2e
popq %r13
cmpq %rbx, %rbp
jge 0x2b03
movq (%r14,%rbp,8), %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x3256
movq 0x150(%rsp), %rax
movsbl (%rax), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x2ac1
movq %r15, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x3291
movl %eax, 0x14(%rsp)
jmp 0x2af6
movq %r15, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x21f0
cmpq $-0x1, %rax
je 0x2ae6
leaq 0xe0(%rsp), %rdi
movq %r15, %rsi
callq 0x20e0
jmp 0x2af6
leaq 0x118(%rsp), %rdi
movq %r15, %rsi
callq 0x3cd0
movq %r15, %rdi
callq 0x20c0
incq %rbp
jmp 0x2a87
leaq 0x360(%rsp), %rdi
callq 0x48e0
leaq 0x150(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x2270
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x150(%rsp), %rbx
leaq 0x50(%rsp), %r14
leaq 0x118(%rsp), %r15
leaq 0x360(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2b94
cmpq $0x4, 0x58(%rsp)
jb 0x2b56
movq %r15, %rdi
movq %r14, %rsi
callq 0x4140
testq %rax, %rax
jne 0x2b56
movq %r12, %rdi
movq %r14, %rsi
callq 0x492a
jmp 0x2b56
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
leaq 0x150(%rsp), %r15
movq %r15, %rdi
callq 0x2060
andq $0x0, 0x10(%r14)
leaq 0x160(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movb $0x0, (%rax)
movq 0x7408(%rip), %rbx      # 0x9fe0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2c28
movq 0x150(%rsp), %r12
movq 0x158(%rsp), %r13
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x2c1b
movzbl (%r12,%rbp), %edi
callq 0x2230
movb %al, (%r12,%rbp)
incq %rbp
jmp 0x2c03
movq %r14, %rdi
movq %r15, %rsi
callq 0x32ae
jmp 0x2bd8
leaq 0x150(%rsp), %rdi
callq 0x20c0
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r14
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rsp)
movslq %eax, %rsi
leaq 0x30(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x150(%rsp), %rdi
leaq 0x98(%rsp), %rcx
callq 0x32da
subq %rbx, %r14
shrq $0x5, %r14
movslq %r14d, %rsi
leaq 0xc8(%rsp), %rdi
leaq 0x150(%rsp), %rbx
leaq 0x70(%rsp), %rcx
movq %rbx, %rdx
callq 0x333e
movq %rbx, %rbp
movq %rbx, %rdi
callq 0x3394
xorps %xmm0, %xmm0
andq $0x0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
andq $0x0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
testl %eax, %eax
movl $0x0, %r13d
cmovgl %eax, %r13d
testl %r14d, %r14d
movl $0x0, %eax
movq %r14, 0x28(%rsp)
cmovgl %r14d, %eax
movl %eax, 0x18(%rsp)
leaq 0x50(%rsp), %r14
leaq 0x360(%rsp), %r12
cmpl 0x18(%rsp), %ebx
je 0x2d38
xorl %r15d, %r15d
cmpl %r15d, %r13d
je 0x2d34
subq $0x8, %rsp
movq %r14, %rdi
movq 0x30(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
movl %ebx, %r8d
movl %r15d, %r9d
leaq 0x38(%rsp), %rax
pushq %rax
pushq %rbp
leaq 0xe0(%rsp), %rax
pushq %rax
callq 0x24f4
addq $0x20, %rsp
incl %r15d
jmp 0x2cf7
incl %ebx
jmp 0x2cee
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rax
subq %r14, %rax
pushq $0x38
popq %rcx
cqto
idivq %rcx
movq %rax, %r13
movq 0x28(%rsp), %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
imull %ecx, %edx
leal -0x1(%rax), %ebp
leal -0x1(%rcx), %r15d
imull %r15d, %ebp
movq %rdx, 0x18(%rsp)
leal (%rdx,%rbp), %eax
movl %eax, 0x20(%rsp)
imull %r13d, %eax
movslq %eax, %rbx
movq 0x722c(%rip), %rsi      # 0x9fb0
movq %rbx, %rdi
callq 0x22a0
testq %rax, %rax
je 0x2f99
movq %rax, %r12
movq %rbp, 0x90(%rsp)
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x20f0
movl 0x20(%rsp), %eax
testl %r13d, %r13d
movl $0x0, %ecx
movq %r13, 0x20(%rsp)
cmovgl %r13d, %ecx
cmpq %rcx, %rbp
je 0x2e82
imulq $0x38, %rbp, %rdx
movq 0x20(%r14,%rdx), %rsi
movq 0x28(%r14,%rdx), %r9
movq %r9, %r8
subq %rsi, %r8
movq %rbp, %rdi
imulq %rax, %rdi
movq %rsi, %r10
cmpq %r9, %r10
je 0x2e0f
movl (%r10), %edx
imull 0x8(%rsp), %edx
addl %edi, %edx
movslq 0x4(%r10), %r11
movslq %edx, %rdx
addq %r11, %rdx
movb $0x1, (%r12,%rdx)
addq $0x8, %r10
jmp 0x2deb
shrq $0x3, %r8
movslq %r8d, %r8
addl 0x18(%rsp), %edi
pushq $0x1
popq %r9
cmpq %r8, %r9
jge 0x2e7a
movl -0x8(%rsi,%r9,8), %r10d
movl (%rsi,%r9,8), %ebx
movl %ebx, %edx
subl %r10d, %edx
incl %edx
testl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
jne 0x2e75
movl -0x4(%rsi,%r9,8), %r11d
movl 0x4(%rsi,%r9,8), %r13d
movl %r13d, %edx
subl %r11d, %edx
incl %edx
testl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
jne 0x2e75
cmpl %r10d, %ebx
cmovll %ebx, %r10d
imull %r15d, %r10d
cmpl %r11d, %r13d
cmovll %r13d, %r11d
addl %edi, %r11d
addl %r10d, %r11d
movslq %r11d, %rdx
movb $0x1, (%r12,%rdx)
incq %r9
jmp 0x2e1e
incq %rbp
jmp 0x2dc4
leaq 0x98(%rsp), %rdi
callq 0x499c
leaq 0x98(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x90(%rsp), %rcx
movq %r12, %r8
callq 0x4a86
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x100(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
leaq 0x98(%rsp), %rdi
xorl %ecx, %ecx
movl 0x14(%rsp), %r8d
callq 0x4d98
movq 0x70(%rsp), %r14
movq 0x78(%rsp), %r15
leaq 0xb0(%rsp), %r12
leaq 0x3164(%rip), %rbp      # 0x6062
cmpq %r15, %r14
je 0x2f71
andq $0x0, 0xc0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movq (%r14), %rbx
movq 0x8(%r14), %r13
cmpq %r13, %rbx
je 0x2f3d
movslq (%rbx), %rax
imulq $0x38, %rax, %rsi
addq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x33a4
addq $0x4, %rbx
jmp 0x2f1e
leaq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
callq 0x2793
movq 0x706d(%rip), %rdi      # 0x9fc8
movq %rbp, %rsi
callq 0x2180
movq %r12, %rdi
callq 0x33d0
addq $0x18, %r14
jmp 0x2efe
leaq 0x100(%rsp), %rdi
callq 0x475e
leaq 0x70(%rsp), %rdi
callq 0x33f2
leaq 0x98(%rsp), %rdi
callq 0x49e4
xorl %ebx, %ebx
jmp 0x2fa8
leaq 0x3148(%rip), %rdi      # 0x60e8
callq 0x2260
pushq $0x1
popq %rbx
leaq 0x30(%rsp), %rdi
callq 0x33d0
leaq 0x150(%rsp), %rdi
callq 0x3492
leaq 0xc8(%rsp), %rdi
callq 0x3414
leaq 0x50(%rsp), %rdi
callq 0x3234
leaq 0x360(%rsp), %rdi
callq 0x48ec
leaq 0xe0(%rsp), %rdi
callq 0x20c0
leaq 0x118(%rsp), %rdi
callq 0x35ea
movl %ebx, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x30b2
movq %rax, %rbx
jmp 0x30c9
jmp 0x30d8
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x3394
jmp 0x30ff
movq %rax, %rbx
jmp 0x30ff
movq %rax, %rbx
jmp 0x3109
jmp 0x306a
movq %rax, %rbx
jmp 0x3123
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x20c0
jmp 0x3116
movq %rax, %rbx
jmp 0x3116
jmp 0x30a2
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x20c0
leaq 0x150(%rsp), %rdi
callq 0x2060
jmp 0x3109
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x20c0
jmp 0x30ff
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x33d0
leaq 0x100(%rsp), %rdi
callq 0x475e
leaq 0x70(%rsp), %rdi
callq 0x33f2
leaq 0x98(%rsp), %rdi
callq 0x49e4
jmp 0x30db
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x33d0
leaq 0x150(%rsp), %rdi
callq 0x3492
leaq 0xc8(%rsp), %rdi
callq 0x3414
leaq 0x50(%rsp), %rdi
callq 0x3234
leaq 0x360(%rsp), %rdi
callq 0x48ec
leaq 0xe0(%rsp), %rdi
callq 0x20c0
leaq 0x118(%rsp), %rdi
callq 0x35ea
movq %rbx, %rdi
callq 0x22b0