Way Too Long Words - Codeforces problem solution
Way Too Long Words - Codeforces problem solution
A. Way Too Long Words
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.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
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.
Examples
inputCopy
4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis
outputCopy
word
l10n
i18n
p43s
Solution with C++
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while (t--)
{
string s;
cin>>s;
if(s.length()>10){
cout<<s[0]<<s.length()-2<<s[s.length()-1]<<endl;
}else{
cout<<s<<endl;
}
}
return 0;
}
Solution with C
#include <stdio.h>
#include <string.h>
int main()
{
int n, len, p, i, j;
char s[1001];
scanf("%d", &n);
for(i=1; i<=n; i++){
scanf("%s", s);
len = strlen(s);
if(len>10){
printf("%c%d%c\n", s[0],len-2, s[len-1]);
}else{
printf("%s\n", s);
}
}
return 0;
}
Output
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
Comments
Post a Comment