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;
}
相关推荐
Tisfy14 小时前
LeetCode 3838.带权单词映射:求和、取模、拼接(附python一行版)
python·算法·leetcode·字符串·题解·模拟·取模
阿方.91818 天前
C++ string 超全精讲 | 从零使用、底层原理、手搓简易string、高频考点、易错点、面试手撕
开发语言·c++·字符串·string·知识分享
罗湖老棍子24 天前
The xor-longest Path(信息学奥赛一本通- P1478)
算法·字符串·字典树··lca最近公共祖先
金创想25 天前
积木移动题目分析及解题思路——木块问题(1)
c++·算法·字符串·c·刷题·信息学奥赛·积木
进击的荆棘1 个月前
优选算法——字符串
开发语言·c++·算法·leetcode·字符串
王老师青少年编程1 个月前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:[NOIP 2015 提高组] 子串
c++·字符串·csp·高频考点·子串·信奥赛
王老师青少年编程1 个月前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:遍历问题
c++·字符串·csp·高频考点·信奥赛
你很易烊千玺1 个月前
日常练习-数组 字符串常用的场景
前端·javascript·字符串·数组
王老师青少年编程1 个月前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:字符排序
c++·字符串·csp·高频考点·信奥赛·字符串排序·字符排序
王老师青少年编程1 个月前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:[NOIP 2004 普及组] FBI 树
c++·字符串·csp·高频考点·信奥赛·字符串综合·fbi树