※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 3281번 문제인 T9이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/3281
3281번: T9
The first line of input file contains a natural number M, 1 ≤ M ≤ 100, the number of words in a dictionary. Next M lines contain dictionary words, one word in each line. The words are sorted in ascending order. The words contain only capital letters of
www.acmicpc.net
주어지는 (2 이상의) 숫자로 나타나는 숫자열이 어떤 문자로 이루어진 단어에 대응되는지를 나타내는 map을 하나 만들어 문제를 해결하자. 대응되는 문자열이 없다면 해당 숫자열의 길이만큼 '*'를 찍어주자. 글쓴이는 숫자열을 vector<int> 자료형으로 표현하였다.
숫자열에 대응되는 문자열이 여러가지라면 가장 먼저 등장한 문자열에 대응시키라는 문제의 조건에 유의하자. 이 조건에 따라 같은 숫자열에 대응되는 늦게 입력된 문자열은 무시하고 넘기는 식으로 구현하는 것으로 문제를 해결할 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
int ctoi[128];
int M, N;
map<vector<int>, string> mp;
vector<int> vec;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ctoi['A'] = ctoi['B'] = ctoi['C'] = 2;
ctoi['D'] = ctoi['E'] = ctoi['F'] = 3;
ctoi['G'] = ctoi['H'] = ctoi['I'] = 4;
ctoi['J'] = ctoi['K'] = ctoi['L'] = 5;
ctoi['M'] = ctoi['N'] = ctoi['O'] = 6;
ctoi['P'] = ctoi['Q'] = ctoi['R'] = ctoi['S'] = 7;
ctoi['T'] = ctoi['U'] = ctoi['V'] = 8;
ctoi['W'] = ctoi['X'] = ctoi['Y'] = ctoi['Z'] = 9;
cin >> M;
while (M--) {
string s; cin >> s;
for (auto& l : s) vec.emplace_back(ctoi[l]);
if (!mp.count(vec)) mp.insert(make_pair(vec, s));
vec.clear();
}
cin >> N;
while (N--) {
int x; cin >> x;
if (x > 1) vec.emplace_back(x);
else {
if (mp.count(vec)) cout << mp[vec] << ' ';
else {
for (auto& d : vec) cout << '*';
cout << ' ';
}
vec.clear();
}
}
if (mp.count(vec)) cout << mp[vec] << ' ';
else {
for (auto& d : vec) cout << '*';
cout << ' ';
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 3280 // C++] CARDS (0) | 2023.01.16 |
---|---|
[BOJ 3276 // C++] ICONS (0) | 2023.01.16 |
[BOJ 3282 // C++] ROOMS (0) | 2023.01.15 |
[BOJ 27225 // C++] Класс (0) | 2023.01.15 |
[BOJ 27182 // C++] Rain Diary (0) | 2023.01.15 |