Online Judge PTA 单词统计(I)

单词统计(I)

描述

给定一段文章,每行不超过1000个字符,统计文章中每个单词出现的频率,忽略大小写差异。为了简化问题,文章中只有英文字母和空白字符,没有标点符号和特殊符号。

输入

第一行为一个整数T,表示数据的组数。 对于每组数据以一个独占一行的“#”结束。

输出

对于每组测试数据: 第一行为一个整数n表示文章中出现的单词的总数; 接下来n行按字母序从小到大输出文章中出现的单词(全部以大写字母表示)和出现的次数。

输入样例 1 

2
Hello World
#
Abc abc cde aa bb cc aas test
#

输出样例 1

2
HELLO 1
WORLD 1
7
AA 1
AAS 1
ABC 2
BB 1
CC 1
CDE 1
TEST 1

提示

HINT 时间限制:200ms 内存限制:64MB

#include <iostream>
#include <map>
#include <cctype>
#include <string>
#include <vector>
#include <algorithm>

int main() {
    int T;
    std::cin >> T;
    std::cin.ignore();
    while (T--) {
        std::map<std::string, int> word_count;
        std::string line;
        while (std::getline(std::cin, line) && line != "#") {
            std::string word = "";
            for (char c : line) {
                if (isalpha(c)) {
                    word += toupper(c);
                } else if (!word.empty()) {
                    word_count[word]++;
                    word = "";
                }
            }
            if (!word.empty()) word_count[word]++;
        }
        std::vector<std::pair<std::string, int>> words(word_count.begin(), word_count.end());
        std::sort(words.begin(), words.end());
        std::cout << words.size() << std::endl;
        for (const auto& p : words) {
            std::cout << p.first << " " << p.second << std::endl;
        }
    }
    return 0;
}

如有侵权,联系删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Flocx

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

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

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

打赏作者

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

抵扣说明:

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

余额充值