Algorithm/PS

[BOJ/백준] 6841 - I Speak TXTMSG [C++/python]

chanwoong1 2023. 3. 2. 14:58
728x90

문제 링크

 

6841번: I Speak TXTMSG

The program will output text immediately after each line of input. If the input is one of the phrases in the translation table, the output will be the translation; if the input does not appear in the table, the output will be the original word. The transla

www.acmicpc.net

문제 풀이

미리 설정해놓은 단축 메세지가 있다면, 변환된 메세지로 출력해주는 문제이다.
파이썬은 두개의 리스트를 설정해서 문제를 해결했고, C++은 map을 통해 미리 단축 메세지와 변환 메세지를 짝지어놓았다.

정답 코드

C++

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <unordered_map>

using namespace std;

#define fast ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define ll long long

unordered_map<string, string> short_map;

void madeTable()
{
    short_map["CU"] = "see you";
    short_map[":-)"] = "I’m happy";
    short_map[":-("] = "I’m unhappy";
    short_map[";-)"] = "wink";
    short_map[":-P"] = "stick out my tongue";
    short_map["(~.~)"] = "sleepy";
    short_map["TA"] = "totally awesome";
    short_map["CCC"] = "Canadian Computing Competition";
    short_map["CUZ"] = "because";
    short_map["TY"] = "thank-you";
    short_map["YW"] = "you’re welcome";
    short_map["TTYL"] = "talk to you later";
}

int main()
{
    fast;
    madeTable();

    string s;
    while (1) {
        cin >> s;
        if (short_map.find(s) == short_map.end()) cout << s << "\n";
        else cout << short_map[s] << "\n";
        if (s == "TTYL") break;
    }
    return 0;
}

python

short = ["CU", ":-)", ":-(", ";-)", ":-P", "(~.~)", "TA", "CCC", "CUZ", "TY", "YW", "TTYL"]
trans = ["see you", "I’m happy", "I’m unhappy", "wink", "stick out my tongue", "sleepy", "totally awesome", "Canadian Computing Competition", "because", "thank-you", "you’re welcome", "talk to you later"]
while True:
    text = input()
    if text in short : print(trans[short.index(text)])
    else : print(text)
    if text == "TTYL" : break
728x90