Codeforces Round 65 A. Way Too Long Words(71)

本题地址:https://codeforces.com/problemset/problem/71/A

A. Way Too Long Words

Codeforces Beta Round 65 (Div. 2)

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(严格超过10) more than 10 characters.
All too long words should be replace 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.
(the number =words_length-2) (中间是字符开始和结束的长度)
That number is in decimal system and doesn't contain any leading zeroes. (这个10进制的数字,不包括全是0的数字)

Thus,'localization' will be spelt as 'l10n','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
4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis

Output
word
l10n
i18n
p43s

题意很明确:一个字符长度超过10,就要用缩写表示,即首字母和尾巴字母,中间是其单词的中间部分长度。

这样理解完,就很容易写了,就是简单的字符串判断,使用to_string(int)函数

代码如下

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

void solve_longwords_to_abbreviation()
{
	string str1="";
	string abbreviation="";
	string middle_value = "";
	int key1 = 2;// i=0 and i=length-1 is used

	getline(cin, str1);
	int len = str1.length();
	if (len > 10)
	{
		middle_value = to_string(len - key1);
		abbreviation = str1[0] + middle_value + str1[len - 1];

		cout << abbreviation << endl;
	}
	else
	{
		cout << str1 << endl;
	}
}
int main()
{
	int t = 0;
	cin >> t;
	getchar();
	while (t--)
	{
		solve_longwords_to_abbreviation();
	}
	return 0;
}
/*
   A. Way Too Long Words
   problemset/problem/71/A
   Codeforces Beta Round 65 (Div. 2)

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 replace 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.
(the number =words_length-2)
That number is in decimal system and doesn't contain any leading zeroes.


Thus,'localization' will be spelt as 'l10n','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
 Input
 4
 word
 localization
 internationalization
 pneumonoultramicroscopicsilicovolcanoconiosis
 
 Output
 word
 l10n
 i18n
 p43s
*/
相关推荐
你怎么知道我是队长7 小时前
C语言---typedef
c语言·c++·算法
带土18 小时前
5. enum(枚举)关键字在C/C++中的作用
c语言·c++
驴友花雕8 小时前
【花雕学编程】Arduino BLDC 之群体机器人协同探索
c++·单片机·嵌入式硬件·arduino bldc·群体机器人协同探索
驴友花雕8 小时前
【花雕学编程】Arduino BLDC 之仿人机器人膝关节稳定系统
c++·单片机·嵌入式硬件·arduino bldc·仿人机器人膝关节稳定系统
Qhumaing9 小时前
C++学习:【PTA】数据结构 7-1 实验7-1(最小生成树-Prim算法)
c++·学习·算法
Z1Jxxx10 小时前
01序列01序列
开发语言·c++·算法
坚定学代码11 小时前
基于观察者模式的ISO C++信号槽实现
开发语言·c++·观察者模式·ai
ha204289419412 小时前
Linux操作系统学习记录之---TcpSocket
linux·网络·c++·学习
AI视觉网奇13 小时前
ue5 插件 WebSocket
c++·ue5
左直拳13 小时前
将c++程序部署到docker
开发语言·c++·docker