• 每日一九度之题目1021:统计字符


    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:6050

    解决:3491

    题目描述:
        统计一个给定字符串中指定的字符出现的次数。
    输入:
        测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
    输出:
        对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
        c0 n0
        c1 n1
        c2 n2
        ...
        其中ci是第1行中第i个字符,ni是ci出现的次数。
    样例输入:
    I
    THIS IS A TEST
    i ng
    this is a long test string
    #
    样例输出:
    I 2
    i 3
      5
    n 2
    g 2

    继续用桶的思想,这种题目很简单!O(∩_∩)O哈哈~

    //Asimple
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cctype>
    #include <cstdlib>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <queue>
    #define INF 100000
    using namespace std;
    const int maxn = 105;
    typedef long long ll ;
    int n, m, num;
    char str[maxn], s1[6];
    int a[128]; 
    
    int main(){
        while( gets(s1) != NULL ){
            if( strlen(s1) == 1 && s1[0] == '#' ) break;
            memset(a,0,sizeof(a));
            gets(str);
            for(int i=0; str[i]!=''; i++){
                a[str[i]] ++ ;
            }
            for(int j=0; s1[j]!=''; j++){
                printf("%c %d
    ",s1[j],a[s1[j]]);
            }
        } 
        return 0;
    }
    低调做人,高调做事。
  • 相关阅读:
    计蒜客 移除数组中的重复元素 (双指针扫描)
    计蒜客 寻找插入位置 (二分查找)
    poj 1007 DNA Sorting
    全排列函数 nyoj 366(next_permutation()函数)
    nyoj 202 红黑树
    nyoj 92 图像有用区域
    nyoj 82 迷宫寻宝(一)
    nyoj 58 最少步数
    nyoj 43 24 Point game
    nyoj 42 一笔画问题
  • 原文地址:https://www.cnblogs.com/Asimple/p/5839532.html
Copyright © 2020-2023  润新知