Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

hwooo

프로그래머스 (C/C++) 120838 : 모스부호(1) 본문

Study/Algorithm

프로그래머스 (C/C++) 120838 : 모스부호(1)

hwooo 2023. 8. 17. 22:01

https://school.programmers.co.kr/learn/courses/30/lessons/120838

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

풀이

공백을 기준으로 문자열을 잘라 해당 문자열이 저장되어 있는 map에서 알파벳을 가져왔다.

문자열의 끝에 공백이 없어 마지막에 들어온 문자는 answer에 저장되지 않으므로 반복문 탈출 이후에 마지막 문자를 한 번 더 저장해준다.

 


코드

#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

string solution(string letter) {
    string answer="";
    unordered_map<string, char> M ={
        {".-",'a'},{"-...",'b'},{"-.-.",'c'},{"-..",'d'},{".",'e'},{"..-.",'f'},
    {"--.",'g'},{"....",'h'},{"..",'i'},{".---",'j'},{"-.-",'k'},{".-..",'l'},
    {"--",'m'},{"-.",'n'},{"---",'o'},{".--.",'p'},{"--.-",'q'},{".-.",'r'},
    {"...",'s'},{"-",'t'},{"..-",'u'},{"...-",'v'},{".--",'w'},{"-..-",'x'},
    {"-.--",'y'},{"--..",'z'}
    };
    
    string s="";
    for(char c:letter){
        if(c!=' ') s+=c;
        else{
            answer+=M[s];
            s="";
        }
    }
    answer+=M[s];
    return answer;
}