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;
}
相关推荐
金创想2 小时前
积木移动题目分析及解题思路——木块问题(1)
c++·算法·字符串·c·刷题·信息学奥赛·积木
进击的荆棘1 天前
优选算法——字符串
开发语言·c++·算法·leetcode·字符串
王老师青少年编程3 天前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:[NOIP 2015 提高组] 子串
c++·字符串·csp·高频考点·子串·信奥赛
王老师青少年编程5 天前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:遍历问题
c++·字符串·csp·高频考点·信奥赛
你很易烊千玺6 天前
日常练习-数组 字符串常用的场景
前端·javascript·字符串·数组
王老师青少年编程6 天前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:字符排序
c++·字符串·csp·高频考点·信奥赛·字符串排序·字符排序
王老师青少年编程6 天前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:[NOIP 2004 普及组] FBI 树
c++·字符串·csp·高频考点·信奥赛·字符串综合·fbi树
王老师青少年编程7 天前
csp信奥赛C++高频考点专项训练之字符串 --【回文字符串】:回文拼接
c++·字符串·csp·高频考点·信奥赛·字符串回文·回文拼接
王老师青少年编程7 天前
csp信奥赛C++高频考点专项训练之字符串 --【回文字符串】:判断字符串是否为回文
c++·字符串·csp·高频考点·信奥赛·回文字符串·判断字符串是否为回文
王老师青少年编程8 天前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:合并序列
c++·字符串·csp·高频考点·信奥赛·字符串排序·合并序列