309808: CF1738H. Palindrome Addicts

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

Description

Palindrome Addicts

题意翻译

你需要维护一个字符串,初始时字符串为空,然后有 $q(1\le q\le10^6)$ 次操作,每次操作会删除字符串的第一个字符或者在字符串尾部加上一个英文小写字符,求每次操作后该字符串的本质不同回文串有多少个。

题目描述

Your task is to maintain a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) consisting of lowercase English letters as follows: - "push $ c $ ": insert a letter $ c $ at the back of the queue; - "pop": delete a letter from the front of the queue. Initially, the queue is empty. After each operation, you are asked to count the number of distinct palindromic substrings in the string that are obtained by concatenating the letters from the front to the back of the queue. Especially, the number of distinct palindromic substrings of the empty string is $ 0 $ . A string $ s[1..n] $ of length $ n $ is palindromic if $ s[i] = s[n-i+1] $ for every $ 1 \leq i \leq n $ . The string $ s[l..r] $ is a substring of string $ s[1..n] $ for every $ 1 \leq l \leq r \leq n $ . Two strings $ s[1..n] $ and $ t[1..m] $ are distinct, if at least one of the following holds. - $ n \neq m $ ; - $ s[i] \neq t[i] $ for some $ 1 \leq i \leq \min\{n,m\} $ .

输入输出格式

输入格式


The first line is an integer $ q $ ( $ 1 \leq q \leq 10^6 $ ), indicating the number of operations. Then $ q $ lines follow. Each of the following lines contains one of the operations as follows. - "push $ c $ ": insert a letter $ c $ at the back of the queue, where $ c $ is a lowercase English letter; - "pop": delete a letter from the front of the queue. It is guaranteed that no "pop" operation will be performed when the queue is empty.

输出格式


After each operation, print the number of distinct palindromic substrings in the string presented in the queue.

输入输出样例

输入样例 #1

12
push a
pop
push a
push a
push b
push b
push a
push a
pop
pop
pop
push b

输出样例 #1

1
0
1
2
3
4
5
6
5
4
3
4

说明

Let $ s_k $ be the string presented in the queue after the $ k $ -th operation, and let $ c_k $ be the number of distinct palindromic substrings of $ s_k $ . The following table shows the details of the example. $ k $ $ s_k $ $ c_k $ $ 1 $ $ a $ $ 1 $ $ 2 $ $ \textsf{empty} $ $ 0 $ $ 3 $ $ a $ $ 1 $ $ 4 $ $ aa $ $ 2 $ $ 5 $ $ aab $ $ 3 $ $ 6 $ $ aabb $ $ 4 $ $ 7 $ $ aabba $ $ 5 $ $ 8 $ $ aabbaa $ $ 6 $ $ 9 $ $ abbaa $ $ 5 $ $ 10 $ $ bbaa $ $ 4 $ $ 11 $ $ baa $ $ 3 $ $ 12 $ $ baab $ $ 4 $ It is worth pointing out that - After the $ 2 $ -nd operation, the string is empty and thus has no substrings. So the answer is $ 0 $ ; - After the $ 8 $ -th operation, the string is " $ aabbaa $ ". The $ 6 $ distinct palindromic substrings are " $ a $ ", " $ aa $ ", " $ aabbaa $ ", " $ abba $ ", " $ b $ ", and " $ bb $ ".

Input

题意翻译

你需要维护一个字符串,初始时字符串为空,然后有 $q(1\le q\le10^6)$ 次操作,每次操作会删除字符串的第一个字符或者在字符串尾部加上一个英文小写字符,求每次操作后该字符串的本质不同回文串有多少个。

Output

**回文串爱好者**

**题意翻译:**
你需要维护一个字符串,初始时字符串为空,然后有 $ q (1 \le q \le 10^6) $ 次操作,每次操作会删除字符串的第一个字符或者在字符串尾部加上一个英文小写字符,求每次操作后该字符串的本质不同回文子串有多少个。

**题目描述:**
你的任务是按照以下方式维护一个由英文小写字母组成的队列:

- "push $ c $": 在队列的尾部插入一个字母 $ c $;
- "pop": 删除队列前端的字母。

最初,队列是空的。

在每次操作之后,你需要计算通过将队列前端的字母到尾端的字母连起来形成的字符串中,有多少个不同的回文子串。

特别是,空字符串的回文子串数量为 $ 0 $。

一个长度为 $ n $ 的字符串 $ s[1..n] $ 是回文的,如果对于所有的 $ 1 \leq i \leq n $ 都有 $ s[i] = s[n-i+1] $。

字符串 $ s[l..r] $ 是字符串 $ s[1..n] $ 的子串,对于所有的 $ 1 \leq l \leq r \leq n $ 都成立。

两个字符串 $ s[1..n] $ 和 $ t[1..m] $ 是不同的,如果至少满足以下条件之一:

- $ n \neq m $;
- 对于某些 $ 1 \leq i \leq \min\{n,m\} $,有 $ s[i] \neq t[i] $。

**输入输出格式:**

**输入格式:**
第一行是一个整数 $ q $ ($ 1 \leq q \leq 10^6 $),表示操作的数量。

接下来有 $ q $ 行,每行包含以下操作之一:

- "push $ c $": 在队列的尾部插入一个字母 $ c $,其中 $ c $ 是一个英文小写字母;
- "pop": 删除队列前端的字母。

保证在队列为空时不会执行 "pop" 操作。

**输出格式:**
在每次操作之后,打印队列中呈现的字符串中不同回文子串的数量。

**输入输出样例:**

**输入样例 #1:**
```
12
push a
pop
push a
push a
push b
push b
push a
push a
pop
pop
pop
push b
```

**输出样例 #1:**
```
1
0
1
2
3
4
5
6
5
4
3
4
```

**说明:**
让 $ s_k $ 表示执行第 $ k $ 次操作后队列中呈现的字符串,$ c_k $ 表示 $ s_k $ 的不同回文子串的数量。下表显示了示例的详细信息。

| $ k $ | $ s_k $ | $ c_k $ |
|-------|---------|---------|
| 1 | a | 1 |
| 2 | (空) | 0 |
| 3 | a | 1 |
| 4 | aa | 2 |
| 5 | aab | 3 |
| 6 | aabb | 4 |
| 7 | aabba | 5 |
| 8 | aabbaa | 6 |
| 9 | abbaa | 5 |
| 10 | bbaa | 4 |
| 11 | baa | 3 |
| 12 | baab | 4 |

值得指出的是:

- 在第 2 次操作之后,字符串为空,因此没有子串。所以答案是 0;
- 在第 8 次操作之后,字符串是 "aabbaa"。6 个不同的回文子串是 "a", "aa", "aabbaa", "abba", "b", 和 "bb"。**回文串爱好者** **题意翻译:** 你需要维护一个字符串,初始时字符串为空,然后有 $ q (1 \le q \le 10^6) $ 次操作,每次操作会删除字符串的第一个字符或者在字符串尾部加上一个英文小写字符,求每次操作后该字符串的本质不同回文子串有多少个。 **题目描述:** 你的任务是按照以下方式维护一个由英文小写字母组成的队列: - "push $ c $": 在队列的尾部插入一个字母 $ c $; - "pop": 删除队列前端的字母。 最初,队列是空的。 在每次操作之后,你需要计算通过将队列前端的字母到尾端的字母连起来形成的字符串中,有多少个不同的回文子串。 特别是,空字符串的回文子串数量为 $ 0 $。 一个长度为 $ n $ 的字符串 $ s[1..n] $ 是回文的,如果对于所有的 $ 1 \leq i \leq n $ 都有 $ s[i] = s[n-i+1] $。 字符串 $ s[l..r] $ 是字符串 $ s[1..n] $ 的子串,对于所有的 $ 1 \leq l \leq r \leq n $ 都成立。 两个字符串 $ s[1..n] $ 和 $ t[1..m] $ 是不同的,如果至少满足以下条件之一: - $ n \neq m $; - 对于某些 $ 1 \leq i \leq \min\{n,m\} $,有 $ s[i] \neq t[i] $。 **输入输出格式:** **输入格式:** 第一行是一个整数 $ q $ ($ 1 \leq q \leq 10^6 $),表示操作的数量。 接下来有 $ q $ 行,每行包含以下操作之一: - "push $ c $": 在队列的尾部插入一个字母 $ c $,其中 $ c $ 是一个英文小写字母; - "pop": 删除队列前端的字母。 保证在队列为空时不会执行 "pop" 操作。 **输出格式:** 在每次操作之后,打印队列中呈现的字符串中不同回文子串的数量。 **输入输出样例:** **输入样例 #1:** ``` 12 push a pop push a push a push b push b push a push a pop pop pop push b ``` **输出样例 #1:** ``` 1 0 1 2 3 4 5 6 5 4 3 4 ``` **说明:** 让 $ s_k $ 表示执行第 $ k $ 次操作后队列中呈现的字符串,$ c_k $ 表示 $ s_k $ 的不同回文子串的数量。下表显示了示例的详细信息。 | $ k $ | $ s_k $ | $ c_k $ | |-------|---------|---------| | 1 | a | 1 | | 2 | (空) | 0 | | 3 | a | 1 | | 4 | aa | 2 | | 5 | aab | 3 | | 6 | aabb | 4 | | 7 | aabba | 5 | | 8 | aabbaa | 6 | | 9 | abbaa | 5 | | 10 | bbaa | 4 | | 11 | baa | 3 | | 12 | baab | 4 | 值得指出的是: - 在第 2 次操作之后,字符串为空,因此没有子串。所以答案是 0; - 在第 8 次操作之后,字符串是 "aabbaa"。6 个不同的回文子串是 "a", "aa", "aabbaa", "abba", "b", 和 "bb"。

加入题单

算法标签: