CF1738H.Palindrome Addicts

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Your task is to maintain a queue consisting of lowercase English letters as follows:

  • "push cc": insert a letter cc 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 00.

A string s[1..n]s[1..n] of length nn is palindromic if s[i]=s[n−i+1]s[i] = s[n-i+1] for every 1≤i≤n1 \leq i \leq n.

The string s[l..r]s[l..r] is a substring of string s[1..n]s[1..n] for every 1≤l≤r≤n1 \leq l \leq r \leq n.

Two strings s[1..n]s[1..n] and t[1..m]t[1..m] are distinct, if at least one of the following holds.

  • n≠mn \neq m;
  • s[i]≠t[i]s[i] \neq t[i] for some 1≤i≤min⁡n,m1 \leq i \leq \min{n,m}.

你的任务是维护一个由小写英文字母组成的队列,具体操作如下:

  • “push cc”:将字母 cc 插入队列尾部;
  • “pop”:从队列头部删除一个字母。

初始时,队列为空。

每次操作后,你需要计算当前队列中从头到尾拼接所得字符串中不同的回文子串的个数。

特别地,空字符串的不同回文子串个数为 00。

长度为 nn 的字符串 s[1..n]s[1..n] 是回文串,当且仅当对所有 1≤i≤n1 \leq i \leq n,均有 s[i]=s[n−i+1]s[i] = s[n-i+1]。

对任意满足 1≤l≤r≤n1 \leq l \leq r \leq n 的整数 l,rl, r,字符串 s[l..r]s[l..r] 是字符串 s[1..n]s[1..n] 的一个子串。

两个字符串 s[1..n]s[1..n] 与 t[1..m]t[1..m] 被称为不同,当且仅当以下任一条件成立:

  • n≠mn \neq m;
  • 存在某个 1≤i≤min⁡{n,m}1 \leq i \leq \min\{n,m\},使得 s[i]≠t[i]s[i] \neq t[i]。

输入格式

The first line is an integer qq (1≤q≤1061 \leq q \leq 10^6), indicating the number of operations.

Then qq lines follow. Each of the following lines contains one of the operations as follows.

  • "push cc": insert a letter cc at the back of the queue, where cc 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.

第一行是一个整数 qq(1≤q≤1061 \leq q \leq 10^6),表示操作的次数。

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

  • “push cc”:将小写字母 cc 插入队列尾部;
  • “pop”:从队列头部删除一个字母。

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

输出格式

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 sks_k be the string presented in the queue after the kk-th operation, and let ckc_k be the number of distinct palindromic substrings of sks_k. The following table shows the details of the example.

kk

sks_k

ckc_k

11

aa

11

22

empty\textsf{empty}

00

33

aa

11

44

aaaa

22

55

aabaab

33

66

aabbaabb

44

77

aabbaaabba

55

88

aabbaaaabbaa

66

99

abbaaabbaa

55

1010

bbaabbaa

44

1111

baabaa

33

1212

baabbaab

44

It is worth pointing out that

  • After the 22-nd operation, the string is empty and thus has no substrings. So the answer is 00;
  • After the 88-th operation, the string is "aabbaaaabbaa". The 66 distinct palindromic substrings are "aa", "aaaa", "aabbaaaabbaa", "abbaabba", "bb", and "bbbb".

令 sks_k 表示第 kk 次操作后队列中呈现的字符串,ckc_k 表示 sks_k 中不同回文子串的个数。下表展示了该示例的详细信息。

kk

sks_k

ckc_k

11

aa

11

22

empty\textsf{empty}

00

33

aa

11

44

aaaa

22

55

aabaab

33

66

aabbaabb

44

77

aabbaaabba

55

88

aabbaaaabbaa

66

99

abbaaabbaa

55

1010

bbaabbaa

44

1111

baabaa

33

1212

baabbaab

44

需要特别指出的是:

  • 第 22 次操作后,字符串为空,因此没有子串,答案为 00;
  • 第 88 次操作后,字符串为 "aabbaaaabbaa"。其 66 个不同的回文子串为 "aa"、"aaaa"、"aabbaaaabbaa"、"abbaabba"、"bb" 和 "bbbb"。

输入解题思路,AI测评打分。不知道怎么写?

首页