알고리즘(코딩 테스트) 공부를 안한지 2달째.. 현업에서 막상 코드 유지보수 업무를 하다보니까 생각보다 코드를 건드리거나 구현할 일이 적은 것 같다. 그래서 알고리즘을 취미로 꾸준히 하려고 한다. 막상 오랜만에 다시 시작하니까 ㅋㅋㅋㅋㅋㅋㅋ 손이 안떨어진다... 코딩의 길 멀고도 험하다..
https://www.acmicpc.net/problem/1620
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
unordered_map<string, string> p1;
unordered_map<string, string> p2;
int n,m;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
string s;
for (int i = 1; i <= n; i++)
{
cin >> s;
p1.insert(make_pair(s, to_string(i)));
p2.insert(make_pair(to_string(i), s));
}
for (int i=0 ; i<m ;i++)
{
cin >> s;
cout << p1[s];
cout << p2[s];
cout << '\n';
}
}
아래는 바킹독님 풀이다.
// http://boj.kr/6c00940e0dab4c4e994d524d38e5582c
#include
using namespace std;
unordered_map<string, int=""> s2i;
string i2s[100005];
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> i2s[i];
s2i[i2s[i]] = i;
}
while(m--){
string query;
cin >> query;
if(isdigit(query[0])) // isdigit 문자열의 특정 원소가 1~9 숫자인지 판단하는 함수
cout << i2s[stoi(query)] << '\n'; // stoi string to int 숫자 문자를 숫자로 바꿔주는 함수
else
cout << s2i[query] << '\n';
}
}
</string,>
'CS(Computer Science)지식 > [C++][코딩 테스트] 자료구조 및 알고리즘' 카테고리의 다른 글
백준 9375번: 패션왕 신해빈 [C++] (0) | 2024.01.31 |
---|---|
백준 17219번: 비밀번호 찾기 [C++] (0) | 2024.01.31 |
백준 13414번: 수강신청 [C++] (0) | 2024.01.31 |
백준 7785번: 회사에 있는 사람 [C++] (0) | 2024.01.31 |
바킹독의 실전 알고리즘 강의 총정리(C++, 코딩 테스트, 알고리즘) (1) | 2024.01.29 |