AtCoder题解——Beginner Contest 168——B - ... (Triple Dots)

解析AtCoder竞赛中BC168B题,介绍字符串处理算法,当字符串长度超过给定值K时,如何截取并添加省略号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目相关

题目链接

AtCoder Beginner Contest 168 B题,https://atcoder.jp/contests/abc168/tasks/abc168_b

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print SS without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Samples1

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

Explaination

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Samples2

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

Constraints

  • K is an integer between 1 and 100 (inclusive).
  • S is a string consisting of lowercase English letters.
  • The length of S is between 1 and 100 (inclusive).

题解报告

本题含义

给一个数字 K,一个字符串 S。如果字符串 S 的长度小于 K,输出本身。如果字符串 S 的长度大于 K,输出前 K 个字符,后面加上 3 个点。

样例数据分析

本题是一个水题,我写这个报告,也只是水一下。所以不需要样例数据分析了。

数据范围分析

K 的最大值是 100,字符串 S 的最大长度为 100。

算法设计

一道非常简单的入门级模拟题。

本题可以使用 STL 的 string,也可以使用字符串数组。如果使用字符串数组,要注意长度的定义。100 是不够的。为什么?假设 S 输入长度为 100 个字符,K 的大小为 99,那么最终实际的字符长度为 99+3=102 个。

AC 参考代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    int k;
    char s[106];
    cin>>k>>s;
    if (strlen(s)>k) {
        s[k]='.';
        s[k+1]='.';
        s[k+2]='.';
        s[k+3]='\0';
    }
    cout << s << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值