백준/브론즈

[백준] 브론즈4-알파벳 개수 C++ 10808번

taene_ 2025. 2. 21. 23:08

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

 

1. 배열 사용

#include <iostream>
using namespace std;

string S;
int cnt[26];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> S;
	for (int i = 0; i < S.size(); i++)
	{
		cnt[S[i] - 'a'] += 1;
	}

	for (int i = 0; i < 26; i++)
		cout << cnt[i] << ' ';

	return 0;
}

 

2. map 사용

#include <iostream>
#include <map>
using namespace std;

string S;
map<char, int> cnt;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> S;
	for (int i = 0; i < S.size(); i++)
	{
		cnt[S[i]] += 1;
	}

	for (int i = 0; i < 26; i++)
		cout << cnt[i + 'a'] << ' ';

	return 0;
}