-
Notifications
You must be signed in to change notification settings - Fork 0
/
2004G.cpp
121 lines (104 loc) · 2.19 KB
/
2004G.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#ifdef DBG
#include "debug.h"
#else
#define dbg(...)
#define dbg_export(...)
#endif
int n, k;
string s;
int inf = 1e9;
struct Mat
{
std::array<std::array<int, 10>, 10> a;
Mat()
{
for (auto &x : a) {
x.fill(inf);
}
}
Mat(int x) : Mat()
{
// init
// 0 1 2 3
// 0 X X X
//
// suppose x = 3
// 0 1 2 3
// 0 X X X 0
// 1 1 1 X X
// 2 2 X 2 X
// 3 3 X X 3
a[0][x] = 0;
for (int i = 1; i <= 9; i++) {
a[i][0] = i;
a[i][i] = i;
}
}
static Mat eye()
{
Mat ret;
for (int i = 0; i < 10; i++) {
ret[i][i] = 0;
}
return ret;
}
std::array<int, 10> &operator[](int i) { return a[i]; }
Mat operator*(Mat &b) const
{
Mat ret;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++) {
auto v = inf;
for (int k = 0; k < 10; k++) {
v = min(v, a[i][k] + b[k][j]);
}
ret[i][j] = v;
}
return ret;
}
};
int main()
{
#ifndef DBG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cin >> n >> k >> s;
s = '_' + s;
vector<Mat> mat(10);
for (int i = 1; i < 10; i++)
mat[i] = Mat(i);
mat[0] = Mat::eye();
vector<Mat> right(n + 1), left(n + 1);
assert(k >= 2);
for (int i = 1; i <= n; i++) {
if (i % k == 1) {
left[i] = mat[s[i] - '0'];
} else {
left[i] = left[i - 1] * mat[s[i] - '0'];
}
}
for (int i = n; i >= 1; i--) {
if (i == n || i % k == 0)
right[i] = mat[s[i] - '0'];
else
right[i] = mat[s[i] - '0'] * right[i + 1];
}
vector<int> ans;
for (int i = 1; i + k - 1 <= n; i++) {
Mat m;
if (i % k == 1)
m = right[i];
else
m = right[i] * left[i + k - 1];
ans.push_back(m[0][0]);
}
for (auto x : ans) {
cout << x << ' ';
}
cout << endl;
return 0;
}