top1编程
← 返回题目
题解

[NOIP 2018 普及组] 标题统计

1 条题解

  • 0
    @ 2026-10-20 19:16:06

    1. 解题思路

    **核心方法:**逐字符读入,只有字母或数字才把计数增加1。

    2. 坑点和注意事项

    • 空格与换行不计入答案;字符分类函数先转unsigned char。标题最多5个字符,int足够。
    /*
    思路:逐字符读入,只有字母或数字才把计数增加1。
    关键规则与坑点:空格与换行不计入答案;字符分类函数先转unsigned char。标题最多5个字符,int足够。
    时间复杂度:O(L)。空间复杂度:O(1)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        char c;
        int ans = 0;
        while (cin.get(c))
        {
            // 逐字符读入,只有字母或数字才把计数增加1。
            ans += isalnum((unsigned char)c) != 0;
        }
        cout << ans << '\n';
    }
    

    3. 复杂度

    • **时间复杂度:**O(L)。
    • **空间复杂度:**O(1)。
    • 1