QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 110

#13393. 回文

统计

给你一个长度为 $n$ 的字符序列,字符仅由 01 组成,下标为 $1, 2, \dots, n$。初始时,每个字符都代表一个长度为 $1$ 的字符串。在一次合并操作中,选择两个单词 $a$ 和 $b$,将它们删除,并用字符串 $ab$ 代替(即把 $b$ 的字符拼接在 $a$ 的字符后面)。

通过一系列共 $n-1$ 次合并操作,这 $n$ 个初始字符串最终会被合并为一个字符串。第 $i$ 次合并由一对下标 $(a_i, b_i)$ 描述,表示将包含第 $a_i$ 个字符的字符串与包含第 $b_i$ 个字符的字符串进行合并。保证下标为 $a_i$ 和 $b_i$ 的字符当前不在同一个字符串中。

一个字符串 $w$ 的回文值定义为 $w$ 中所有本质不同的回文子串的总数。我们定义回文串为从左往右读和从右往左读完全相同的字符串。字符串的子串定义为通过从字符串的开头和/或结尾删除零个或多个字符而得到的字符串。

对于每一次合并操作,输出合并后得到的新字符串的回文值。

输入格式

第一行包含一个整数 $n$($1 \le n \le 100\,000$),表示字符的数量。

第二行包含一个由 $n$ 个字符 01 组成的字符串,表示初始的字符串。

接下来的 $n-1$ 行中,第 $i$ 行包含两个整数 $a_i$ 和 $b_i$($1 \le a_i, b_i \le n$,$a_i \neq b_i$),表示第 $i$ 次合并操作。

输出格式

输出 $n-1$ 行,每行一个整数,表示每次合并后得到的新字符串的回文值。

数据范围

子任务 分值 数据范围
1 10 $1 \le n \le 100$
2 20 $1 \le n \le 1000$
3 30 对于所有 $i = 1, 2, \dots, n-1$,有 $a_i = 1, b_i = i + 1$
4 50 无附加限制

样例

输入样例 1

3
010
1 2
2 3

输出样例 1

2
3

输入样例 2

5
00111
4 1
1 5
2 1
3 1

输出样例 2

2
3
4
5

输入样例 3

8
10010000
7 5
4 2
3 6
1 3
6 8
5 3
1 2

输出样例 3

2
2
2
3
4
6
8

说明

第三个样例的解释:

每次合并后新创建的字符串依次为:001000100100000100000100010。它们对应的回文值如样例输出所示。

例如,00100010 的回文值为 $8$,因为该字符串包含 $8$ 个本质不同的回文子串:000000100010100010101000100

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.