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 E2012 · EasyString filtering

Remove Hollow Letters

National Olympiad — Regional round (2012 season)

Submission name: lazy.cpp

Problem summary

Delete all spaces and all letters in the set a,b,d,e,g,o,p,q from a text, retaining the other letters in their original order.

Input

n, then one line of n lowercase Latin letters and spaces; no leading or trailing spaces.

Output

The remaining text, without spaces.

Constraints

1 ≤ n ≤ 100,000.

Worked example

An independently prepared example for this explanation.

Input

17
a sample sentence

Output

smlsntnc

Solution analysis

  1. The decision to keep a character depends only on that character. Store the eight forbidden letters in a small string.
  2. Read the whole text line with getline after consuming the newline following n. Print a character only when it is neither a space nor forbidden.
  3. In a sample sentence, removing a,p,e and all spaces leaves smlsntnc. The surviving letters keep their original order.

Why it works

Each character is printed exactly when it is allowed. The left-to-right scan preserves order, so its output is precisely the text after the required deletions.

Complexity: O(n) time and O(n) input storage; O(1) additional working space.

Watch out. Formatted word input alone loses the original spaces, though filtering tokens would also work. An empty result should still end with a newline.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main() {
    int n;
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    string text;
    getline(cin, text);
    const string forbidden = "abdegopq";
    // Print survivors immediately to preserve their original order.
    for (char letter : text) {
        if (letter != ' ' && forbidden.find(letter) == string::npos) {
            cout << letter;
        }
    }
    cout << '\n';
}

Source: Arena Olimpiici ↗