cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2011 · EasyStrings & text

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

  1. Convert N to its corresponding lowercase letter: a + N − 1. Read complete title lines so spaces inside a title do not create extra attempts.
  2. Normalize only the first character to lowercase. Record the earliest matching attempt and leave that answer unchanged even if later titles also match.
  3. 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.

Complexity: O(T) time to read T total title characters and O(L) space for one title of length L ≤ 80.

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';
    }
}

Source: Arena Olimpiici ↗