Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ๋ฐ์ดํฌ์คํธ๋ผ
- ๋ถํ ์ ๋ณต
- ์ต๋จ ๊ฒฝ๋ก
- ๋ฐฑํธ๋ํน
- ๋์ ํฉ
- dynamic debugging
- ์ฌ๊ท
- JPA
- Reversing
- dfs
- ์์ ์ ๋ ฌ
- OS
- Spring
- ๋ฌธ์์ด
- ์ฐ์ ์์ ํ
- ์ด๋ถ ํ์
- web
- ๊ตฌํ
- ์๋ฎฌ๋ ์ด์
- java
- ๋งต
- GCP
- thymeleaf
- DP
- c++
- ๊ทธ๋ฆฌ๋
- CVE
- error
- ์คํ
- BFS
Archives
- Today
- Total
hades
[Baekjoon] 1620๋ฒ: ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์ ๋ณธ๋ฌธ
๐ PS/Algorithm
[Baekjoon] 1620๋ฒ: ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์
hades1 2024. 7. 7. 16:10๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1620
๐ ์ค๊ณ
ํฌ์ผ๋ชฌ๋ค์ ์ด๋ฆ์ด ์ฃผ์ด์ง๊ณ , ์ ๋ ฅ์ด ํฌ์ผ๋ชฌ์ ์ด๋ฆ์ด๋ฉด ๋ฒํธ๋ฅผ, ๋ฒํธ์ด๋ฉด ํฌ์ผ๋ชฌ์ ์ด๋ฆ์ ์ถ๋ ฅํด์ผ ํ๋ค. C++์์ string๊ณผ int๋ฅผ ์ฐ๊ด์ง์ผ๋ฉด์๋ ์๋๊ฐ ๋น ๋ฅธ ์๋ฃ๊ตฌ์กฐ์๋ ํธ๋ฆฌ ๊ธฐ๋ฐ์ธ map์ด ์๋ค. map์ ํ์ฉํ์ฌ ํฌ์ผ๋ชฌ ๋๊ฐ์ ๋ง๋ ๋ค.
key:value๊ฐ ์ด๋ฆ:๋ฒํธ, ๋ฒํธ:์ด๋ฆ์ธ ๋๊ฐ์ ๋ ๊ฐ ๋ง๋ค์ด์ ํด๊ฒฐํ๋ค. map ํด๋์ค์์ find ํจ์๋ map ์์ ์ ๋ฌํ๋ key ๊ฐ์ด ์๋ค๋ฉด, map.end()๋ฅผ ๋ฐํํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;
int n, m;
string name, target;
map<string, int> dogam1;
map<int, string> dogam2;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i=1; i<=n; i++){
cin >> name;
dogam1.insert({name, i});
dogam2.insert({i, name});
}
for (int i=1; i<=m; i++){
cin >> target;
if (dogam1.find(target) == dogam1.end()) {
cout << dogam2.find(stoi(target))->second << "\n";
}
else{
cout << dogam1.find(target)->second << "\n";
}
}
return 0;
}
๋๊ฐ์ ๋ ๊ฐ๋ก ๋๋์ด์ ํด๊ฒฐํ์ง๋ง, ๋ฒํธ๋ string ์ทจ๊ธํ์ฌ ํ๋์ ๋๊ฐ์์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int n, m;
string s;
map<string, string> dogam;
int main(void){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i=1;i<=n;i++){
cin >> s;
dogam[s] = to_string(i);
dogam[to_string(i)] = s;
}
for (int i=0; i<m; i++){
cin >> s;
cout << dogam[s] << "\n";
}
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1764๋ฒ: ๋ฃ๋ณด์ก (0) | 2024.07.09 |
---|---|
[Baekjoon] 1697๋ฒ: ์จ๋ฐ๊ผญ์ง (0) | 2024.07.08 |
[Baekjoon] 1541๋ฒ: ์์ด๋ฒ๋ฆฐ ๊ดํธ (0) | 2024.07.05 |
[Baekjoon] 1260๋ฒ: DFS์ BFS (0) | 2024.07.03 |
[Baekjoon] 1074๋ฒ: Z (0) | 2024.07.02 |