luogu#P8304. [CoE R4 D] 01 串
[CoE R4 D] 01 串
Problem Description
Define a good string that satisfies the following conditions:
-
is non-empty.
-
For any prefix , the number of is not greater than the number of .
-
For any suffix $\mathcal S[p\dots |\mathcal{S}|] (p \in [1,|\mathcal S|])$, the number of is not greater than the number of .
Now you are given a string of length . There are queries. In each query, you are given a pair . Find the length of the longest good subsequence in . If there is no good subsequence, output .
Note: A subsequence is a new sequence formed by removing some elements without changing the relative order of the remaining elements.
Input Format
The first line contains two integers , representing the length of the string and the number of queries.
The second line contains a string of length , representing .
The next lines each contain two integers , representing one query.
Output Format
Output lines. Each line contains one integer, representing the answer to each query in order.
10 4
0100101011
1 1
1 5
2 9
1 10
-1
3
7
8
Hint
Sample Explanation
In the first query, the queried string is . No subsequence is good, so the answer is .
In the second query, the queried string is . The subsequence is good and is the longest, so the answer is .
In the third query, the queried string is . The subsequence is good and is the longest, so the answer is .
In the fourth query, the queried string is . The subsequence is good and is the longest, so the answer is .
Constraints
This problem uses bundled testdata.
| Subtask | Score | ||
|---|---|---|---|
For of the testdata, , and .
Translated by ChatGPT 5