This page is still under construction.

Parts of this page are still being built. What you see may change.

Walk

Time limit5sMemory limit256 MB

Summary
Given up to one million missing strings among n-bit names, decide whether two present names are connected through single-bit flips avoiding blocked names.
Level

Medium7 of 10

Topics
BFS, Graph, Bit manipulation, Hash map
Solved
No attempts yet

Problem

The names of towns in Byteotia are distinct binary strings of exactly nn bits. There are 2n−k2^n - k towns in Byteotia, so exactly kk of the length-nn bit sequences name no town.

Some pairs of towns are directly connected by roads. Precisely, two towns are directly linked by a road if and only if their names differ in exactly one bit. Roads never cross outside of towns.

Byteasar wants to take a stroll from town xx to town yy, walking only along existing roads. Write a program that decides whether such a walk from xx to yy is possible.

Input

The first line contains two integers nn and kk separated by a single space (1≤n≤601 \le n \le 60, 0≤k≤1,000,0000 \le k \le 1{,}000{,}000, k≤2n−1k \le 2^n - 1, n⋅k≤5,000,000n \cdot k \le 5{,}000{,}000). Here nn is the length of a town name in bits and kk is the number of length-nn bit sequences that name no town.

The second line contains two strings separated by a single space, each a name of length nn over the characters 0 and 1. These are the names of towns xx and yy.

Each of the next kk lines contains one length-nn bit sequence that names no town, one per line, each a string of 0 and 1. Neither xx nor yy appears among these kk sequences.

Output

Print TAK (Polish for yes) on a single line if a walk from town xx to town yy is possible, and NIE (Polish for no) otherwise.

Hint

For instance, here are two possible walks from 00000000 to 10111011:

  • 0000→1000→1100→1110→1111→10110000 \to 1000 \to 1100 \to 1110 \to 1111 \to 1011
  • 0000→0100→1100→1110→1111→10110000 \to 0100 \to 1100 \to 1110 \to 1111 \to 1011

Examples2

  1. Example 1

    Input
    4 6
    0000 1011
    0110
    0111
    0011
    1101
    1010
    1001
    
    Expected output
    TAK
    
  2. Example 2

    Input
    2 2
    00 11
    01
    10
    
    Expected output
    NIE