#π ππͺπΌπ πππΉπ΅πͺπ·πͺπ½π²πΈπ· : Way Too Long Words Codeforces Solution C++ | Codeforces Beta Round 65
Way Too Long Words Codeforces Solution
Β
Question link: Way Too Long Words Codeforces.
For Beginners step by step procedure to solve this Way Too Long Words Codeforces.
- Understand the question
- Try to think of other test cases than present in the question.
- Try to write a pseudo code for it.
Question:
Sometimes some words like βlocalizationβ or βinternationalizationβ are so long that writing them many times in one text is quite tiresome.
Letβs consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesnβt contain any leading zeroes.
You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
Input:
The first line contains an integerΒ nΒ (1ββ€βnββ€β100). Each of the followingΒ nΒ lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of fromΒ 1Β toΒ 100Β characters.
Output:
PrintΒ nΒ lines. TheΒ i-th line should contain the result of replacing of theΒ i-th word from the input data.
Solution:
Hint 1: How to find the length of the string in c ++ ?
Hint 2: We have to only consider all those string whose length is greater than 10 and write all the words less then 10 as it is.
Intuition for Way Too Long Words Codeforces Solution: So, We can use str.length() method in C++ to find the length of the the string and if it is greater than 10 then we can simply writeΒ s[0] + (num-2) +s[s.size()-1] , where num is length of the string. We are writing num-2 because we have printed the first char and last char at the end so , two characters have been printed and length-2 have to be shorten.
Important: Since we are only considering words of length >10 ,for all words whose length < 10 will be written directly as it is and this can be used by if/else strategy in C++. |
Steps to Solve Way Too Long Words Codeforces Solution:
- Take input using cin in a string variable C++.
- Check if the length of the string is greater than 10 .
- If true, print , first_char + num-2 + last_char , where num = length of string;
- Β If false, print the whole character using cout.
Β
C++Β Way Too Long Words Codeforces Solution:
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; while(n>0) { string h; cin>>h; if(h.size()>10) { cout<<h[0]<<h.size() - 2<<h[h.size()-1]<<endl; } else { cout<<h<<endl; } n--; } return 0; }
Thank You!
Visit to See More Coding Solutions.
Codeforces Solution : Watermelon Codeforces Solution C++