본문 바로가기
PS 문제 풀이/Jungol

[Jungol] 정올 1516 단어세기

by 까망 하르방 2021. 3. 18.
반응형

출처: http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=788&sca=20

Approach

해당 문제에서는 문장의 앞, 뒤에 필요 없는 공백 문자를 포함하지 않는 데이터가 Input으로 주어지며

단어와 단어 사이에도 하나의 공백만이 있다.

하지만 '임의의 문장'은 맨 앞, 맨 뒤에 공백이 한 개 이상 포함될 수 있습니다.

▶ "    leading space     multiple space         tailing space        "


#include <iostream>
#include <string>
#include <map>
#include <sstream>
using namespace std;
 
int main() {
    string s, w;
    while (getline(cin, s) && s!= "END") {
        map<string, int> dic;
        for (stringstream sts(s); sts >> w;) dic[w]++;
        for (auto &d : dic) cout << d.first << " : " << d.second << "\n";
    }
}

 

반응형

댓글