๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/PS

[๋ฐฑ์ค€] ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ(1620), C++

by candosh 2024. 11. 14.

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1620

โœ๐Ÿป ๋ฌธ์ œ ์„ค๋ช…

์•ˆ๋…•? ๋‚ด ์ด๋ฆ„์€ ์ด๋‹ค์†œ

๐Ÿ’๐Ÿป‍โ™€๏ธ ๋‚ด ํ’€์ด

์Šคํ† ๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๋‹ค..๐Ÿ˜ฑ ๋‹ค ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋งˆ์ง€๋ง‰ ์˜ค๋ฐ•์‚ฌ๋‹˜ ๋ง์”€๋งŒ ๋ณด๋ฉด ๋  ๊ฑฐ ๊ฐ™๋‹ค!

 

์˜ค๋ฐ•์‚ฌ : ๊ทธ๋Ÿผ ๋‹ค์†œ์•„ ์ด์ œ ์ง„์ •ํ•œ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด ๋„๊ฐ์„ ์™„์„ฑ์‹œํ‚ค๋„๋ก ํ•˜์—ฌ๋ผ. ์ผ๋‹จ ๋„ค๊ฐ€ ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํฌ์ผ“๋ชฌ ๋„๊ฐ์—์„œ ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์„ ๋ณด๋ฉด ํฌ์ผ“๋ชฌ์˜ ๋ฒˆํ˜ธ๋ฅผ ๋งํ•˜๊ฑฐ๋‚˜, ํฌ์ผ“๋ชฌ์˜ ๋ฒˆํ˜ธ๋ฅผ ๋ณด๋ฉด ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์„ ๋งํ•˜๋Š” ์—ฐ์Šต์„ ํ•˜๋„๋ก ํ•˜์—ฌ๋ผ. ๋‚˜์˜ ์‹œํ—˜์„ ํ†ต๊ณผํ•˜๋ฉด, ๋‚ด๊ฐ€ ์ƒˆ๋กœ ๋งŒ๋“  ๋„๊ฐ์„ ์ฃผ๋„๋ก ํ•˜๊ฒ ๋„ค.

 

ํฌ์ผ“๋ชฌ ๋งคํ•‘ ๋ฌธ์ œ๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค!

 

์˜ˆ์‹œ : 26๊ฐœ๊ฐ€ ๋“ค์–ด์˜ค๊ณ , ๋งž์ถฐ์•ผํ•  5๊ฐœ๊ฐ€ ๋“ค์–ด์˜ด

 

M๊ฐœ ๋ฌธ์ œ ์ค‘์—์„œ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๊ณ , ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ํฌ์ผ“๋ชฌ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ, ๋ฐฐ์—ด๋กœ ํ’€๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค.

 

๊ทธ๋ž˜์„œ map์„ ์‚ฌ์šฉํ•ด์„œ ํ‘ธ๋Š”๋ฐ, string -> int , int -> string ๋งต์„ 2๊ฐœ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•œ๋‹ค.  

๐Ÿค– ์ฝ”๋“œ

#include <bits/stdc++.h>
using namespace std;

int n, m;
string s;
map<string, int> map1;
map<int, string> map2;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;

	for (int i = 1; i <= n; i++)
	{
		cin >> s;
		map1[s] = i;
		map2[i] = s;
	}

	for (int i = 1; i <= m; i++)
	{
		cin >> s;
		if (atoi(s.c_str()) == 0)
		{
			cout << map1[s] << "\n";
		}
		else
		{
			cout << map2[atoi(s.c_str())] << "\n";
		}
	}

	return 0;
}

 

๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋‚ฌ์—ˆ๋Š”๋ฐ, ์•„๋ž˜ ๋‘๊ฐœ ์ถ”๊ฐ€ํ•ด์ฃผ๋‹ˆ ๋งž์ท„

ios::sync_with_stdio(0);
cin.tie(0);

 

์œ„์˜ ์ฝ”๋“œ๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์•„๋ž˜ ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ 

https://seize-the-day9.tistory.com/24

 

[C++] C++ ์ž…์ถœ๋ ฅ ์†๋„๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๋งž๋Š” ๋ฌธ์ œ์˜ ๋งž๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ผ๊ณ , ์ตœ์ ํ™”๋ฅผ ํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค๋ฉด! ์•„๋ž˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด ๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค! 1. ios_base::sync_with_stdio(false) ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ์œ„์˜ ์ฝ”

seize-the-day9.tistory.com