CF1043G.Speckled Band

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Ildar took a band (a thin strip of cloth) and colored it. Formally, the band has nn cells, each of them is colored into one of 2626 colors, so we can denote each color with one of the lowercase letters of English alphabet.

Ildar decided to take some segment of the band [l,r][l, r] (1≤l≤r≤n1 \le l \le r \le n) he likes and cut it from the band. So he will create a new band that can be represented as a string t=slsl+1…srt = s_l s_{l+1} \ldots s_r.

After that Ildar will play the following game: he cuts the band tt into some new bands and counts the number of different bands among them. Formally, Ildar chooses 1≤k≤∣t∣1 \le k \le |t| indexes 1≤i1<i2<…<ik=∣t∣1 \le i_1 \lt i_2 \lt \ldots \lt i_k = |t| and cuts tt to kk bands-strings t1t2…ti1,ti1+1…ti2,…,tik−1+1…tikt_1 t_2 \ldots t_{i_1}, t_{i_1 + 1} \ldots t_{i_2}, \ldots, {t_{i_{k-1} + 1}} \ldots t_{i_k} and counts the number of different bands among them. He wants to know the minimal possible number of different bands he can get under the constraint that at least one band repeats at least two times. The result of the game is this number. If it is impossible to cut tt in such a way, the result of the game is -1.

Unfortunately Ildar hasn't yet decided which segment he likes, but he has qq segments-candidates [l1,r1][l_1, r_1], [l2,r2][l_2, r_2], ..., [lq,rq][l_q, r_q]. Your task is to calculate the result of the game for each of them.

伊尔达尔取了一条带子(一条细长的布条)并给它染色。形式化地说,这条带子有 nn 个格子,每个格子被染成 2626 种颜色之一,因此我们可以用英文字母表中的一个小写字母来表示每种颜色。

伊尔达尔决定选取他喜欢的带子上某个区间 [l,r][l, r](其中 1≤l≤r≤n1 \le l \le r \le n)并将该区间从原带子上剪下。于是他将得到一条新带子,可表示为字符串 t=slsl+1…srt = s_l s_{l+1} \ldots s_r。

之后,伊尔达尔将进行如下游戏:他将带子 tt 切割成若干新的带子,并统计其中互不相同的带子的数量。形式化地说,伊尔达尔选择 1≤k≤∣t∣1 \le k \le |t| 个下标 1≤i1<i2<…<ik=∣t∣1 \le i_1 \lt i_2 \lt \ldots \lt i_k = |t|,将 tt 切割为 kk 条子带(即子字符串):
t1t2…ti1t_1 t_2 \ldots t_{i_1},
ti1+1…ti2t_{i_1 + 1} \ldots t_{i_2},
…\ldots,
tik−1+1…tikt_{i_{k-1} + 1} \ldots t_{i_k},
并统计这些子带中互不相同的数量。他希望在“至少有一条子带出现不少于两次”的约束下,使互不相同的子带数量尽可能小。该最小值即为本游戏的结果。若无法按此约束切割 tt,则游戏结果为 −1-1。

遗憾的是,伊尔达尔尚未确定他究竟喜欢哪一段,但他已有 qq 个候选区间:[l1,r1][l_1, r_1]、[l2,r2][l_2, r_2]、…、[lq,rq][l_q, r_q]。你的任务是对每个候选区间,计算对应的游戏结果。

输入格式

The first line contains one integer nn (1≤n≤200 0001 \le n \le 200\,000) — the length of the band Ildar has.

The second line contains a string ss consisting of nn lowercase English letters — the band Ildar has.

The third line contains a single integer qq (1≤q≤200 0001 \le q \le 200\,000) — the number of segments Ildar has chosen as candidates.

Each of the next qq lines contains two integer integers lil_i and rir_i (1≤li≤ri≤n1 \le l_i \le r_i \le n) denoting the ends of the ii-th segment.

第一行包含一个整数 nn(1≤n≤200 0001 \le n \le 200\,000)—— 表示 Ildar 所拥有的带子的长度。

第二行包含一个由 nn 个小写英文字母组成的字符串 ss —— 表示 Ildar 所拥有的带子。

第三行包含一个整数 qq(1≤q≤200 0001 \le q \le 200\,000)—— 表示 Ildar 所选出的候选区间的数量。

接下来的 qq 行中,每行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤n1 \le l_i \le r_i \le n),表示第 ii 个区间的左右端点。

输出格式

Output qq lines, where the ii-th of them should contain the result of the game on the segment [li,ri][l_i, r_i].

输出 qq 行,其中第 ii 行应包含区间 [li,ri][l_i, r_i] 上游戏的结果。

输入输出样例

  • 输入#1

    9
    abcabcdce
    7
    1 6
    4 7
    5 9
    6 9
    1 9
    3 6
    4 4

    输出#1

    1
    -1
    4
    3
    2
    2
    -1

说明/提示

Consider the first example.

If Ildar chooses the segment [1,6][1, 6], he cuts a string t=abcabct = abcabc. If he cuts tt into two bands abcabc and abcabc, the band abcabc repeats two times and the number of different tapes is 11. So, the result of this game is 11.

If Ildar chooses the segment [4,7][4, 7], he cuts a string t=abcdt = abcd. It is impossible to cut this band in such a way that there is at least one band repeating at least two times. So, the result of this game is −1-1.

If Ildar chooses the segment [3,6][3, 6], he cuts a string t=cabct = cabc. If he cuts tt into three bands cc, abab and cc, the band cc repeats two times and the number of different bands is 22. So, the result of this game is 22.

考虑第一个例子。

如果伊尔达尔选择区间 [1,6][1, 6],他将截取字符串 t=abcabct = abcabc。若将 tt 切割成两个条带 abcabc 和 abcabc,则条带 abcabc 重复了两次,且不同条带的数量为 11。因此,该轮游戏的结果为 11。

如果伊尔达尔选择区间 [4,7][4, 7],他将截取字符串 t=abcdt = abcd。此时无法以某种方式切割该条带,使得至少有一个条带重复出现至少两次。因此,该轮游戏的结果为 −1-1。

如果伊尔达尔选择区间 [3,6][3, 6],他将截取字符串 t=cabct = cabc。若将 tt 切割成三个条带 cc、abab 和 cc,则条带 cc 重复了两次,且不同条带的数量为 22。因此,该轮游戏的结果为 22。

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

首页