题目链接:
题目:
A string t is called nice if a string “2017” occurs in t as a subsequence but a string “2016” doesn’t occur in t as a subsequence. For example, strings “203434107” and “9220617” are nice, while strings “20016”, “1234” and “20167” aren’t nice.
The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it’s impossible to make a string nice by removing characters, its ugliness is - 1.
Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).
Input
The first line of the input contains two integers n and q (4 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the length of the string s and the number of queries respectively.
The second line contains a string s of length n. Every character is one of digits ‘0’–‘9’.
The i-th of next q lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ n), describing a substring in the i-th query.
Output
For each query print the ugliness of the given substring.
Examples
Input
8 3
20166766
1 8
1 7
2 8
Output
4
3
-1
Input
15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
Output
-1
2
1
-1
-1
Input
4 2
1234
2 4
1 2
Output
-1
-1
Note
In the first sample:
In the first query, ugliness(“20166766”) = 4 because all four sixes must be removed.
In the second query, ugliness(“2016676”) = 3 because all three sixes must be removed.
In the third query, ugliness(“0166766”) = - 1 because it’s impossible to remove some digits to get a nice string.
In the second sample:
In the second query, ugliness(“01201666209167”) = 2. It’s optimal to remove the first digit ‘2’ and the last digit ‘6’, what gives a string “010166620917”, which is nice.
In the third query, ugliness(“016662091670”) = 1. It’s optimal to remove the last digit ‘6’, what gives a nice string “01666209170”.
题目大意:
给定只包含数字的字符串 S S S ( 4 ≤ ∣ S ∣ ≤ 200000 ) (4 \leq |S| \leq 200000) (4≤∣S∣≤200000),有 q ( 1 ≤ q ≤ 200000 ) q(1 \leq q \leq 200000) q(1≤q≤200000)次询问,每次询问给定区间 [ l , r ] [l,r] [l,r],输出使得在这个区间内不含有子序列"2016"且含有子序列"2017"需要删除的最少的字符数,如果不能满足要求,输出 − 1 -1 −1。
解题思路:
我们可以把
2017
2017
2017拆成5种状态,分别如下:
∅
,
2
,
20
,
201
,
2017
\emptyset , 2,20,201,2017
∅,2,20,201,2017
从前往后我们依次用
0
,
1
,
2
,
3
,
4
0,1,2,3,4
0,1,2,3,4来代表这五种状态(如果字符串中存与对应状态相同的子序列,我们就称字符串处于状态
i
(
i
=
0
,
1
,
2
,
3
,
4
)
i(i=0,1,2,3,4)
i(i=0,1,2,3,4))。
那么,对于字符串中的每个字符,我们都可以用一个矩阵来表示删除这个字符带来的影响。
比如,假设我们当前遍历到了第
i
i
i个字符
S
i
S_i
Si(设之前的
i
−
1
i-1
i−1个字符组成的字符串为
S
′
S'
S′),那么当
S
i
=
2
S_i = 2
Si=2时候,我们可以得到矩阵
M
M
M(
M
i
j
M_{ij}
Mij代表使得字符串
S
′
S'
S′从状态
i
i
i变成字符串(
S
′
+
S
i
S' + S_i
S′+Si)的状态
j
j
j,所需要删除的最少字符,若不可能则值为
∞
\infty
∞)如下:
M
=
[
1
0
∞
∞
∞
∞
0
∞
∞
∞
∞
∞
0
∞
∞
∞
∞
∞
0
∞
∞
∞
∞
∞
0
]
M = \left[ \begin{matrix} 1 & 0 & \infty & \infty & \infty \\ \infty & 0 & \infty & \infty & \infty \\ \infty & \infty & 0 & \infty & \infty \\ \infty & \infty & \infty & 0 & \infty \\ \infty & \infty & \infty & \infty & 0 \\ \end{matrix} \right]
M=⎣⎢⎢⎢⎢⎡1∞∞∞∞00∞∞∞∞∞0∞∞∞∞∞0∞∞∞∞∞0⎦⎥⎥⎥⎥⎤
具体解释一下,
M
00
=
1
M_{00} = 1
M00=1,因为从
S
′
S'
S′的状态0变化到新串的状态0,就说明当前的字符
2
2
2必须删掉,否则新串就会包含2,就变成了状态1。同理,
M
01
=
1
M_{01} = 1
M01=1,因为从
S
′
S'
S′的状态0变化到新串的状态0,就说明当前的字符
2
2
2必须保留,否则新串就不包含2,就变成了状态0。
M
02
=
∞
M_{02} = \infty
M02=∞,因为从
S
′
S'
S′的状态0变化到新串的状态2,只添加当前字符
2
2
2根本不可能达到。
同理,我们可以得到
0
、
1
、
7
0、1、7
0、1、7对应的矩阵,在这里就省略不写了。
要注意,因为不能包含2016,所以如果存在201,那么当前的6是必须要删掉的。矩阵如下:
M
=
[
0
∞
∞
∞
∞
∞
0
∞
∞
∞
∞
∞
0
∞
∞
∞
∞
∞
1
∞
∞
∞
∞
∞
1
]
M = \left[ \begin{matrix} 0 & \infty & \infty & \infty & \infty \\ \infty & 0 & \infty & \infty & \infty \\ \infty & \infty & 0 & \infty & \infty \\ \infty & \infty & \infty & 1 & \infty \\ \infty & \infty & \infty & \infty & 1 \\ \end{matrix} \right]
M=⎣⎢⎢⎢⎢⎡0∞∞∞∞∞0∞∞∞∞∞0∞∞∞∞∞1∞∞∞∞∞1⎦⎥⎥⎥⎥⎤
对于其他不等于
2
、
0
、
1
、
7
、
6
2、0、1、7、6
2、0、1、7、6中任何一个的字符,根本不会对产生影响,所以矩阵如下:
M
=
[
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
]
M = \left[ \begin{matrix} \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \infty & \infty & \infty & \infty & \infty \\ \end{matrix} \right]
M=⎣⎢⎢⎢⎢⎡∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞⎦⎥⎥⎥⎥⎤
那么,查询
[
l
,
r
]
[l,r]
[l,r]之间的答案也就可以把这些矩阵合并起来得到了,具体见代码。
代码:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 200000 + 100, MATSIZE = 5;
char s[MAXN];
struct Matrix {
int a[MATSIZE][MATSIZE]{};
Matrix() {
memset(a, 0x3f, sizeof(a));
}
Matrix operator*(const Matrix &tMat) const {
Matrix ans;
for (int k = 0; k < MATSIZE; k++)
for (int i = 0; i < MATSIZE; i++)
for (int j = 0; j < MATSIZE; j++)
ans.a[i][j] = min(a[i][k] + tMat.a[k][j], ans.a[i][j]);
return ans;
}
};
struct SegTree {
int l{}, r{};
Matrix mat;
} T[MAXN << 2];
void build(int l, int r, int cur) {
T[cur].l = l, T[cur].r = r;
if (l == r) {
for (int i = 0; i < MATSIZE; i++) T[cur].mat.a[i][i] = 0;
if (s[l] == '2') T[cur].mat.a[0][0] = 1, T[cur].mat.a[0][1] = 0;
if (s[l] == '0') T[cur].mat.a[1][1] = 1, T[cur].mat.a[1][2] = 0;
if (s[l] == '1') T[cur].mat.a[2][2] = 1, T[cur].mat.a[2][3] = 0;
if (s[l] == '7') T[cur].mat.a[3][3] = 1, T[cur].mat.a[3][4] = 0;
if (s[l] == '6') T[cur].mat.a[3][3] = 1, T[cur].mat.a[4][4] = 1;
return;
}
int mid = (l + r) >> 1;
build(l, mid, cur << 1);
build(mid + 1, r, cur << 1 | 1);
T[cur].mat = T[cur << 1].mat * T[cur << 1 | 1].mat;
}
Matrix query(int l, int r, int cur) {
if (l <= T[cur].l && T[cur].r <= r) return T[cur].mat;
if (l >= T[cur << 1 | 1].l) return query(l, r, cur << 1 | 1);
if (r <= T[cur << 1].r) return query(l, r, cur << 1);
return query(l, r, cur << 1) * query(l, r, cur << 1 | 1);
}
int main() {
int n, m, l, r;
Matrix tempMat;
scanf("%d %d %s", &n, &m, s + 1);
build(1, n, 1);
for (int i = 0; i < m; i++) {
scanf("%d %d", &l, &r);
tempMat = query(l, r, 1);
printf("%d\n", tempMat.a[0][4] <= n ? tempMat.a[0][4] : -1);
}
return 0;
}