CF1819F Willy-nilly, Crack, Into Release!

题目描述

考虑长度为 $n$ 的,仅包含前四个小写字母的字符串。 称无序字母对 "ab", "bc", "cd", "da" 是好的。若无序字母对 $(x, y)$ 是好的,那么你可以进行以下操作之一: - 若 $s_n = x$,则将 $s_n$ 改成 $y$。 - 若 $s_i = x$ 且 $s_{i + 1} = s_{i + 2} = \cdots = s_n = y$,则将 $s_i$ 改成 $y$,$s_i$ 之后的字符改成 $x$。 例如,字符串 "bacdd" 可以变成 "bacda", "bacdc", "badcc" 之一;字符串 "aac" 可以变成 "aab" 或 "aad"。 一个字符串 $s$ 的非空操作序列是合法的,当且仅当: 1. 执行完所有操作后,字符串变回 $s$。 2. 操作过程中除了 $s$ 没有字符串出现超过一次。同时 $s$ 出现恰好两次 —— 所有操作之前,和所有操作之后。 设字符串集合 $S$,初始为空。$q$ 次往集合中添加或从集合中删去一个字符串。 每次操作后,求出非空操作序列的最小长度和最大长度,使得 $S$ 的每个字符串在操作过程中出现了至少一次。不存在则输出 $-1$。初始字符串 $s$ 由你自己决定。 $1\leq n\leq 20$,$1\leq q\leq 10 ^ 5$,输入字符串长度为 $n$ 且仅包含前四个小写字母。

输入格式

第一行两个正整数 $n, q$。 接下来 $q$ 行,每行一个长度为 $n$ 的字符串 $t_i$。若 $t_i\in S$,则将 $t_i$ 删去,否则加入 $t_i$。

输出格式

对于每个操作,输出一行一个或两个整数表示答案 —— 无解,或操作序列长度的最小值和最大值。 翻译贡献者:[Alex_Wei](https://www.luogu.com.cn/user/123294)。

说明/提示

Let's consider the first test example. - After the first query, the set of important words is equal to $ \{ $ aa $ \} $ , the minimum sequence of actions has the following form: aa, ab, aa. The maximum sequence of actions that fits is aa, ab, ba, bb, bc, cb, cc, cd, dc, dd, da, ad, aa. - After the second query, the set of important words is equal to $ \{ $ aa, ac $ \} $ . The minimum and maximum sequences of actions are: aa, ab, ac, ad, aa. - After the third query, the set of important words is equal to $ \{ $ aa, ac, dd $ \} $ . There is no sequence of actions that fits the condition, so $ -1 $ should be outputted. - After the fourth query, the set of important words is equal to $ \{ $ aa, dd $ \} $ . The minimum and maximum sequences of actions are as follows: aa, ab, ba, bb, bc, cb, cc, cd, dc, dd, da, ad, aa.