- 扫描 3×n 图:左向右,窗口长 3 列;匹配骰子模板则记录点数、 i+=3 ,否则 i+=1。
- 骰子 x 翻转得7−x,只算翻转后变大的增量 Δ=(7−x)−x。
- 所有正增量降序排序,选前至多 k 个加到原始总和,输出答案。
C++#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
string s[3];
cin >> s[0] >> s[1] >> s[2];
vector<string> pat = {
"---", "-o-", "---",
"o--", "---", "--o",
"o--", "-o-", "--o",
"o-o", "---", "o-o",
"o-o", "-o-", "o-o",
"o-o", "o-o", "o-o"
};
vector<int> dice;
int i = 0;
while (i <= n - 3) {
string r0 = s[0].substr(i, 3);
string r1 = s[1].substr(i, 3);
string r2 = s[2].substr(i, 3);
int id = -1;
for (int t = 0; t < 6; t++) {
if (r0 == pat[t*3 + 0] && r1 == pat[t*3 + 1] && r2 == pat[t*3 + 2]) {
id = t;
break;
}
}
if (id != -1) {
dice.push_back(id + 1);
i += 3;
} else {
i += 1;
}
}
vector<int> gain;
long long sum = 0;
for (int x : dice) {
sum += x;
int other = 7 - x;
if (other > x) {
gain.push_back(other - x);
}
}
sort(gain.rbegin(), gain.rend());
int take = min(k, (int)gain.size());
for (int j = 0; j < take; j++) {
sum += gain[j];
}
cout << sum << '\n';
return 0;
}