a444中等規則模擬・循環位移與頻率
APCS 2025/06:轉盤得分 (ZeroJudge q837)
累積每個輪盤的位移,再逐欄統計相同字母的最高次數。
這題在學什麼
輪盤旋轉會保留到下回合,因此為每個原始字串維護累積右移量 offset。新的距離 d 加入後,更新為 (offset+d) mod n;即使 d 是負數或超過一整圈,也可化成 0 到 n−1 的等價位移。
右移 offset 格後,畫面位置 j 的字元來自原字串位置 (j−offset) mod n。如此不用真的搬動字串。每回合要先更新所有輪盤位移,再對每個 j 建立 26 個字母的次數表,取該位置的最大次數作為得分。
每個位置的統計只包含本回合該欄的 m 個字元,然後把 n 欄得分加到總分。位移公式保持了每個輪盤的真實狀態,逐欄統計又恰好符合題目定義,因此累加 k 回合便是答案。
Step-by-step animation
示意輪盤 abc、abc、bac,兩回合位移 (1,0,−1)、(−1,1,1)
步驟 1 / 4
第一回合旋轉
累積右移量為 1、0、2,畫面分別是 cab、abc、acb。
cab
abc
acb
- offset
- [1,0,2]
- 總分
- 0
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
int main() {
int m, n, k;
cin >> m >> n >> k;
vector<string> wheels(m);
for (string &wheel : wheels) cin >> wheel;
vector<int> offset(m, 0);
int total = 0;
for (int round = 0; round < k; ++round) {
for (int i = 0; i < m; ++i) {
int distance;
cin >> distance;
offset[i] = ((offset[i] + distance) % n + n) % n;
}
for (int column = 0; column < n; ++column) {
array<int, 26> count{};
int best = 0;
for (int i = 0; i < m; ++i) {
int source = (column - offset[i] + n) % n;
int letter = wheels[i][source] - 'a';
best = max(best, ++count[letter]);
}
total += best;
}
}
cout << total << '\n';
}先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。