@chen_zhe 继续补充

提交记录:https://www.luogu.com.cn/record/198796346

所属题目 T563940 [202501G] 古希腊掌管节奏的神

提交时间 2025-01-17 19:16:42

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    string S;
    cin >> S;
    int type;
    cin >> type;

    // 存储每拍敲击的手指集合
    vector<string> beats;
    string currentBeat = "";

    // 解析字符串,提取每拍的手指集合
    int n = S.size();
    for (int i = 0; i < n; ++i) {
        if (S[i] == '(') {
            // 开始一个括号,收集多手指同时敲击
            currentBeat = "(";
        } else if (S[i] == ')') {
            // 结束一个括号,加入beats
            currentBeat += ')';
            beats.push_back(currentBeat);
            currentBeat = "";
        } else if (S[i] == 'L' || S[i] == 'R' || S[i] == '\'') {
            // 其他字符,手指敲击
            currentBeat += S[i];
        }
    }

    // 计算拍数
    int totalBeats = beats.size();
    cout << totalBeats << endl;

    if (type == 1) {
        // 计算每根手指的最大连续敲击次数
        vector<int> maxConsecutive(4, 0);  // 存储四个手指的最大连续敲击数

        // 当前拍是敲击的状态,跟踪每个手指是否敲击
        vector<bool> isPressed(4, false);

        // 索引映射
        int fingerIndex[4] = {0, 1, 2, 3};  // 对应 L, L', R, R'
        vector<string> fingers = {"L", "L'", "R", "R'"};

        // 记录连续敲击次数
        vector<int> consecutive(4, 0);
        for (int i = 0; i < totalBeats; ++i) {
            // 解析每一拍,更新当前敲击的状态
            for (int j = 0; j < 4; ++j) {
                if (beats[i].find(fingers[j]) != string::npos) {
                    isPressed[j] = true;
                } else {
                    isPressed[j] = false;
                }
            }
            
            // 更新每根手指的连续敲击次数
            for (int j = 0; j < 4; ++j) {
                if (isPressed[j]) {
                    consecutive[j]++;
                    maxConsecutive[j] = max(maxConsecutive[j], consecutive[j]);
                } else {
                    consecutive[j] = 0;
                }
            }
        }

        // 输出四个手指的最大连续敲击次数
        cout << maxConsecutive[0] << " " << maxConsecutive[1] << " "
             << maxConsecutive[2] << " " << maxConsecutive[3] << endl;
    }

    return 0;
}

与 T563939 [202501F] 知识竞赛 的提交差仅为 1 分钟,我想知道是什么手速能写出一份变量命名规范,有详细注释的代码。

2025/1/20 15:55
8457
chen_zheAya楼主2025/1/20 15:55