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
*/
相关推荐
艾莉丝努力练剑1 小时前
【C++:C++11】C++11新特性深度解析:从可变参数模板到Lambda表达式
c++·stl·c++11·lambda·可变模版参数
同学小张3 小时前
【端侧AI 与 C++】1. llama.cpp源码编译与本地运行
开发语言·c++·aigc·llama·agi·ai-native
爱学习的小邓同学8 小时前
C++ --- 多态
开发语言·c++
招摇的一半月亮14 小时前
P2242 公路维修问题
数据结构·c++·算法
f***019315 小时前
CC++链接数据库(MySQL)超级详细指南
c语言·数据库·c++
合方圆~小文15 小时前
球型摄像机作为现代监控系统的核心设备
java·数据库·c++·人工智能
椰萝Yerosius16 小时前
[题解]2024CCPC郑州站——Z-order Curve
c++·算法
滨HI019 小时前
C++ opencv简化轮廓
开发语言·c++·opencv
学习路上_write19 小时前
FREERTOS_互斥量_创建和使用
c语言·开发语言·c++·stm32·单片机·嵌入式硬件
闻缺陷则喜何志丹20 小时前
【SOSDP模板 容斥原理 逆向思考】3757. 有效子序列的数量|分数未知
c++·算法·力扣·容斥原理·sosdp·逆向思考