Jishan's Log 4: Codeforces 71A solution
Codeforces 71A: Way Too Long Words
You might've heard of codeforces. If not, let me tell you that they are one of the most challenging online judges in the internet right now. Their problem sets are very unique and will pick your brain a lot. Today, I'll walk you through such a problem. Problem 71A: Way Too Long Words.
What the problem wants?
The problem basically tells us to take a word and if it is way too long we need to convert it to a shorter form (a guideline which they have given). The author states that any word bigger than 10 is a long word and must be shortened in the way that: the first letter will be its starting letter. The next will be the number of characters present between the starting and ending letter, and finally, the last letter will be its ending letter.
Example: initialization
i12n
There are 12 letter between i and n.
The Solution:
Algorithm:
1) Take the word input
2) check if it's less than or equal to 10.
3) If yes, simple print the word.
4) Else, take the word and append the first letter.
From there till just before the last letter, use a loop to increment a counter.
Then finally, append the counter and the last letter of the word and print it.
The Code:
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
string shortenedString(string str)
{
string s = "";
s += str[0];
int count = 0;
for(int i=1; i<=str.length()-2; i++)
{
count++;
}
s += to_string(count);
s += str[str.length()-1];
return s;
}
int main()
{
fastio;
int n; cin >> n;
while (n)
{
string s;
fflush(stdin); cin >> s; fflush(stdout);
if(s.length() <= 10)
{
cout << s << "\n";
}
else
{
s = shortenedString(s);
cout << s << "\n";
}
n--;
}
}
Comments
Post a Comment