cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2021 · EasySimulation

Every Third Character

Autumn Tournament (2021)

Submission name: third.cpp

Problem summary

Repeatedly delete the 3rd, 6th, 9th, … characters of the current string, then reverse what remains. Stop when two characters remain and print them alphabetically.

Source clarification. The original lower bound allows a one-character input, for which two surviving characters are impossible. For that under-specified case this implementation outputs the only character; judge expectations for N=1 are not established.

Input

One string of lowercase Latin letters.

Output

The two surviving characters in alphabetical order, with no space.

Constraints

The statement gives 1 ≤ N ≤ 1000000; the described two-survivor process requires N ≥ 2.

Worked example

Original statement sample.

Input

sequence

Output

eu

Solution analysis

  1. Build a new string containing positions whose one-based indices are not divisible by 3. Position i in zero-based indexing survives exactly when i%3!=2. Reverse this new string, then repeat while the length is greater than two.
  2. A round retains ceil(2N/3) characters, so the total amount of scanning forms a decreasing geometric sum. It is safe to use straightforward strings rather than a complicated linked representation. Sort the last two characters because their final left-to-right order is not the requested alphabetical order.
  3. For sequence, deleting q and n leaves seuece, then reversing gives eceues. Deleting its 3rd and 6th characters and reversing leaves euce. Another round gives eue, and the last round leaves ue. Sorting produces eu.

Why it works

Each round copies exactly the characters not designated for deletion, in their current order, and then reverses them as specified. Repeating until at most two remain therefore simulates the complete process; sorting yields the required output order.

Complexity: O(N) total time and O(N) space. After a round the length drops by about a third, so the cumulative scanning and reversing is a geometric sum.

Watch out. Recount positions from 1 at every round and reverse after deletion, not before it. The published lower bound N=1 contradicts the two-survivor output; the implementation safely prints that one character rather than inventing a second.

Commented C++17 solution

An independently written complete program with standard headers.

#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string text;
    cin>>text;
    while(text.size()>2){
        string next;
        next.reserve(text.size());
        for(size_t index=0;index<text.size();index++){
            // One-based positions 3,6,9,... have zero-based remainder 2.
            if(index%3!=2){
                next.push_back(text[index]);
            }
        }
        reverse(next.begin(),next.end());
        text.swap(next);
    }
    sort(text.begin(),text.end());
    cout<<text<<'\n';
}

Source: Arena Olimpiici ↗