// File: /workspace/github_cpp_makefile_star2_O3/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 $0x448, %rsp            # imm = 0x448
movq %rsi, %r12
movl %edi, %ebx
leaq 0x158(%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 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x3498(%rip), %rsi      # 0x6064
leaq 0x349a(%rip), %rdx      # 0x606d
leaq 0x70(%rsp), %rdi
callq 0x3fa2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
cmpl $0x2, %ebx
jl 0x2d0c
movl %ebx, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
movl $0x1, %r14d
leaq 0x160(%rsp), %r13
movq (%r12,%r14,8), %rsi
movq %r13, %rdi
leaq 0x370(%rsp), %rdx
callq 0x365c
movq 0x160(%rsp), %rbp
movsbl (%rbp), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x2c97
movq %rbx, %r15
callq 0x2040
movq %rax, %rbx
movl (%rax), %eax
movl %eax, 0x8(%rsp)
movl $0x0, (%rbx)
movq %rbp, %rdi
leaq 0x370(%rsp), %rsi
movl $0xa, %edx
callq 0x21e0
cmpq %rbp, 0x370(%rsp)
je 0x32a1
movslq %eax, %rcx
movq %rax, 0x18(%rsp)
cmpq %rax, %rcx
jne 0x32ad
movl (%rbx), %eax
cmpl $0x22, %eax
je 0x32ad
testl %eax, %eax
jne 0x2c92
movl 0x8(%rsp), %eax
movl %eax, (%rbx)
movq %r15, %rbx
jmp 0x2cdb
movq %r13, %rdi
movl $0x2e, %esi
xorl %edx, %edx
callq 0x21c0
cmpq $-0x1, %rax
je 0x2cbb
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x20c0
jmp 0x2cdb
leaq 0x128(%rsp), %rdi
movq %rdi, 0x370(%rsp)
movq %r13, %rsi
leaq 0x370(%rsp), %rdx
callq 0x4038
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x2d00
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2160
incq %r14
cmpq %r14, %rbx
jne 0x2c0a
leaq 0x370(%rsp), %rdi
callq 0x4afc
leaq 0x160(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl $0x8, %edx
callq 0x2240
leaq 0x30(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x160(%rsp), %rbx
leaq 0x20(%rsp), %r14
leaq 0x128(%rsp), %r15
leaq 0x370(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2da1
cmpq $0x4, 0x28(%rsp)
jb 0x2d63
movq %r15, %rdi
movq %r14, %rsi
callq 0x4456
testq %rax, %rax
jne 0x2d63
movq %r12, %rdi
movq %r14, %rsi
callq 0x4b44
jmp 0x2d63
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2db8
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0x2060
xorl %eax, %eax
leaq 0x20(%rsp), %r14
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
leaq 0x170(%rsp), %rcx
movq %rcx, (%rbx)
movq %rax, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
movq 0x61ec(%rip), %r15      # 0x8fe0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x21a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
movq 0x160(%rsp), %r12
jne 0x2e47
movq 0x168(%rsp), %r13
testq %r13, %r13
je 0x2e3a
xorl %ebp, %ebp
movzbl (%r12,%rbp), %edi
callq 0x2200
movb %al, (%r12,%rbp)
incq %rbp
cmpq %rbp, %r13
jne 0x2e24
movq %r14, %rdi
movq %rbx, %rsi
callq 0x369a
jmp 0x2df4
leaq 0x170(%rsp), %rax
cmpq %rax, %r12
je 0x2e67
movq 0x170(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2160
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r14
movq 0x8(%rbx), %r15
movslq %r15d, %rsi
leaq 0x40(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x160(%rsp), %rdi
leaq 0xb8(%rsp), %rcx
callq 0x36d8
subq %rbx, %r14
shrq $0x5, %r14
movq %r14, 0x8(%rsp)
movslq %r14d, %rsi
leaq 0x110(%rsp), %rdi
leaq 0x160(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %rdx, %rbp
callq 0x3740
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2ee1
movq 0x180(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
xorl %ebx, %ebx
movq %rbx, 0x170(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
movq %rbx, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x8(%rsp), %rsi
testl %esi, %esi
jle 0x2f69
leaq 0x20(%rsp), %r14
leaq 0x370(%rsp), %r12
testl %r15d, %r15d
jle 0x2f57
xorl %r13d, %r13d
subq $0x8, %rsp
movq %r14, %rdi
movl %r15d, %edx
movq %r12, %rcx
movl %ebx, %r8d
movl %r13d, %r9d
leaq 0x48(%rsp), %rax
pushq %rax
pushq %rbp
leaq 0x128(%rsp), %rax
pushq %rax
callq 0x25cf
addq $0x20, %rsp
incl %r13d
cmpl %r13d, %r15d
movq 0x8(%rsp), %rsi
jne 0x2f1e
incl %ebx
cmpl %esi, %ebx
jne 0x2f16
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rbx
jmp 0x2f6c
xorl %r14d, %r14d
subq %r14, %rbx
sarq $0x3, %rbx
movabsq $0x6db6db6db6db6db7, %r13 # imm = 0x6DB6DB6DB6DB6DB7
imulq %rbx, %r13
movl %esi, %eax
imull %r15d, %eax
leal -0x1(%rsi), %ecx
leal -0x1(%r15), %r12d
imull %r12d, %ecx
movq %rax, 0x68(%rsp)
movq %rcx, 0xb0(%rsp)
addl %ecx, %eax
movl %eax, 0x14(%rsp)
imull %r13d, %eax
movslq %eax, %rbx
movq 0x6005(%rip), %rsi      # 0x8fb8
movq %rbx, %rdi
callq 0x2270
testq %rax, %rax
je 0x3202
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x20d0
movq %r13, 0x60(%rsp)
testl %r13d, %r13d
jle 0x30c4
movq 0x60(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
imulq $0x38, %rcx, %rsi
movq 0x20(%r14,%rsi), %rdi
movq 0x28(%r14,%rsi), %r8
movq %r8, %rdx
subq %rdi, %rdx
je 0x3030
movl 0x14(%rsp), %r9d
imull %ecx, %r9d
movl (%rdi), %r10d
imull %r15d, %r10d
addl %r9d, %r10d
movslq 0x4(%rdi), %r11
movslq %r10d, %r10
addq %r11, %r10
movb $0x1, (%rbp,%r10)
addq $0x8, %rdi
cmpq %r8, %rdi
jne 0x300d
shrq $0x3, %rdx
cmpl $0x2, %edx
jl 0x30b8
addq %r14, %rsi
addq $0x20, %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x14(%rsp), %edi
imull %ecx, %edi
addl 0x68(%rsp), %edi
movl $0x1, %r8d
movq (%rsi), %rbx
movl -0x8(%rbx,%r8,8), %r9d
movl (%rbx,%r8,8), %r11d
movl %r11d, %r10d
subl %r9d, %r10d
incl %r10d
testl $0xfffffffd, %r10d      # imm = 0xFFFFFFFD
jne 0x30b0
movl -0x4(%rbx,%r8,8), %r10d
movl 0x4(%rbx,%r8,8), %r13d
movl %r13d, %ebx
subl %r10d, %ebx
incl %ebx
testl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jne 0x30b0
cmpl %r9d, %r11d
cmovll %r11d, %r9d
imull %r12d, %r9d
cmpl %r10d, %r13d
cmovll %r13d, %r10d
addl %edi, %r10d
addl %r9d, %r10d
movslq %r10d, %r9
movb $0x1, (%rbp,%r9)
incq %r8
cmpq %r8, %rdx
jne 0x3057
incq %rcx
cmpq %rax, %rcx
jne 0x2fee
leaq 0xb8(%rsp), %rdi
callq 0x4bc0
leaq 0xb8(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0xb0(%rsp), %rcx
movq %rbp, %r8
callq 0x4cb6
movq 0x18(%rsp), %r8
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0xd0(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0xb8(%rsp), %rdi
xorl %ecx, %ecx
callq 0x4fe0
movq 0x90(%rsp), %r14
movq 0x98(%rsp), %rbx
cmpq %rbx, %r14
je 0x31c7
leaq 0xf0(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
movq (%r14), %r13
movq 0x8(%r14), %rbp
cmpq %rbp, %r13
je 0x3189
movslq (%r13), %rax
imulq $0x38, %rax, %rsi
addq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x37d8
addq $0x4, %r13
jmp 0x3169
leaq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl %r15d, %edx
movq %r12, %rcx
callq 0x28bb
movl $0x1, %edx
movq 0x5e26(%rip), %rdi      # 0x8fd0
leaq 0x2eb1(%rip), %rsi      # 0x6062
callq 0x2180
movq %r12, %rdi
callq 0x3804
addq $0x18, %r14
cmpq %rbx, %r14
jne 0x314b
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x31e4
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x90(%rsp), %rdi
callq 0x3834
leaq 0xb8(%rsp), %rdi
callq 0x4c0c
xorl %ebx, %ebx
jmp 0x3213
leaq 0x2edf(%rip), %rdi      # 0x60e8
callq 0x2230
movl $0x1, %ebx
leaq 0x40(%rsp), %rdi
callq 0x3804
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x323a
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x110(%rsp), %rdi
callq 0x3888
leaq 0x20(%rsp), %rdi
callq 0x35fe
leaq 0x370(%rsp), %rdi
callq 0x4b08
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x3280
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x128(%rsp), %rdi
callq 0x39ea
movl %ebx, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2df0(%rip), %rdi      # 0x6098
callq 0x20e0
leaq 0x2de4(%rip), %rdi      # 0x6098
callq 0x21f0
movq %rax, %r14
jmp 0x33c6
movq %rax, %r14
jmp 0x33f0
jmp 0x33ff
movq %rax, %r14
movq 0x160(%rsp), %rsi
testq %rsi, %rsi
je 0x3436
leaq 0x160(%rsp), %rdi
callq 0x22d0
jmp 0x3436
movq %rax, %r14
jmp 0x3436
movq %rax, %r14
jmp 0x3440
jmp 0x3313
movq %rax, %r14
jmp 0x346f
movq %rax, %r14
jmp 0x3329
movq %rax, %r14
jmp 0x344d
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x3329
movl 0x8(%rsp), %eax
movl %eax, (%rbx)
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x344d
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x344d
jmp 0x33b6
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x3373
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x160(%rsp), %rdi
callq 0x2060
jmp 0x3440
movq %rax, %r14
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x3436
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x3436
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0x3804
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x33e3
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x90(%rsp), %rdi
callq 0x3834
leaq 0xb8(%rsp), %rdi
callq 0x4c0c
jmp 0x3402
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x3804
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x3429
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x110(%rsp), %rdi
callq 0x3888
leaq 0x20(%rsp), %rdi
callq 0x35fe
leaq 0x370(%rsp), %rdi
callq 0x4b08
leaq 0x80(%rsp), %rax
movq 0x70(%rsp), %rdi
cmpq %rax, %rdi
je 0x346f
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x128(%rsp), %rdi
callq 0x39ea
movq %r14, %rdi
callq 0x2280