DMOJ · ccc24s2

CCC 2024 S2 Heavy-Light Composition

This C++ solution uses string processing for CCC 2024 S2 Heavy-Light Composition. Read the reasoning, inspect the code, or try your own test case below.

ccc24s2CCC 2024 S2Implementation & simulationString processingC++34 lines
Solution246of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

String processing

The implementation follows the official CCC 2024 S2 definition: count each letter, classify frequencies greater than one as heavy, and verify adjacent classifications alternate.

Implementation & simulation

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

CCC_24_S2_Heavy_Light_Composition.cpp

C++

    #include <array>
    #include <iostream>
    #include <string>
     
    using namespace std;
     
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
     
        int testCases, length;
        cin >> testCases >> length;
     
        while (testCases--) {
            string composition;
            cin >> composition;
     
            array<int, 26> frequency{};
            for (char letter : composition) {
                ++frequency[letter - 'a'];
            }
     
            bool alternates = true;
            for (int index = 1; index < length; ++index) {
                bool previousIsHeavy = frequency[composition[index - 1] - 'a'] > 1;
                bool currentIsHeavy = frequency[composition[index] - 'a'] > 1;
                if (previousIsHeavy == currentIsHeavy) {
                    alternates = false;
                    break;
                }
            }
     
            cout << (alternates ? 'T' : 'F') << '\n';
        }
    }
     
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.