Under construction

Still being built. Things may change

Machine Learning

Time limit2sMemory limit1024 MB

Summary
Given every binary word up to length n marked accept or reject, build a DFA with the fewest states that matches all the marks.
Level

Hard9 of 10

Topics
Backtracking, Trie, String
Solved
No attempts yet

Statement

Machine learning is a branch of theoretical computer science that studies what algorithms and computer programs can learn from training examples. In this problem you implement the simplest form of machine learning: train a deterministic finite automaton so that it correctly recognizes the words of a given set.

Formally, a deterministic finite automaton is a tuple ⟨Σ,U,s,T,φ⟩\langle\Sigma, U, s, T, \varphi\rangle, where Σ\Sigma is the finite input alphabet (here Σ={0,1}\Sigma = \{0, 1\}), UU is a finite set of states, s∈Us \in U is the initial state, T⊂UT \subset U is the set of accepting states, and φ:U×Σ→U\varphi : U \times \Sigma \rightarrow U is the transition function.

The input to the automaton is a word α\alpha over Σ\Sigma. Initially the automaton is in state ss. At each step it reads the next symbol cc of the input and moves from its current state uu to φ(u,c)\varphi(u, c). After the whole word is processed, if the automaton is in an accepting state, it accepts α\alpha. Otherwise it rejects α\alpha.

Split all words of length from 0 to a given number nn into two sets, S+S^+ and S−S^-. An automaton matches this split if it accepts every word in S+S^+ and rejects every word in S−S^-. Words longer than nn may be either accepted or rejected.

Build an automaton that matches the given split and has the minimum possible number of states.

Input

The first line contains an integer nn (1≤n≤121 \le n \le 12). The next 2n+1−12^{n+1}-1 lines describe S+S^+ and S−S^-. Each line contains one word preceded by + if the word is in S+S^+, or by - if it is in S−S^-. Words are sorted by length, and words of equal length are sorted lexicographically.

Output

The first line contains uu (u≥1u \ge 1), the minimum number of states, and ss, the number of the initial state. States are numbered from 1 to uu.

The second line contains tt, the number of accepting states, followed by the tt numbers of the accepting states.

Then uu lines follow. The ii-th line contains φ(i,0)\varphi(i, 0) and φ(i,1)\varphi(i, 1), separated by a space.

Examples1

  1. Example 1

    Input
    2
    +
    +0
    -1
    +00
    -01
    -10
    -11
    
    Expected output
    2 1
    1 1
    1 2
    2 2