3615. 单词个数统计

3615. 单词个数统计

⭐️难度:简单

⭐️类型:字符串

📖题目:题目链接

📚题解:

cpp 复制代码
 #include <stdio.h>
#include <iostream>
#include <map>
using namespace std;
int main() {
    // This is An Pencil Case
    char arr[2000] = { 0 };
    fgets(arr, 2000, stdin);
    int i = 0;
    int alphaCount = 0;
    bool isSpace = true;
    int wordCount = 0;
    map<char, int> alphaMap;
    while (true) {
        if (arr[i] == '\0' || arr[i] == '\n') {
            break;
        }
        else if (arr[i] == ' ') {
            isSpace = true;
        }
        else {
            ++alphaCount;
            if (isSpace) {
                ++wordCount;
            }
            if (arr[i] >= 'A' && arr[i] <= 'Z') {
                // ASCII
                arr[i] += 32;
            }
            ++alphaMap[arr[i]];
            isSpace = false;
        }
        ++i;
    }

    printf("%d\n", alphaCount);
    printf("%d\n", wordCount);
    map<char, int>::iterator it;
    int maxTimes = 0;
    for (it = alphaMap.begin(); it != alphaMap.end(); ++it) {
        if (it->second > maxTimes) {
            maxTimes = it->second;
        }
    }

    for (it = alphaMap.begin(); it != alphaMap.end(); ++it) {
        if (it->second == maxTimes) {
            printf("%c ", it->first);
        }
    }
    printf("\n");
    printf("%d\n", maxTimes);

    // cin.getline()
    return 0;
}
相关推荐
汉克老师3 天前
GESP2023年6月认证C++三级( 第三部分编程题(2、密码合规检测))
c++·字符串·gesp三级·gesp3级
Tisfy5 天前
LeetCode 2833.距离原点最远的点:计数
算法·leetcode·字符串·题解·模拟·计数
罗湖老棍子7 天前
Beads(信息学奥赛一本通- P1461) [POI 2010] KOR-Beads(洛谷-P3498)
算法·字符串·哈希
itman30114 天前
C语言printf输出格式:%d %f %s等用法详解
c语言·字符串·printf·格式化输出·整数
Tisfy14 天前
LeetCode 2515.到目标字符串的最短距离:从中间往两边遍历
算法·leetcode·字符串·题解·数组·遍历
庞轩px14 天前
第二篇:String、StringBuilder、StringBuffer深度剖析
java·字符串·stringbuilder·string·stringbuffer·字符串常量池
罗湖老棍子15 天前
Power Strings(信息学奥赛一本通- P1457)
算法·字符串·哈希
wenhaoran1116 天前
CF1800F Dasha and Nightmares
c++·算法·字符串·codeforces·位运算
网域小星球17 天前
C 语言从 0 入门(七)|字符数组与字符串完整精讲|VS2022 高质量实战
c语言·开发语言·字符串·vs2022·字符数组
九英里路18 天前
cpp容器——string模拟实现
java·前端·数据结构·c++·算法·容器·字符串