반응형
문제 출처 :
https://www.acmicpc.net/problem/10545
알고리즘 분석 :
문제 해결에 필요한 사항
1. map
map을 이용하여 문제를 해결해보자.
위의 그림처럼 우선 가장 base로 주어지는 핸드폰 자판을 mp에 담아둔다.
그리고 mp2를 만들고 저 핸드폰 자판에서 바뀐 자판에 맞게 mp2에 넣어주자.
이제 for문에서 한단어씩 받아오는데
그 단어가 몇번 자판에 존재하는지 확인해주고 prev를 통해서 이전 자판과 똑같은 자판을 쓰는지 파악해주면서 #을 추가해주자.
소스 코드 :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <cstdio> #include <map> #include <string> using namespace std; map<int, string> mp; int main() { char s = 'a'; for (int i = 2; i <= 9; i++) { if (i != 7 && i != 9) for (int j = 0; j < 3; j++) mp[i] += s++; else for (int j = 0; j < 4; j++) mp[i] += s++; } map<int, string> mp2; for (int i = 1; i <= 9; i++) { int val; scanf("%d", &val); mp2[i] = mp[val]; } string str; cin >> str; int len = str.size(); int prev = -1; for (int i = 0; i < len; i++) { char ch = str[i]; for (auto it = mp2.begin(); it != mp2.end(); it++) { int len2 = it->second.size(); for (int j = 0; j < len2; j++) { if (ch == it->second[j]) { if (it->first == prev) printf("#"); for (int k = 0; k < j + 1; k++) printf("%d", it->first); prev = it->first; } } } } return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[1744번] 수 묶기 (0) | 2018.03.08 |
---|---|
[13717번] 포켓몬 GO (0) | 2018.03.07 |
[15501번] 부당한 퍼즐 (2) | 2018.03.05 |
[11000번] 강의실 배정 (0) | 2018.02.24 |
[14950번] 정복자 (0) | 2018.02.23 |