Codeforces 71A: Way Too Long Words Solution & Explanation (C++)

Welcome to Episode 3 of our 50-Day Competitive Programming Challenge! After tackling basic math conditions in our last problem, we are stepping into a very crucial topic for coding interviews and placement drives: String Manipulation. Today, we are solving Way Too Long Words (Codeforces 71A). Have you ever noticed how developers sometimes write “localization” as l10n or “internationalization” as i18n? In this tutorial, we will learn the exact programmatic logic behind generating these abbreviations using C++.

The Problem Statement

Sometimes words are considered strictly too long (having strictly more than 10 characters). We need to replace these long words with a special abbreviation.

The abbreviation is created like this:

first letter + count of middle letters + last letter.

If a word has 10 or fewer characters, it is not considered too long and should be printed exactly as it is, without any changes.

Input:

  • The first line contains an integer $n$ (where $1 \le n \le 100$), representing the number of test cases (words to process).
  • The following $n$ lines each contain one word consisting of lowercase English letters.

Output: Print $n$ lines. The $i$-th line should contain the result of replacing the $i$-th word from the input data.

The Logic & Theory Breakdown

This problem relies on basic string indexing and length calculation. Let’s look at the word localization.

  1. Calculate the Length: localization has 12 characters. Because 12 is strictly greater than 10, we must abbreviate it.
  2. Find the First Letter: In C++ (and most languages), strings are zero-indexed. The first letter is always at index 0. So, word[0] gives us l.
  3. Find the Last Letter: The last letter is always at the index of total length - 1. So, word[11] gives us n.
  4. Calculate the Middle Letters: Since we are removing exactly 2 characters (the first and the last), the number of letters between them is always total length - 2. For localization, $12 – 2 = 10$.

Combine them together, and you get l10n.

The “Gotchas” (Edge Cases to watch out for)

  1. Exactly 10 characters: What about the word microscope? It has exactly 10 letters. The problem states the word must be strictly greater than 10. So microscope remains unchanged. Do not use >= 10 in your conditionals!
  2. The $n$ Test Cases: Beginners often forget that Codeforces requires you to read the integer $n$ first and then loop through the code $n$ times to process all the words. If you only process one word, you will get a “Wrong Answer” on test 1.

C++ Code Implementation

Here is the clean, optimized C++ solution for the Way Too Long Words problem.

#include <iostream>
#include <string>

using namespace std;

int main() {
    // Fast I/O for competitive programming
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n; // Read the number of words

    // Loop n times to process each word
    while (n--) {
        string word;
        cin >> word; 

        int len = word.length();

        // Check if the word is strictly greater than 10 characters
        if (len > 10) {
            // Print: first char + (length - 2) + last char
            cout << word[0] << len - 2 << word[len - 1] << "\n";
        } else {
            // Word is 10 characters or less, print unchanged
            cout << word << "\n";
        }
    }

    return 0;
}

Complexity Analysis

  • Time Complexity: $O(n \times L)$ where $n$ is the number of test cases and $L$ is the length of the string. Since calculating string length and accessing indices by position takes $O(1)$ time, the time taken per word is constant.
  • Space Complexity: $O(1)$ auxiliary space. We are simply modifying the output stream based on a single stored string variable, without using any additional data structures.

Key Takeaways

  • String Indices: Always remember that strings start at index 0, making the final character reside at index length - 1.
  • The While Loop Trick: Using while (n--) is a very common and fast competitive programming trick to run a loop exactly $n$ times without needing to declare a separate iterator variable like i.

Conclusion

Great job! You have officially learned how to manipulate strings, access specific characters via indexing, and handle multiple test cases in a single run. If you want to see exactly how this looks on the whiteboard, watch the full video tutorial below!

https://www.youtube.com/watch?v=cnN_CciG6ws&list=PLIY8eNdw5tW__CZfjV0Her7RXmwqZS5Pf&index=3

Grab the Downloadable Study Notes! Want to take these notes offline for interview prep? Hit the “Join” button on our YouTube channel to become a Snippet Supporter. Members get exclusive access to our downloadable CP Study PDFs—including clean code logic—to help you revise in minutes right before your placement drives!

Leave a Comment