A Film for the Letter
National Olympiad — Regional round (2011 season)
Submission name: alphabet.cpp
Problem summary
Given the 1-based index N of an English alphabet letter, inspect up to M film titles. The player wins on the first title starting with that letter, ignoring case; otherwise the player loses. Titles may contain spaces and end with a period.
Input
N and M, followed by M lines of film titles.
Output
win and the 1-based successful attempt, or lose if none succeeds.
Constraints
1 < N < 26; 1 < M < 20; each title has at most 80 characters and contains Latin letters and possibly spaces.
Worked example
An independently prepared example for this explanation.
Input
7 4 dvama zavinagi. Vinetu. Geroi. Vakanciq.
Output
win 3
Solution analysis
- Convert N to its corresponding lowercase letter: a + N − 1. Read complete title lines so spaces inside a title do not create extra attempts.
- Normalize only the first character to lowercase. Record the earliest matching attempt and leave that answer unchanged even if later titles also match.
- With N = 7 the target is g. The first two sample titles start with d and v, while Geroi starts with G. Ignoring case gives a win on attempt 3.
Why it works
Each line corresponds to exactly one attempt. The character test succeeds exactly for a title with the target initial, and recording only the first success gives its smallest attempt number. If none succeeds, all M permitted attempts have failed, so lose is correct.
Watch out. Use getline after consuming the numeric line. Case must be ignored, but only the initial letter matters.
Commented C++17 solution
An independently written complete program with standard headers.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
int letterIndex, attempts;
cin >> letterIndex >> attempts;
string title;
getline(cin, title);
char target = static_cast<char>('a' + letterIndex - 1);
int firstWin = 0;
for (int attempt = 1; attempt <= attempts; attempt++) {
getline(cin, title);
char initial = static_cast<char>(tolower(static_cast<unsigned char>(title[0])));
// Preserve the earliest successful attempt.
if (initial == target && firstWin == 0) {
firstWin = attempt;
}
}
if (firstWin == 0) {
cout << "lose\n";
} else {
cout << "win " << firstWin << '\n';
}
}