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 天前
内存数据库性能调优
数据库·redis·字符串·高并发·哈希·内存碎片
Tisfy5 天前
LeetCode 761.特殊的二进制字符串:分治(左右括号对移动)
算法·leetcode·字符串·递归·分治
hnjzsyjyj8 天前
洛谷 P8738:[蓝桥杯 2020 国 C] 天干地支 ← string
蓝桥杯·字符串·天干地支
闻缺陷则喜何志丹9 天前
【C++DFS 马拉车】3327. 判断 DFS 字符串是否是回文串|2454
c++·算法·深度优先·字符串·力扣·回文·马拉车
Tisfy10 天前
LeetCode 3714.最长的平衡子串 II:前缀和(一二三分类)
算法·leetcode·前缀和·字符串·题解
码农幻想梦11 天前
PKUKY150 浮点数加法(北京大学考研机试真题)
考研·字符串
远方23514 天前
哈希计算器1.0
字符串·md5·哈希·sha256
hnjzsyjyj17 天前
洛谷 P13270:【模板】最小表示法 ← 双指针 + 解环成链
字符串·双指针·解环成链
会叫的恐龙17 天前
C++ 核心知识点汇总(第六日)(字符串)
c++·算法·字符串