๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Baekjoon][map] 1620 ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ(map/arr ์‹œ๊ฐ„๋ณต์žก๋„, map value๋กœ key์ฐพ๊ธฐ)

ibelieveinme 2024. 4. 14. 21:02
728x90

[๋ฌธ์ œ]

 

1620๋ฒˆ: ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

์ฒซ์งธ ์ค„์—๋Š” ๋„๊ฐ์— ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ๊ฐœ์ˆ˜ N์ด๋ž‘ ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ ธ. N๊ณผ M์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ธ๋ฐ, ์ž์—ฐ์ˆ˜๊ฐ€ ๋ญ”์ง€๋Š” ์•Œ์ง€? ๋ชจ๋ฅด๋ฉด

www.acmicpc.net

์ฒซ์งธ ์ค„์—๋Š” ๋„๊ฐ์— ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ๊ฐœ์ˆ˜ N์ด๋ž‘ ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค.

N๊ณผ M์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ํฌ์ผ“๋ชฌ์˜ ๋ฒˆํ˜ธ๊ฐ€ 1๋ฒˆ์ธ ํฌ์ผ“๋ชฌ๋ถ€ํ„ฐ N๋ฒˆ์— ํ•ด๋‹นํ•˜๋Š” ํฌ์ผ“๋ชฌ๊นŒ์ง€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ๋‹ค.

 

๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ์ด M๊ฐœ์˜ ์ค„์— ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ๋‹ค.

 

์ตœ์ข…์ ์œผ๋กœ ์ž…๋ ฅ์œผ๋กœ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์™”๋‹ค๋ฉด ๊ทธ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์„, ๋ฌธ์ž๊ฐ€ ๋“ค์–ด์™”์œผ๋ฉด ๊ทธ ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

 

[ํ•ด๊ฒฐ๋ฐฉ๋ฒ•]

*1์ฐจ์‹œ๋„:

ํฌ์ผ“๋ชฌ ๋„๊ฐ์„ map<int, string> mp; ๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

๋ฌธ์ œ๊ฐ€ string์ธ์ง€ ์ˆซ์ž์ธ์ง€ ํŒ๋ณ„ํ•œ ํ›„, string์ด๋ฉด map value๋กœ key ๋ฅผ ์ฐพ๋„๋กํ•˜๊ณ  int๋ฉด key๋กœ value๋ฅผ ์ฐพ๋„๋ก ๊ตฌํ˜„ํ–ˆ๋‹ค.

=> ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ์‹คํŒจ.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
	int mapSize, quizNum;
	string input;
	map<int, string> mp;

	cin >> mapSize >> quizNum;
	for (int i = 1; i <= mapSize; i++) {
		cin >> input;
		mp.insert({ i, input });
	}

	for (int i = 0; i < quizNum; i++) {
		cin >> input;
		//๋ฌธ์ž
		if (atoi(input.c_str()) == 0) {
			for (auto& it : mp) {
				if (it.second == input) {
					cout << it.first << "\n";
				}
			}
		}
		//์ˆซ์ž
		else {
			cout << mp[stoi(input)] << "\n";
		}
	}
	return 0;
}

๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” 2๋ฒˆ์งธ for๋ฌธ์—์„œ value๋กœ key ๊ฐ’์„ ์ฐพ์„ ๋•Œ map์ด iterator ์ˆœํšŒ๋ฅผ ํ•ด์•ผํ•˜๋ฏ€๋กœ ์ตœ์•…์˜ ๊ฒฝ์šฐ n*n = 100,000*100,000 = 10,000,000,000์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. 100์–ตใ…Ž. ์ œํ•œ์‹œ๊ฐ„์ด 1์ดˆ์ด๋ฏ€๋กœ 1์–ต ๋ฏธ๋งŒ์ด ๋‚˜์™€์•ผํ•˜๋Š”๋ฐ ๋‹น์—ฐํžˆ ์‹œ๊ฐ„์ดˆ๊ณผ์ด๋‹ค.

 

*2์ฐจ์‹œ๋„: ๋‹ต์•ˆ์ฒ˜๋Ÿผ map๊ณผ array ์ž๋ฃŒ๊ตฌ์กฐ 2๊ฐœ ์‚ฌ์šฉํ•˜๊ธฐ.

string ์œผ๋กœ int ๋ฅผ ์ฐพ์„ ๋•Œ,

map ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(logN) / array ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ด๋‹ค. (map ์ด ๋ ˆ๋“œ๋ธ”๋ž™ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜์œผ๋กœ ํƒ์ƒ‰ํ•ด์„œ O(logN) ์ด ๊ฑธ๋ฆฐ๋‹ค.)

 

int ๋กœ string ์„ ์ฐพ์„ ๋•Œ,

map ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(logN) / array ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(1) ์ด๋‹ค. ex) arr[1] = "Bulbasaur"

 

๋”ฐ๋ผ์„œ, array ์™€ map ์ž๋ฃŒ๊ตฌ์กฐ ๋‘๊ฐœ๋ฅผ ๊ฐ๊ฐ์˜ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ตœ์ข…์ ์œผ๋กœ O(N)์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

 

[์•Œ์•„๋‘˜ ๊ฒƒ]

* ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์ด ๋ฌธ์ž์—ด์ธ์ง€ ํŒ๋…ํ•˜๋Š” ํ•จ์ˆ˜: atoi(s.c_str()). ๊ฐ’์ด 0์ด๋ฉด ๋ฌธ์ž์—ด, 0์ด ์•„๋‹ˆ๋ฉด ์ˆซ์ž์ด๋‹ค.

https://cplusplus.com/reference/string/string/c_str/

 

https://cplusplus.com/reference/string/string/c_str/

Complexity, iterator, access, exceptions Unspecified or contradictory specifications. Complexity Constant. Iterator validity No changes. Data races The object is accessed. Exception safety No-throw guarantee: this member function never throws exceptions.

cplusplus.com

 

*๋ฌธ์ž์—ด -> ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜: stoi(s)

 

*map ์—์„œ value ๊ฐ’์œผ๋กœ key ๊ฐ’ ์ฐพ๋Š” ๋ฒ•: ๋ฐ˜๋ณต๋ฌธ iterator ๋ฅผ ์ด์šฉํ•ด์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ์•„๋ž˜ 3๊ฐ€์ง€์ธ๋ฐ ๊ธฐ์–ต๋‚˜๋Š”๊ฑฐ ์“ฐ์ž!

for (auto it = map.begin(); it != map.end(); it++) {
    if (it->second == target) {
        keys.push_back(it->first);
    }
}
for (auto& it : mp) {
    if (it.second == target) {
        cout << it.first << "\n";
    }
}
for (pair<int, string> p : mp) {
    if (p.second == target) {
        cout << p.first << "\n";
    }
}

 

[์ฝ”๋“œ]

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int mapSize, quizNum;
	string input;

	cin >> mapSize >> quizNum;

	map<string, int> mp;
	vector<string> v(mapSize + 1, "");

	//๋„๊ฐ ์ •๋ณด
	for (int i = 1; i <= mapSize; i++) {
		cin >> input;
		mp.insert({ input, i });
		v[i] = input;
	}

	//๋ฌธ์ œ
	for (int i = 0; i < quizNum; i++) {
		cin >> input;
		//๋ฌธ์ž
		if (atoi(input.c_str()) == 0) {
			cout << mp[input] << "\n";
		}
		//์ˆซ์ž
		else {
			cout << v[stoi(input)] << "\n";
		}
	}
	return 0;
}

 

์ฐธ๊ณ ๋กœ

ios_base::sync_with_stdio(false); 
cin.tie(NULL); cout.tie(NULL);

์š” ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ค˜์•ผ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค. ๊ด€๋ จ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ… ์ฐธ๊ณ .

 

https://i-believe-in-me.tistory.com/387

 

ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

๊ฐ€๋” ์ œํ•œ์‹œ๊ฐ„์„ ๋„˜๊ธฐ์ง€ ์•Š์•˜๋Š”๋ฐ๋„ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ ๋• ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ฝ”๋“œ ์‹œ์ž‘ ์ „์— ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ์ด ๋˜๊ธฐ๋„ ํ•œ๋‹ค. ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ios_base::sync_wi

i-believe-in-me.tistory.com

 

728x90