求助:Kattis平台Ptice题目C++代码运行时错误原因排查
Hey there! Let's break down what's causing your runtime error and fix the other logic issues in your code, so you can get all test cases passing.
1. The Root Cause of Your Runtime Error
Your code crashes when the input length (numCharacters) exceeds 12 because you've defined fixed-length strings for Adrian, Bruno, and Goran's answer sequences. For example, your Adrian string is only 12 characters long—when i reaches 12, Adrian.at(i) tries to access a character that doesn't exist, triggering an out-of-bounds memory access (a common runtime error).
The fix here is to use their base repeating patterns instead of a fixed-length string, then use modulo (%) to cycle through the pattern no matter how long the input is:
- Adrian repeats "ABC" (length 3)
- Bruno repeats "BABC" (length 4)
- Goran repeats "CCAABB" (length 6)
2. Fixing the Code Step by Step
Here's how to adjust your code to fix the crash and output logic issues:
First, replace your fixed answer strings with their base patterns:
#include <iostream> #include <string> #include <algorithm> // Needed for std::max with initializer lists int main() { int numCharacters; std::string inputSequence; // Use base repeating patterns instead of fixed-length strings std::string adrian_pattern = "ABC"; std::string bruno_pattern = "BABC"; std::string goran_pattern = "CCAABB"; int adrian_score = 0, bruno_score = 0, goran_score = 0; std::cin >> numCharacters; std::cin >> inputSequence; for (int i = 0; i < numCharacters; i++) { // Use modulo to cycle through the pattern safely if (inputSequence[i] == adrian_pattern[i % adrian_pattern.size()]) { adrian_score++; } if (inputSequence[i] == bruno_pattern[i % bruno_pattern.size()]) { bruno_score++; } if (inputSequence[i] == goran_pattern[i % goran_pattern.size()]) { goran_score++; } }
Next, fix the output logic—your current code will print duplicate names or missing scores when there's a tie for the highest score. Instead, first calculate the maximum score, then print all players who achieved that score in alphabetical order (Adrian → Bruno → Goran, which matches the problem's requirements):
int max_score = std::max({adrian_score, bruno_score, goran_score}); std::cout << max_score << std::endl; if (adrian_score == max_score) { std::cout << "Adrian" << std::endl; } if (bruno_score == max_score) { std::cout << "Bruno" << std::endl; } if (goran_score == max_score) { std::cout << "Goran" << std::endl; } return 0; }
3. Why This Works
- The modulo operation ensures we never access an index outside the bounds of the base pattern strings, eliminating the runtime crash entirely.
- The revised output logic correctly handles ties by printing all top-scoring players in the required order, instead of duplicating output or omitting scores.
Quick Additional Tips
- Using
inputSequence[i]instead ofinputSequence.at(i)is slightly faster (though not critical here)—at()does extra bounds checking, which you don't need sinceiis guaranteed to stay withinnumCharacters. - Variable names like
adrian_scoreare a bit more concise and readable thanpointsForAdrian, but either style works as long as you're consistent.
内容的提问来源于stack exchange,提问作者Olle

