[EOJ] 实训题 P3024 随机排序 StringStream Enhancement

[EOJ] 实训题 P3024 随机排序

先把题目地址贴出来:P3024 随机排序

题干

提取英文文本中的单词,重复出现的单词只取一个,把它们按照字典顺序排序,建立为一个单词表。

例如:英文文本如下:

ask not what your country can do for you,ask what you can do for your country.

提取的非重复单词为:

ask not what your country can do for you

排序后建立的单词表为:

ask can country do for not what you your

注意:

(1) 单词与单词之间用空格或标点符号(逗号 (,),句号 (.), 惊叹号 (!), 问号 (?))分隔。

(2) 提取的单词只包含 26 个英文字符。
输入格式

第 1 行:一个整数 T (1≤T≤10) 为问题数。

接下来 T 行,每行输入一段文本,文本长度不超过 500 个字符。

文本由空格,逗号 (,),句号 (.), 惊叹号 (!),问号 (?) 以及 26 个小写英文字符组成。
输出格式

对于每个问题,输出一行问题的编号(0 开始编号,格式:case #0: 等)。

然后对应每个问题 , 在一行中输出建立的单词表,单词与单词之间用一个空格分隔。最后一个单词后面没有空格。

样例
##### Input

3
ask not what your country can do for you,ask what you can do for your country.
no enthusiasm forever,no unexpected happening of surprising and pleasing so,only silently ask myself in mind next happiness,when will come?
let us go! let us go!a things.

###### Output

case #0:
ask can country do for not what you your
case #1:
and ask come enthusiasm forever happening happiness in mind myself next no of only pleasing silently so surprising unexpected when will
case #2:
a go let things us

核心代码

思路:

Isalpha()替换符号 + StringSteam切割 + STL.set 自动去重排序

string s, t;
 set<string> ss;
 getline(cin, s);
 for(auto &i : s)
     if(!isalpha(i)) i = ' ';
 stringstream stream(s);
 while(stream >> t)
     ss.insert(t);
 auto x = ss.begin();
 cout << *x;
 while(++x != ss.end())
     cout << ' ' << *x;
 cout << endl;
 return ;

stringstream 是定义在头文件 #include 中的一种类。可以用来进行数据类型转换,以及字符串拼接等等。感兴趣的朋友可以参考这篇博文:stringstream常见用法介绍

完整AC代码如下:
#include <iostream>
#include <string>
#include <set>
#include <sstream> // Be remind of this head file.Plz
using namespace std;
void init()/* Define function init() to do global initialization if needed   */
{
    return ;
}
void solve()/* Define function solve() to process one case of the problem    */
{
    string s, t;
    set<string> ss;
    getline(cin, s);
    for(auto &i : s)
        if(!isalpha(i)) i = ' ';
    stringstream stream(s);
    while(stream >> t)
        ss.insert(t);
    auto x = ss.begin();
    cout << *x;
    while(++x != ss.end())
        cout << ' ' << *x;
    cout << endl;
    return ;
}
/******************************************************************************/
/*  DON'T MODIFY main() function anyway!                                      */
/******************************************************************************/
int main()
{
    int T, i;
    (cin >> T).get();
    for(i = 0; i < T; ++i){cout << "case #" << i << ":\n"; solve();}
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值