Search
Duplicate
๐Ÿฅˆ

์—๋””ํ„ฐ

์ฃผ์ฐจ
15
๋ฌธ์ œ๋ฒˆํ˜ธ
1406
์–ธ์–ด
C++
ํ‹ฐ์–ด
์‹ค๋ฒ„
์œ ํ˜•
์ž๋ฃŒ๊ตฌ์กฐ
nj_Blog
O
nj_์ƒํƒœ
์™„๋ฃŒ
์ดํ•ด๋„
ํ’€์ด
์‚ฌ๋žŒ
์ดํ•ด๋„ 2
13 more properties

๋ฌธ์ œ๋งํฌ

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

์ฝ”๋“œ ์ œ์ถœ ๊ธฐ๋ก (๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์‹œ๊ฐ„)

๋ฉ”๋ชจ๋ฆฌ : 2808 KB
์‹œ๊ฐ„ : 40 ms

Code

#include <iostream> #include <stack> int N; std::stack <char> left; std::stack <char> right; std::stack <char> tmp; void input_faster() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); } void input() { std::string str; std::cin >> str; for(int i = 0; i < str.size() ; i++){ left.push(str[i]); } std::cin >> N; } void solve() { char func; while (N--){ std::cin >> func; if (func == 'P'){ char x; std::cin >> x; left.push(x); } else if (func == 'L'){ if (!left.empty()){ right.push(left.top()); left.pop(); } } else if (func == 'B'){ if (!left.empty()) left.pop(); } else if (func == 'D'){ if (!right.empty()){ left.push(right.top()); right.pop(); } } } } void print_val() { while (!left.empty()){ tmp.push(left.top()); left.pop(); } while (!tmp.empty()){ std::cout << tmp.top(); tmp.pop(); } while (!right.empty()){ std::cout << right.top(); right.pop(); } } int main() { input_faster(); input(); solve(); print_val(); return (0); }
C++
๋ณต์‚ฌ

๋ฉ”๋ชจ

๋ฌธ์ž์—ด์—์„œ ์ปค์„œ๋ฅผ ์ด๋™ํ•˜๊ณ  ์ค‘๊ฐ„์— ๋ฌธ์ž๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๊ณผ์ •์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด stack ๋‘๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํŽธ์˜์ƒ ๋‘ ์Šคํƒ์˜ ์ด๋ฆ„์„ left, right ๋ผ๊ณ  ํ•˜๊ณ 
์ดํ•ด๋ฅผ ์œ„ํ•ด right ์Šคํƒ์˜ ์ขŒ์šฐ๋ฅผ ๋ฐ˜์ „ํ•˜์—ฌ ๊ทธ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค!
๊ฐ๊ฐ์˜ ์—ฐ์‚ฐ์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๊ณ 
๊ฐ ์—ฐ์‚ฐ์—์„œ ์Šคํƒ์ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•˜๊ณ ! pop!์„ ํ•˜๋Š”๊ฑด ์ ˆ๋Œ€ ์žŠ์œผ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ท! ใ…Žใ…Ž