HJ10.字符个数统计

字符个数统计

https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50?tpId=37&tags=&title=&difficulty=&judgeStatus=&rp=1&gioEnter=menu

HJ10.字符个数统计

#include <string>
#include <iostream>

const int LEN = 128;

int cnt_ch(std::string s) {
    int ch[LEN];
    for (int i = 0; i < LEN; ++i) {
        ch[i] = 0;
    }
    for (auto& i : s) {
        ch[(int)i] = 1;
    }
    int cnt = 0;
    for (int i = 0; i < LEN; ++i) {
        if (ch[i] == 1) {
            ++cnt;
        }
    }
    return cnt;
}

int main() {
    std::string s;
    std::cin >> s;
    std::cout << cnt_ch(s);
    return 0;
}

解题思路:

难点1:题目没有对空间做限制,就看能否想到利用字典来存每个字符是否出现的信息了。

全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务