๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Baekjoon][๋ฌธ์ž์—ด] 9996๋ฒˆ ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€

ibelieveinme 2024. 3. 31. 01:25
728x90

[๋ฌธ์ œ]

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

 

9996๋ฒˆ: ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€

์ด N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ i๋ฒˆ์งธ ํŒŒ์ผ ์ด๋ฆ„์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋ฉด "DA", ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด "NE"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ, "DA"๋Š” ํฌ๋กœ์•„ํ‹ฐ์–ด์–ด๋กœ "YES"๋ฅผ, "NE"๋Š” "NO"๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

www.acmicpc.net

 

ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜์™€ ํŒจํ„ด์ด ์ฃผ์–ด์ง„๋‹ค.

ํŒจํ„ด์€ a*b ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฃผ์–ด์ง€๋˜, *์—” ๊ณต๋ž€์„ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ์ƒ์ดํ•œ ๋ฌธ์ž์—ด์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ๋‹ค.

ํŒŒ์ผ ์ด๋ฆ„์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋ฉด "DA", ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด "NE" ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ฃผ์˜ํ•  ์ ์€, a*b ์ด๋Ÿฐ ์‹์œผ๋กœ ํŒจํ„ด์€ 1๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ab*bc ์ด๋Ÿฐ ์‹์œผ๋กœ 2๊ธ€์ž, 3๊ธ€์ž... ๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ.

 

์˜ˆ์ œ ์ž…๋ ฅ

3
a*d
abcd
anestonestod
facebook

 

์˜ˆ์ œ ์ถœ๋ ฅ

DA
DA
NE

 

 

 

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

1. ํŒจํ„ด์— ํ•ด๋‹น๋˜๋Š” prefix, suffix ๋ฌธ์ž์—ด์„ ์ฐพ๊ธฐ ์œ„ํ•ด * ์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค. ๋ฌธ์ž์—ด find ํ•จ์ˆ˜ ์‚ฌ์šฉ.

2. *๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ prefix, suffix ๋กœ ๋‚˜๋ˆˆ๋‹ค. ๋ฌธ์ž์—ด substr ํ•จ์ˆ˜ ์‚ฌ์šฉ.

3. folder ์ด๋ฆ„์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๋งจ ์•ž๊ณผ ๋งจ ๋’ค๊ฐ€ prefix, suffix ์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

4. ์ด ๋•Œ, ์ถ”๊ฐ€ ์กฐ๊ฑด์œผ๋กœ folder name ์ด prefix + suffix ๊ฐ’๋ณด๋‹ค ํฐ์ง€๋ฅผ ํ™•์ธํ•ด์ค˜์•ผ ํ•œ๋‹ค. ํ™•์ธํ•˜์ง€ ์•Š์€์ฑ„ index +, - ๋กœ suffix ๋ฅผ ํ™•์ธํ•˜๋‹ค๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋งž์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

[์ฝ”๋“œ]

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

int main() {

	int num;
	string pattern, name;

	cin >> num;
	cin >> pattern;

	int starPos = pattern.find('*');
	string prefix = pattern.substr(0, starPos);
	string suffix = pattern.substr(starPos + 1);

	for (int i = 0; i < num; i++) {
		cin >> name;
		if (name.size() < prefix.size() + suffix.size()) {
			cout << "NE\n";
		}

		else{
			if(name.substr(0, prefix.size()) == prefix
			&& name.substr(name.size() - suffix.size()) == suffix) cout << "DA\n";
			else cout << "NE\n";
		}
	}
	
	return 0;
}
728x90