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;
}
相关推荐
xiaoye-duck6 小时前
《算法题讲解指南:优选算法-字符串》--61.最长公共前缀,62.最长回文子串,63.二进制求和,64.字符串相乘
c++·算法·字符串
itman3012 天前
C语言字符串必知:末尾有个隐藏的\0,新手易踩坑
c语言·字符串·内存管理·库函数·指针操作
汉克老师3 天前
GESP2024年12月认证C++三级( 第一部分选择题(9-15))
c++·字符串·位运算·进制·补码·gesp三级·gesp3级
汉克老师3 天前
GESP2024年12月认证C++三级( 第一部分选择题(1-8))
c++·字符串·二进制·八进制·补码·gesp三级·gesp3级
王老师青少年编程4 天前
csp信奥赛c++之字符数组与字符串的区别
c++·字符串·字符数组·csp·信奥赛
Tisfy6 天前
LeetCode 3474.字典序最小的生成字符串:暴力填充
算法·leetcode·字符串·题解
汉克老师7 天前
GESP2025年6月认证C++三级( 第一部分选择题(9-15))
c++·字符串·位运算·gesp三级·gesp3级
Tisfy9 天前
LeetCode 2839.判断通过操作能否让字符串相等 I:if-else(两两判断)
算法·leetcode·字符串·题解
Thomas.Sir11 天前
第三章:Python3 之 字符串
开发语言·python·字符串·string
Hknll15 天前
CSP第33次认证题解
数据结构·c++·算法·stl·字符串·csp认证·vector/array