๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BOJ] 9935 ๋ฌธ์ž์—ด ํญ๋ฐœ :: ๋ฌธ์ž์—ด ๋น„๊ต(stack, erase)

ibelieveinme 2024. 5. 15. 20:22
728x90

ํญ๋ฐœ, ์ง์ง“๊ธฐ ๋ฌธ์ œ๋Š” stack์„ ๋– ์˜ฌ๋ฆฌ์ž.

 

[๋ฌธ์ œ]

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

 

๋ฌธ์ž์—ด์— ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ์žˆ๋‹ค.

ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ํญ๋ฐœํ•˜๋ฉด ๊ทธ ๋ฌธ์ž๋Š” ๋ฌธ์ž์—ด์—์„œ ์‚ฌ๋ผ์ง€๋ฉฐ, ๋‚จ์€ ๋ฌธ์ž์—ด์€ ํ•ฉ์ณ์ง€๊ฒŒ ๋œ๋‹ค.

๋ชจ๋“  ํญ๋ฐœ์ด ๋๋‚œ ํ›„์— ์–ด๋–ค ๋ฌธ์ž์—ด์ด ๋‚จ๋Š”์ง€ ๊ตฌํ•ด๋ณด์ž. ๋‚จ์•„์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ "FRULA"๋ฅผ ์ถœ๋ ฅํ•˜์ž.

 

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 36๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

๋‘ ๋ฌธ์ž์—ด์€ ๋ชจ๋‘ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž, ์ˆซ์ž 0, 1, ..., 9๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

 

[ํ’€์ด]

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๋งค์šฐ ๊ธฐ๋ฏ€๋กœ, ์™„์ „ํƒ์ƒ‰์œผ๋กœ๋Š” ํ’€ ์ˆ˜ ์—†๋‹ค.

๋ฌธ์ž์—ด ๋น„๊ต๋ฅผ ์œ„ํ•ด stack ์ž๋ฃŒ๊ตฌ์กฐ๋‚˜ erase ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ for๋ฌธ ํ•˜๋‚˜๋กœ ๋๋‚ด์•ผ ํ•œ๋‹ค.

 

1๋ฒˆ ํ’€์ด

  1. stack์„ ์ด์šฉํ•ด์„œ stack ํฌ๊ธฐ๊ฐ€ ํญ๋ฐœ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋ณด๋‹ค ์ปค์กŒ์œผ๋ฉด ๋น„๊ต ์‹œ์ž‘ํ•œ๋‹ค.
  2. stack ์˜ ๋งจ ์œ„ ๊ธ€์ž์™€ ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๋งจ ๋์ž๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•œ๋‹ค.
  3. ๊ฐ™๋‹ค๋ฉด ํญ๋ฐœ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋นผ์„œ stack ์•ž์˜ ๋ฌธ์ž์—ด ๋น„๊ตํ•œ๋‹ค.
  4. 3์—์„œ stack ๋ฌธ์ž์—ด์€ ๋’ค์ง‘์–ด์ ธ ์žˆ์œผ๋‹ˆ๊นŒ reverse ํ•จ์ˆ˜๋กœ ๋’ค์ง‘๋Š”๋‹ค.
  5. ๋‹ค๋ฅด๋ฉด ๋‹ค์‹œ ์ง‘์–ด๋„ฃ๋Š”๋‹ค. ๊ฐ™์œผ๋ฉด ๋บ€ ์ƒํƒœ์ด๋ฏ€๋กœ ๋„˜๊ธด๋‹ค.
  6. stack์— ๋“ค์–ด์žˆ๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋ฉด ๋.
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;


//ํญ๋ฐœ, ์ง์ง“๊ธฐ => stack

int main() {
	string origin_str, bumb_str, answer_str = ""; //Input ๋ฌธ์ž์—ด, ํด๋ฐœ ๋ฌธ์ž์—ด, ์ •๋‹ต ๋ฌธ์ž์—ด
	stack<char> stk; // ํญ๋ฐœ ๋ฌธ์ž์—ด๊ณผ์˜ ๋น„๊ต๋ฅผ ์œ„ํ•œ stack

	cin >> origin_str >> bumb_str;

	//stack ์„ ์ด์šฉํ•œ ๋ฌธ์ž์—ด๋น„๊ต
	for (char s : origin_str) {
		stk.push(s); // ์ผ๋‹จ stack ์— ๋ฌธ์ž์—ด ์ฐจ๋ก€๋กœ ๋„ฃ๊ธฐ
		
		if (stk.size() >= bumb_str.size()) { //stack ํฌ๊ธฐ๊ฐ€ ํญ๋ฐœ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋ณด๋‹ค ์ปค์กŒ์œผ๋ฉด ๋น„๊ต ์‹œ์ž‘
			char stack_end = stk.top(); // stack ์˜ ๋งจ ์œ„ ๊ธ€์ž์™€
			char bumb_end = bumb_str[bumb_str.size() - 1]; // ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๋งจ ๋์ž๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ

			if (stack_end == bumb_end) { // ๊ฐ™๋‹ค๋ฉด ํญ๋ฐœ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋นผ์„œ stack ์•ž์˜ ๋ฌธ์ž์—ด ๋น„๊ต
				string compare_str = "";
				for (char i : bumb_str) {
					compare_str += stk.top();
					stk.pop();
				}
				reverse(compare_str.begin(), compare_str.end()); // stack ๋ฌธ์ž์—ด์€ ๋’ค์ง‘์–ด์ ธ ์žˆ์œผ๋‹ˆ๊นŒ ํ•œ๋ฒˆ ๋’ค์ง‘๊ธฐ
				if (bumb_str != compare_str) { //๋‹ค๋ฅด๋ฉด ๋‹ค์‹œ ์ง‘์–ด๋„ฃ๊ธฐ ๊ฐ™์œผ๋ฉด ๋บ€ ์ƒํƒœ์ž„
					for (int i : compare_str) {
						stk.push(i);
					}
				}
			}
		}
	}

	//์ถœ๋ ฅ
	if (stk.size() == 0) cout << "FRULA";
	else {
		while (stk.size()) {
			answer_str += stk.top(); stk.pop();
		}
		reverse(answer_str.begin(), answer_str.end());// stack ๋ฌธ์ž์—ด์€ ๋’ค์ง‘์–ด์ง„ ์ƒํƒœ๋‹ˆ๊นŒ ํ•œ๋ฒˆ ๋’ค์ง‘๊ธฐ
		cout << answer_str;
	}

	return 0;
}

 

*2๋ฒˆ ํ’€์ด

  1. ์ผ๋‹จ ์ž…๋ ฅ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์”ฉ ์ •๋‹ต ๋ฌธ์ž์—ด์— ๋„ฃ๋Š”๋‹ค.
  2. ์ •๋‹ต ๋ฌธ์ž์—ด์ด ํญ๋ฐœ ๋ฌธ์ž์—ด๋ณด๋‹ค ๊ธธ์ด๊ฐ€ ํฌ๊ฑฐ๋‚˜ ๊ฐ™์•„์ง€๋ฉด ๋น„๊ต๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.
  3. ํญ๋ฐœ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋’ท ๊ธ€์ž ์ถ”์ถœํ•œ๋‹ค.
  4. ์ถ”์ถœํ•œ ๋ฌธ์ž์—ด๊ณผ ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ํ™•์ธํ•œ๋‹ค. ๊ฐ™์œผ๋ฉด erase ํ•จ์ˆ˜๋กœ ์ œ๊ฑฐํ•œ๋‹ค.
#include <iostream>
#include <string>
using namespace std;

const string NULL_TEXT = "FRULA";

int main() {
	string origin_str, bumb_str, answer_str = "";

	cin >> origin_str >> bumb_str;

	for (char s : origin_str) {
		answer_str += s; 
		if (answer_str.size() >= bumb_str.size()) { 
			string end_str = answer_str.substr(answer_str.size() - bumb_str.size(), bumb_str.size()); 
			if (end_str == bumb_str) {
				answer_str.erase(answer_str.end() - bumb_str.size(), answer_str.end());
			}
		}
	}
	if (answer_str.size()) cout << answer_str << "\n";
	else cout << NULL_TEXT << "\n";

	return 0;
}

 

728x90