최대 1 분 소요

문제 링크

https://www.acmicpc.net/problem/10808

난이도: 브론즈 4

문제

알파벳 소문자로만 이루어진 단어 S가 주어진다. 각 알파벳이 단어에 몇 개가 포함되어 있는지 구하는 프로그램을 작성하시오.

입력

첫째 줄에 단어 S가 주어진다. 단어의 길이는 100을 넘지 않으며, 알파벳 소문자로만 이루어져 있다.

출력

단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.

C++를 사용한 풀이

#include <iostream>
#include <string>

using namespace std;

class Stack{
    private:
        int* arr;
        int size;
    public:
        Stack(){
            arr=new int[100001];
            size=0;
        }
        ~Stack(){
            delete [] arr;
        }
        void push(int x);
        int pop();
        int size_value();
        int empty();
        int top();

};
void Stack::push(int x){
    arr[size]=x;
    size++;
}
int Stack::pop(){
    if(size==0) return -1;
    int value=arr[size-1];
    size--;
    return value;
}
int Stack::size_value(){
    return size;
}
int Stack::empty(){
    if(size==0) return 1;
    else return 0;
}
int Stack::top(){
    if(size==0) return -1;
    return arr[size-1];
}

int main(void){
    ios_base :: sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);

    Stack s[26];
    string str;

    cin>>str;
    int len=str.length();
    for(int i=0;i<len;++i){
        char c=str[i];
        s[c-'a'].push(1);
    }
    for(int i=0;i<26;++i){
        cout<<s[i].size_value()<<" ";
    }


}

https://www.acmicpc.net/source/31091673

댓글남기기