A12156 | Speckled Band
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Ildar took a band (a thin strip of cloth) and colored it. Formally, the band has $n$ cells, each of them is colored into one of $26$ 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]$ ( $1 \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 = s_l s_{l+1} \ldots s_r$ .
After that Ildar will play the following game: he cuts the band $t$ into some new bands and counts the number of different bands among them. Formally, Ildar chooses $1 \le k \le |t|$ indexes $1 \le i_1 < i_2 < \ldots < i_k = |t|$ and cuts $t$ to $k$ bands-strings $t_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 $t$ in such a way, the result of the game is -1.
Unfortunately Ildar hasn't yet decided which segment he likes, but he has $q$ segments-candidates $[l_1, r_1]$ , $[l_2, r_2]$ , ..., $[l_q, r_q]$ . Your task is to calculate the result of the game for each of them.
Ildar decided to take some segment of the band $[l, r]$ ( $1 \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 = s_l s_{l+1} \ldots s_r$ .
After that Ildar will play the following game: he cuts the band $t$ into some new bands and counts the number of different bands among them. Formally, Ildar chooses $1 \le k \le |t|$ indexes $1 \le i_1 < i_2 < \ldots < i_k = |t|$ and cuts $t$ to $k$ bands-strings $t_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 $t$ in such a way, the result of the game is -1.
Unfortunately Ildar hasn't yet decided which segment he likes, but he has $q$ segments-candidates $[l_1, r_1]$ , $[l_2, r_2]$ , ..., $[l_q, r_q]$ . Your task is to calculate the result of the game for each of them.
输入格式
The first line contains one integer $n$ ( $1 \le n \le 200\,000$ ) — the length of the band Ildar has.
The second line contains a string $s$ consisting of $n$ lowercase English letters — the band Ildar has.
The third line contains a single integer $q$ ( $1 \le q \le 200\,000$ ) — the number of segments Ildar has chosen as candidates.
Each of the next $q$ lines contains two integer integers $l_i$ and $r_i$ ( $1 \le l_i \le r_i \le n$ ) denoting the ends of the $i$ -th segment.
The second line contains a string $s$ consisting of $n$ lowercase English letters — the band Ildar has.
The third line contains a single integer $q$ ( $1 \le q \le 200\,000$ ) — the number of segments Ildar has chosen as candidates.
Each of the next $q$ lines contains two integer integers $l_i$ and $r_i$ ( $1 \le l_i \le r_i \le n$ ) denoting the ends of the $i$ -th segment.
输出格式
Output $q$ lines, where the $i$ -th of them should contain the result of the game on the segment $[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]$ , he cuts a string $t = abcabc$ . If he cuts $t$ into two bands $abc$ and $abc$ , the band $abc$ repeats two times and the number of different tapes is $1$ . So, the result of this game is $1$ .
If Ildar chooses the segment $[4, 7]$ , he cuts a string $t = 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$ .
If Ildar chooses the segment $[3, 6]$ , he cuts a string $t = cabc$ . If he cuts $t$ into three bands $c$ , $ab$ and $c$ , the band $c$ repeats two times and the number of different bands is $2$ . So, the result of this game is $2$ .
If Ildar chooses the segment $[1, 6]$ , he cuts a string $t = abcabc$ . If he cuts $t$ into two bands $abc$ and $abc$ , the band $abc$ repeats two times and the number of different tapes is $1$ . So, the result of this game is $1$ .
If Ildar chooses the segment $[4, 7]$ , he cuts a string $t = 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$ .
If Ildar chooses the segment $[3, 6]$ , he cuts a string $t = cabc$ . If he cuts $t$ into three bands $c$ , $ab$ and $c$ , the band $c$ repeats two times and the number of different bands is $2$ . So, the result of this game is $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted