๊ธฐ๋ก๋ฐฉ

BOJ_2204 : ๋„๋น„์˜ ๋‚œ๋…์ฆ ํ…Œ์ŠคํŠธ ๋ณธ๋ฌธ

CodingTest/C++

BOJ_2204 : ๋„๋น„์˜ ๋‚œ๋…์ฆ ํ…Œ์ŠคํŠธ

Soom_1n 2022. 5. 18. 01:53

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

2204๋ฒˆ: ๋„๋น„์˜ ๋‚œ๋…์ฆ ํ…Œ์ŠคํŠธ

๊ฟ์€ ๋„๋น„์—๊ฒŒ ์˜์–ด๋‹จ์–ด๋“ค์„ ์ œ์‹œํ•œ ํ›„ ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„œ๋Š”์ง€ ๋งž์ถ”๋ฉด ์–‘๋ง์„ ์ฃผ์–ด ์ž์œ ๋ฅผ ์–ป๊ฒŒํ•ด์ค€๋‹ค๊ณ  ํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ ์ธ์„ฑ์ด ์ข‹์ง€ ์•Š์€ ๊ฟ์€ ์‚ฌ์‹ค

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž ํ˜น์€ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด ์ง€๋„๋ก ํ†ต์ผ์‹œํ‚ค๋Š” ๋ณ€ํ™˜์ด ํ•„์š”ํ•˜๋‹ค.
  • ์‚ฌ์ „์‹ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์—์„œ ๊ฐ€์žฅ ์•ž์—์˜ค๋Š” ๊ฒƒ์„ ๊ณ ๋ฅธ๋‹ค.
  • ์ž…๋ ฅ๋ฐ›์ž๋งˆ์ž ์ „ ๊ฐ’๊ณผ ๋น„๊ตํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ, ์˜ค๋ฅ˜๊ฐ€ ๋งŽ์ด ๋‚˜์„œ ์ •๋‹ต ํฌ์ŠคํŒ…์„ ์ฐธ์กฐํ–ˆ๋‹ค.

๐Ÿ”ธ ๊ธฐ์กด ์ฝ”๋“œ (์˜ค๋‹ต)๐Ÿ”ธ

#include<iostream>
#include<string>

using namespace std;

int main(void) {
	int n;
	string input, answer, temp;

	do {
		cin >> n;
		answer = "ZZZZZZZZZZZZZZZZZZZZ";
		for (int i = 0; i < n; i++) {
			cin >> input;
			temp = "";
			for (int j = 0; j < input.size(); j++) {
				if (input[j] > 'Z')
					temp += input[j] - ('a' - 'A');
				else
					temp += input[j];
			}
			int size = input.size() < answer.size() ? input.size() : answer.size();
			bool flag = true;
			for (int j = 0; j < size; j++) {
				if (temp[j] < answer[j])
					break;
				else if (temp[j] > answer[j]) {
					flag = false;
					break;
				}
			}
			if (flag && answer.size() >= input.size()) answer = input;
		}
		if (n != 0)
			cout << answer << endl;
	} while (n != 0);
	return 0;
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด input์„ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด ์ง€๋„๋ก ๋ณ€ํ™˜ ํ›„ temp ์— ์ €์žฅํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€๊ธธ์ด๊ฐ€ 20์ด๋ฏ€๋กœ ์‚ฌ์ „์ˆœ ๊ฐ€์žฅ ๋’ค์— ์žˆ๋Š” z*20 ์„ answer์œผ ์ดˆ๊ธฐ๊ฐ’ ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ–ˆ๋‹ค.
  • answer์™€ temp๋ฅผ ๋น„๊ตํ•ด ์‚ฌ์ด์ฆˆ๊ฐ€ ์ž‘์€ ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๋งŒํผ ํ•œ ๋ฌธ์ž์”ฉ ๋น„๊ตํ•œ๋‹ค.
    • ๊ฒฐ๊ณผ : 50%์—์„œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋‚˜์™”๋‹ค.

๐Ÿ”ธ ์ •๋‹ต ์ฝ”๋“œ ๐Ÿ”ธ

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

bool check(std::string a, std::string b) {
	for (int i = 0; i < a.size(); i++)
		if (a[i] >= 'a' && a[i] <= 'z') a[i] -= 32;
	for (int i = 0; i < b.size(); i++)
		if (b[i] >= 'a' && b[i] <= 'z') b[i] -= 32;
	if (a.compare(b) > 0) return false;
	return true;
}

int main(void) {
	int n;
	string input, answer;
	
	cin >> n;

	while (n != 0) {
		vector<string> strs(n);
		for (int i = 0; i < n; i++) {
			cin >> input;
			strs[i] = input;
		}
		sort(strs.begin(), strs.end(), check);
		if(n != 0)
			cout << strs[0] << endl;
		cin >> n;
	}
	return 0;
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ string ๋ฒกํ„ฐ์— ์ €์žฅํ•œ๋‹ค.
  • ์‚ฌ์ „์‹ ์ •๋ ฌ ํ•จ์ˆ˜ check๋ฅผ ๋งŒ๋“ค๊ณ , sort ํ•จ์ˆ˜์˜ ๋น„๊ต๊ธฐ ํ•จ์ˆ˜๋กœ ํ™œ์šฉํ–ˆ๋‹ค.
    • checkํ•จ์ˆ˜์—์„œ ๋ณต์‚ฌ๋œ ๋ฌธ์ž์—ด a์™€ b๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  compare ํ•จ์ˆ˜๋กœ ๋น„๊ตํ•œ๋‹ค.
    • compare ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์ด ๋น„๊ตํ•  ๋ฌธ์ž์—ด๋ณด๋‹ค ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์— ์˜ค๋ฉด -1, ๊ฐ™์œผ๋ฉด 0, ์‚ฌ์ „์ˆœ ๋’ค์— ์˜ค๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ a, b๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ a๊ฐ€ ์‚ฌ์ „์ˆœ ์•ž์—์˜ค๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์งˆ๊นŒ๋ด ๋˜๋„๋ก ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋ฐ, sort์— ๋น„๊ต๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋„ฃ์Œ์œผ๋กœ์จ ํŽธํ•˜๊ฒŒ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค. ๋น„๊ต๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋” ์—ฐ์Šตํ•ด ๋ณผ ์ƒ๊ฐ์ด๋‹ค.
  • compare๋กœ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ฌธ์ž์—ด์˜ ์‚ฌ์ „์‹ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ์‚ฌ์ „์‹ ์ •๋ ฌ์€ ์ž์ฃผ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ ๊ฐ™์€๋ฐ ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ๋กœ ๊ผญ ๊ธฐ์–ตํ•ด ๋†“์•„์•ผ๊ฒ ๋‹ค.

 

728x90