[BOJ] 9935๋ฒˆ | ๋ฌธ์ž์—ด ํญ๋ฐœ (C++)

2023. 10. 8. 15:21ใ†Coding Test/BOJ

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

9935๋ฒˆ: ๋ฌธ์ž์—ด ํญ๋ฐœ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘˜์งธ ์ค„์— ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 36๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘ ๋ฌธ์ž์—ด์€ ๋ชจ

www.acmicpc.net

 

๐Ÿง‘‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

string์„ ์Šคํƒ(stack)์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ •๋‹ต์„ ์ €์žฅํ•  string ๋ณ€์ˆ˜(answer)๋ฅผ ๋”ฐ๋กœ ์ค€๋น„ํ•œ ํ›„, ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ฐจ๋ก€๋Œ€๋กœ ์ˆœํšŒํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

  1. ํ˜„์žฌ ์ฝ์€ ๋ฌธ์ž์—ด์„ answer ๋ณ€์ˆ˜์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
  2. ํ˜„์žฌ ์ฝ์€ ๋ฌธ์ž์—ด์ด ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์™€ ๊ฐ™๋‹ค๋ฉด, ๋‹ค์Œ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.
    • answer์™€ ํญ๋ฐœ ๋ฌธ์ž์—ด์„ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฝ์œผ๋ฉฐ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.
    • ๋‘ ๋ฌธ์ž์—ด์ด ์„œ๋กœ ๊ฐ™๋‹ค๋ฉด, ํญ๋ฐœ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋งŒํผ answer์—์„œ pop_back()์„ ํ•ด์ค๋‹ˆ๋‹ค. 
  3. ๋ฌธ์ž์—ด ์ˆœํšŒ๋ฅผ ๋งˆ์นœ ํ›„ answer๊ฐ€ ๋น„์—ˆ๋‹ค๋ฉด "FRULA"๋ฅผ, ์•„๋‹ˆ๋ผ๋ฉด answer๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <string>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);

bool IsExplosionString(const string& explosionString, const string& targetString)
{
	if (targetString.size() < explosionString.size())
		return false;

	int targetIdx = targetString.size() - 1;
	for (int expIdx = explosionString.size() - 1; expIdx >= 0;)
	{
		if (targetString[targetIdx--] != explosionString[expIdx--])
			return false;
	}

	return true;
}

int main()
{
	FAST_IO
	string str, explosionString;
	cin >> str >> explosionString;

	string answer;
	for (const auto& character : str)
	{
		answer.push_back(character);

		if (character == explosionString.back())
		{
			if (!IsExplosionString(explosionString, answer))
				continue;

			for (int i = 0; i < explosionString.size(); i++)
				answer.pop_back();
		}
	}

	if (answer.empty())
		cout << "FRULA";
	else
		cout << answer;
	return 0;
}

 

728x90
๋ฐ˜์‘ํ˜•