310336: CF1819F. Willy-nilly, Crack, Into Release!

Memory Limit:1024 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

F. Willy-nilly, Crack, Into Release!time limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard output

You have long dreamed of working in a large IT company and finally got a job there. You have studied all existing modern technologies for a long time and are ready to apply all your knowledge in practice. But then you sit down at your desk and see a sheet of paper with the company's motto printed in large letters: abcdabcdabcdabcd....

The company's motto contains four main principles— a (Willi), b (Nilli), c (Crack), d (Release). Therefore, you consider strings of length $n$ consisting of these four Latin letters. Unordered pairs of letters "ab", "bc", "cd", and "da" in this motto are adjacent, so we will call such pairs of symbols good. So, if you are given a string $s$ of length $n$, and it is known that the unordered pair of symbols $\{ x, y \}$ is good, then you can perform one of the following operations on the string:

  • if $s_n = x$, then you are allowed to replace this symbol with $y$,
  • if there exists $1 \le i < n$ such that $s_i = x$ and $s_{i+1} = \ldots = s_n = y$, then you are allowed to replace the $i$-th symbol of the string with $y$, and all subsequent symbols with $x$.

For example, the string bacdd can be replaced with one of the strings bacda, bacdc, or badcc, and the string aac can be replaced with aab or aad.

A non-empty sequence of operations for the string $s$ will be called correct if the following two conditions are met:

  1. after performing all operations, the string becomes $s$ again,
  2. no string, except for $s$, will occur more than once during the operations. At the same time, the string $s$ can occur exactly twice - before the start of the operations and after performing all operations.

Now we are ready to move on to the problem statement! You have a set of strings that is initially empty. Then, each of $q$ queries adds another string $t_i$ to the set, or removes the string $t_i$ from the set. After each query, you need to output the minimum and maximum size of a correct sequence of operations in which each word occurs at least once. The choice of the initial string $s$ is up to you.

Input

The first line contains two integers $n$ and $q$ ($1 \le n \le 20$, $1 \le q \le 100\,000$) — the length of the strings under consideration and the number of queries to modify the set of strings.

Each of the next $q$ lines contains a string $t_i$ ($\lvert t_i \rvert = n$). All strings consist of characters "a", "b", "c" and "d". If the string $t_i$ was not in the set before the query, it is added to the set, otherwise it is removed from the set.

Output

For each of the $q$ queries, output two integers: the minimum and maximum size of a correct sequence of operations in which each word from the set appears at least once.

If there is no sequence of operations that satisfies the condition of the problem, output a single number $-1$.

ExamplesInput
2 4
aa
ac
dd
ac
Output
2 12
4 4
-1
12 12
Input
3 2
acc
bdd
Output
2 44
28 44
Note

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.

Input

题意翻译

### 题目描述 考虑长度为 $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)。

Output

题目大意:给定一个字符串s,长度为n,包含字符a、b、c、d。定义“好对”为(ab, bc, cd, da)。每次操作可以将字符串中的某个字符x替换为y,或者将某个字符x及其后面的所有字符y替换为y和x。定义一个操作序列是“正确的”,如果满足:1. 执行所有操作后,字符串变回s;2. 除s外,其他字符串在操作过程中不会出现超过一次。现在有q个查询,每个查询将一个字符串加入集合或从集合中删除。对于每个查询,输出满足上述条件的操作序列的最小和最大长度。

输入输出数据格式:

输入:
- 第一行包含两个整数n和q,分别表示字符串长度和查询次数。
- 接下来q行,每行包含一个长度为n的字符串,由字符a、b、c、d组成。如果字符串不在集合中,则将其加入集合;如果已在集合中,则将其删除。

输出:
- 对于每个查询,输出两个整数,分别表示正确的操作序列的最小和最大长度。如果不存在满足条件的操作序列,则输出-1。题目大意:给定一个字符串s,长度为n,包含字符a、b、c、d。定义“好对”为(ab, bc, cd, da)。每次操作可以将字符串中的某个字符x替换为y,或者将某个字符x及其后面的所有字符y替换为y和x。定义一个操作序列是“正确的”,如果满足:1. 执行所有操作后,字符串变回s;2. 除s外,其他字符串在操作过程中不会出现超过一次。现在有q个查询,每个查询将一个字符串加入集合或从集合中删除。对于每个查询,输出满足上述条件的操作序列的最小和最大长度。 输入输出数据格式: 输入: - 第一行包含两个整数n和q,分别表示字符串长度和查询次数。 - 接下来q行,每行包含一个长度为n的字符串,由字符a、b、c、d组成。如果字符串不在集合中,则将其加入集合;如果已在集合中,则将其删除。 输出: - 对于每个查询,输出两个整数,分别表示正确的操作序列的最小和最大长度。如果不存在满足条件的操作序列,则输出-1。

加入题单

算法标签: