題目鏈接:
題目:
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”.
題目大意:
給定只包含數字的字符串 ,有次詢問,每次詢問給定區間,輸出使得在這個區間內不含有子序列"2016"且含有子序列"2017"需要刪除的最少的字符數,如果不能滿足要求,輸出。
解題思路:
我們可以把拆成5種狀態,分別如下:
從前往後我們依次用來代表這五種狀態(如果字符串中存與對應狀態相同的子序列,我們就稱字符串處於狀態)。
那麼,對於字符串中的每個字符,我們都可以用一個矩陣來表示刪除這個字符帶來的影響。
比如,假設我們當前遍歷到了第個字符(設之前的個字符組成的字符串爲),那麼當時候,我們可以得到矩陣(代表使得字符串從狀態變成字符串()的狀態,所需要刪除的最少字符,若不可能則值爲)如下:
具體解釋一下,,因爲從的狀態0變化到新串的狀態0,就說明當前的字符必須刪掉,否則新串就會包含2,就變成了狀態1。同理,,因爲從的狀態0變化到新串的狀態0,就說明當前的字符必須保留,否則新串就不包含2,就變成了狀態0。
,因爲從的狀態0變化到新串的狀態2,只添加當前字符根本不可能達到。
同理,我們可以得到對應的矩陣,在這裏就省略不寫了。
要注意,因爲不能包含2016,所以如果存在201,那麼當前的6是必須要刪掉的。矩陣如下:
對於其他不等於中任何一個的字符,根本不會對產生影響,所以矩陣如下:
那麼,查詢之間的答案也就可以把這些矩陣合併起來得到了,具體見代碼。
代碼:
#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;
}