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;
}
相关推荐
季明洵1 天前
反转字符串、反转字符串II、反转字符串中的单词
java·数据结构·算法·leetcode·字符串
2401_841495641 天前
【Python高级编程】近似串匹配
python·算法·动态规划·字符串·数组·时间复杂度·空间复杂度
源代码•宸4 天前
Redis 攻略(Redis Object)
数据库·redis·后端·缓存·字符串·哈希表·type
拼好饭和她皆失5 天前
字符串题型练习
算法·字符串·哈希算法
gjxDaniel14 天前
A+B问题天堂版
c++·算法·字符串·字符数组
张彦峰ZYF18 天前
QLExpress 字符串能力解析:机制、用法与工程实践
字符串·express·qlexpress规则表达力
SunkingYang20 天前
QT中QStringList如何查找指定字符串,有哪些方式?
qt·字符串·查找·子串·qstringlist
闻缺陷则喜何志丹24 天前
【回文 字符串】3677 统计二进制回文数字的数目|2223
c++·算法·字符串·力扣·回文
Tisfy25 天前
LeetCode 0712.两个字符串的最小ASCII删除和:反向思维保留最大(动态规划)
算法·leetcode·动态规划·字符串·dp·子序列