YeJin's Footsteps

숫자 문자열과 영단어 본문

Computer Science & Engineering/알고리즘

숫자 문자열과 영단어

YeJinii 2021. 9. 10. 22:30

https://programmers.co.kr/learn/courses/30/lessons/81301

 

코딩테스트 연습 - 숫자 문자열과 영단어

네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자

programmers.co.kr

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

unordered_map<string, string> m;

void make_map(){
    m["zero"]="0";
    m["one"]="1";
    m["two"]="2";
    m["three"]="3";
    m["four"]="4";
    m["five"]="5";
    m["six"]="6";
    m["seven"]="7";
    m["eight"]="8";
    m["nine"]="9";
    m["ten"]="10";
}

int fun(string str){
    string ret="";
    string cur="";
    for(int i=0; i<str.length(); i++){
        if(!isdigit(str[i])){ //문자일 경우
            cur+=str[i];
            cout<<str[i]<<cur<<endl;
            if(m[cur]!=""){
                ret+=m[cur]; //문자열에 매칭되는 숫자 추가
                cur="";
            }
        }
        else{ //숫자일 경우
            ret += str[i];
        }
    }
    return stoi(ret);
}



int solution(string s) {
    
    int answer = 0;
    make_map();
    answer = fun(s);
    return answer;
}
Comments