Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-YIM2UL2ET #31

Merged
merged 5 commits into from
Mar 19, 2024
Merged

10-YIM2UL2ET #31

merged 5 commits into from
Mar 19, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Mar 10, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

BOJ 1406 - ์—๋””ํ„ฐ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

70m + ฮฑ

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์„ค๋ช…

์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์ปค์„œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ”ํ•˜๊ฒŒ ์ปดํ“จํ„ฐ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ปค์„œ๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
L, D, B, P ๋ผ๋Š” 4๊ฐœ์˜ ๋ช…๋ น์–ด๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ•œ๋ฒˆ ์„ค๋ช… ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

KakaoTalk_20240310_185639377_01
KakaoTalk_20240310_185639377

์ด ๋ฌธ์ œ๋Š” ๋ช…๋ น์–ด ๊ฐœ์ˆ˜ n๊ณผ, ์ดˆ๊ธฐ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ ๋ฐ›์€ ํ›„, ๋งจ ๋’ค์— ์ปค์„œ๋ฅผ ๋‘” ํ›„์— n๊ฐœ์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์ตœ์ข…์ ์ธ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

2. ํ‹€๋ ธ๋˜ ํ’€์ด

์ฒ˜์Œ์—๋Š” ํด๋ž˜์Šค๋กœ ๋‹จ์ˆœํžˆ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

  1. ์šฐ์„  ๋ฌธ์ž์—ด ์„ str์ด๋ผ๋Š” stringํ˜• ๊ฐ์ฒด์— ๋ฐ›์€ ํ›„
  2. index๋ผ๋Š” ์ปค์„œ์˜ ์œ„์น˜๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ str.size()๋กœ ์ดˆ๊ธฐํ™”
  3. L๊ณผ D๊ฐ€ ๋‚˜์˜ค๋ฉด index ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ฆ๊ฐ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•˜์—ฌ ์กฐ์ •
  4. B๋Š” erase ํ•จ์ˆ˜๋ฅผ ํ†ตํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐ
  5. P๋Š” insert ํ•จ์ˆ˜๋ฅผ ํ†ตํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€

๋ช…๋ น์–ด๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ (ex. ์ปค์„œ๊ฐ€ ๋ฌธ์žฅ ๋งจ ์•ž์— ์žˆ๋‹ค๋ฉด ๋ฌด์‹œ)๋Š” if๋ฌธ์œผ๋กœ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

#include <iostream>  // ํด๋ž˜์Šค ๊ตฌํ˜„

class editor
{
private:
    int index;
    std::string str;
public:
    linked_list(std::string s) {str = s; index = s.size();}
    ~linked_list() {}

    void l(void) {
        if (index != 0) index--;
    }

    void d(void) {
        if (index != str.size()) index++;
    }

    void b(void) {
        if (index != 0) str.erase(str.begin() + index - 1), index--;
    }

    void p(char ch) {
        if (index == str.size()) str.push_back(ch), index++;
        else str.insert(str.begin() + index++, ch);
    }

    void display(void) {
        std::cout << str;
    }
};
#include <iostream>  // ๋ฉ”์ธํ•จ์ˆ˜ ๊ตฌํ˜„

int main(void)
{
    int n;
    char ch;
    std::string str;

    std::cin >> str >> n;
    editor res(str);
    for (int i = 0; i < n; i++) {
        std::cin >> ch;
        if (ch == 'P') {std::cin >> ch; res.p(ch);}
        else if (ch == 'L') res.l();
        else if (ch == 'D') res.d();
        else res.b();
    }
    std::cout << res.output();
    return 0;
}

์Šคํฌ๋ฆฐ์ƒท 2024-03-10 185816

์ด ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๋ฉด ์ •๋‹ต์€ ๋งž์ง€๋งŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค.

3. ๋งž์€ ํ’€์ด

1. ์Šคํƒ ํ™œ์šฉ ๋ฐฉ์‹

๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ์ปค์„œ์— ์ธ์ ‘ํ•ด ์žˆ๋Š” ๋ฌธ์ž๋“ค์„ ์‚ญ์ œ, ์ถ”๊ฐ€, ๋˜๋Š” ์ด๋™ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ
string ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง๊ณ  stack ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์–ด๋ด…์‹œ๋‹ค.

์šฐ์„  stack ๊ฐ์ฒด๋ฅผ ๋‘ ๊ฐœ ์„ ์–ธํ•ด์ค์‹œ๋‹ค.
stk1์€ ์ปค์„œ ์•ž์ชฝ ๋ฌธ์ž๋“ค์„, stk2๋Š” ์ปค์„œ ๋’ค์ชฝ ๋ฌธ์ž๋“ค์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

std::stack <char> stk1, stk2;

๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ์ปค์„œ์— ์ธ์ ‘ํ•ด ์žˆ๋Š” ๋ฌธ์ž๋“ค์„ ์‚ญ์ œ, ์ถ”๊ฐ€, ๋˜๋Š” ์ด๋™ํ•ด์•ผ ํ•˜๋ฏ€๋กœ
๋ฌธ์ž๋“ค์„ ์ €์žฅํ•˜๋Š” ์ด ๋‘ ๊ฐœ์˜ ์Šคํƒ์˜ top์€ ์„œ๋กœ ์ปค์„œ์— ์ธ์ ‘ํ•ด ์žˆ๋Š” ๋ฌธ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ top์—์„œ ๋ฉ€์–ด์งˆ ์ˆ˜๋ก ์ปค์„œ์—์„œ ๋ฉ€์–ด์ง€๋Š” ๋ฌธ์ž์—ฌ์•ผ๊ฒ ์ง€์š”.

๋”ฐ๋ผ์„œ ์ดˆ๊ธฐ์— ๋ฌธ์ž์—ด์„ ์ž„์˜์˜ string ๊ฐ์ฒด์ธ str์— ์ž…๋ ฅ์„ ๋ฐ›์€ ํ›„,
์ปค์„œ ์•ž ๋ฌธ์ž๋ฅผ ์ €์žฅํ•˜๋Š” stk1์— str[0]๋ถ€ํ„ฐ str[str.size()-1]๊นŒ์ง€ pushํ•ด์ค๋‹ˆ๋‹ค.

std::cin >> str >> n;
for (int i = 0; i < str.size(); i++) stk1.push(str[i]);

2. ๋ช…๋ น์–ด ๊ตฌํ˜„

์ด์ œ ๋ช…๋ น์–ด๋ฅผ ๊ตฌํ˜„ํ•ด๋ด…์‹œ๋‹ค.


L ๋ช…๋ น์–ด๋Š” ์ปค์„œ๊ฐ€ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
๊ทธ ๋ง์ธ ์ฆ‰์Šจ ์ปค์„œ ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ์ปค์„œ ๋’ค๋กœ ์ด๋™ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ stk1์˜ top์— ์œ„์น˜ํ•œ ๋ฌธ์ž๋ฅผ popํ•œ ํ›„ stk2์— push ํ•ด์ค์‹œ๋‹ค.
๋˜ํ•œ stk1์ด ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ ์ปค์„œ๊ฐ€ ๋ฌธ์ž์—ด ๋งจ ์•ž์— ์žˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ
stk1์ด ๋น„์–ด์žˆ์ง€ ์•Š์„ ๋•Œ๋งŒ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์ค์‹œ๋‹ค.

if (ch == 'L' && !stk1.empty()) stk2.push(stk1.top()), stk1.pop();

D ๋ช…๋ น์–ด๋Š” ์ปค์„œ๊ฐ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
L ๋ช…๋ น์–ด์™€ ์ •๋ฐ˜๋Œ€์ด์ฃ . ๋”ฐ๋ผ์„œ ๊ฐ ์Šคํƒ๋งŒ ์„œ๋กœ ๋ฐ”๊พธ์–ด์„œ ์ž‘์„ฑํ•ด์ค์‹œ๋‹ค.

if (ch == 'D' && !stk2.empty()) stk1.push(stk2.top()), stk2.pop();

B ๋ช…๋ น์–ด๋Š” ์ปค์„œ ์™ผ์ชฝ์— ์œ„์น˜ํ•œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
์ปค์„œ ์™ผ์ชฝ์— ์œ„์น˜ํ•œ ๋ฌธ์ž๋Š” stk1์˜ top์— ์ €์žฅ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ stk1์„ pop ํ•ด์ค์‹œ๋‹ค.
์ด์ „ ๋ช…๋ น์–ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ปค์„œ๊ฐ€ ๋งจ ์•ž์— ์žˆ์œผ๋ฉด ๋ช…๋ น์–ด๋ฅผ ๋ฌด์‹œํ•˜๋ฏ€๋กœ,
stk1์ด ๋น„์–ด์žˆ์ง€ ์•Š์„ ๋•Œ๋งŒ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์ค์‹œ๋‹ค.

if (ch == 'B' && !stk1.empty()) stk1.pop();

P ๋ช…๋ น์–ด๋Š” ๋ฌธ์ž ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„, ์ด๋ฅผ ์ปค์„œ ์™ผ์ชฝ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
๋ฌธ์ž๋Š” ์ž„์˜์˜ ch๋กœ ์ž…๋ ฅ์„ ๋ฐ›๊ณ , ์ปค์„œ ์™ผ์ชฝ์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ stk1์— pushํ•ด์ค์‹œ๋‹ค.

if (ch == 'P') std::cin >> ch, stk1.push(ch);

์ด n๊ฐœ์˜ ๋ช…๋ น์–ด๋ฅผ ๋ฐ›์œผ๋ฏ€๋กœ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

for (int i = 0; i < n; i++) {
        std::cin >> ch;
        if (ch == 'L' && !stk1.empty()) stk2.push(stk1.top()), stk1.pop();
        else if (ch == 'D' && !stk2.empty()) stk1.push(stk2.top()), stk2.pop();
        else if (ch == 'B' && !stk1.empty()) stk1.pop();
        else if (ch == 'P') std::cin >> ch, stk1.push(ch);
    }

3. ์ถœ๋ ฅ ๊ตฌํ˜„

n๊ฐœ์˜ ๋ช…๋ น์–ด๋ฅผ ๋‹ค ์ž…๋ ฅ ๋ฐ›์€ ํ›„ ์ฒ˜๋ฆฌํ–ˆ๋‹ค๋ฉด ์ด์ œ๋Š” ์ถœ๋ ฅ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ปค์„œ์— ์ธ์ ‘ํ•ด ์žˆ์„์ˆ˜๋ก ๊ฐ ์Šคํƒ์˜ top์— ๊ฐ€๊นŒ์ด ์œ„์น˜ํ•ด ์žˆ์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ์€ ๋ฌธ์ž์—ด์˜ ๋งจ ์•ž์ชฝ๋ถ€ํ„ฐ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•˜๋ฏ€๋กœ ์ปค์„œ๋ฅผ ๋งจ ์•ž์ชฝ์œผ๋กœ ์ด๋™ํ•ด์ค์‹œ๋‹ค.
stk1์ด ๋นŒ ๋•Œ ๊นŒ์ง€ L ๋ช…๋ น์–ด์™€ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์„ ์ทจํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์ง€์š”.

while(stk1.size() > 0) stk2.push(stk1.top()), stk1.pop();

์ด๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋‚˜๋ฉด stk2์˜ top์— ๊ฐ€๊นŒ์šด ๋ฌธ์ž๋Š” ๋ฌธ์ž์—ด ์•ž์ชฝ์— ์œ„์น˜ํ•ด ์žˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ
stk2๊ฐ€ ๋นŒ ๋•Œ ๊นŒ์ง€ ์ถœ๋ ฅ๊ณผ pop์„ ๋ฐ˜๋ณตํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

while(stk2.size() > 0) std::cout << stk2.top(), stk2.pop();

4. ์ตœ์ข… ์ฝ”๋“œ

#include <iostream>
#include <stack>

int main(void)
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    
    int n;
    char ch;
    std::string str;
    std::stack <char> stk1, stk2;

    std::cin >> str >> n;
    for (int i = 0; i < str.size(); i++) stk1.push(str[i]);
    
    for (int i = 0; i < n; i++) {
        std::cin >> ch;
        if (ch == 'L' && !stk1.empty()) stk2.push(stk1.top()), stk1.pop();
        else if (ch == 'D' && !stk2.empty()) stk1.push(stk2.top()), stk2.pop();
        else if (ch == 'B' && !stk1.empty()) stk1.pop();
        else if (ch == 'P') std::cin >> ch, stk1.push(ch);
    }

    while(stk1.size() > 0) stk2.push(stk1.top()), stk1.pop();
    while(stk2.size() > 0) std::cout << stk2.top(), stk2.pop();
    return 0;
}

5. ์™œ ํ‹€๋ ธ์„๊นŒ?

์ด์ „ ๋ฐฉ์‹์ด ์™œ ํ‹€๋ ธ๋Š”์ง€ ๊ถ๊ธˆํ•ด์„œ ํ•œ๋ฒˆ ์ฐพ์•„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
์ €๋Š” ์ฒ˜์Œ์— ์ž์ฒด์ ์œผ๋กœ ๋งŒ๋“  ํด๋ž˜์Šค๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์‹œ๊ฐ„์ด ๊ฝค ์žˆ๋Š” ๊ฑด๊ฐ€ ์‹ถ์—ˆ๋Š”๋ฐ, ๊ทธ๊ฒŒ ์•„๋‹ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ๋Š” string ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์ธ erase ํ•จ์ˆ˜์™€ insert ํ•จ์ˆ˜์— ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๊ธฐ๋ณธ์ ์œผ๋กœ ๋‘ ํ•จ์ˆ˜๋Š” O(n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ ‡๊ธฐ์— ์ตœ์•…์˜ ๊ฒฝ์šฐ์ธ ์ดˆ๊ธฐ ๋ฌธ์ž์—ด์ด 100,000์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๊ณ , P ๋ช…๋ น์–ด๊ฐ€ 500,000๋ฒˆ ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ
string ๋ฉค๋ฒ„ ํ•จ์ˆ˜์˜ ์—ฐ์‚ฐ ๋งŒ์œผ๋กœ๋„ ๋ฌด๋ ค 1,200์–ต ํšŒ์˜ ์—ฐ์‚ฐ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด ๋งž์€ ๊ฒฝ์šฐ์—์„œ ์Šคํƒ์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋“ค์€ ์ „๋ถ€ O(1)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์‹œ๊ฐ„์ด ํ›จ์”ฌ ์ ๊ฒŒ ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ‹€๋ฆฌ๊ณ  ๋‚˜์„œ ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ• ์ง€ ๊ฐ์ด ์ž˜ ์•ˆ ์™€์„œ ํƒœ๊ทธ๋ฅผ ๋ดค๋Š”๋ฐ ์Šคํƒ์ด๋ผ๊ณ  ๋œจ๋”๊ตฐ์š”..
๋ถ„๋ช… ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚  ์ค„ ์•Œ๊ณ  ํ˜น์‹œ ๋ชฐ๋ผ์„œ ๊ตฌํ˜„ํ–ˆ๋”๋‹ˆ ์ •๋ง ๋˜๋„ค์š”.
๋‚˜์ค‘์— string ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ˆ ์ฒซ ๋ฒˆ์งธ๋Š” ์™œ ์•ˆ ๋˜์—ˆ๋˜ ๊ฑด์ง€ ์ดํ•ด๊ฐ€ ๊ฐ‘๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ์Šคํƒ์„ ์“ธ ์ค„์€ ์ •๋ง ์ƒ์ƒ๋„ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋ถ„๋ช… ๊ตฌํ˜„ ๋ฌธ์ œ์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ ๋ง์ด์ฃ .
์ƒ๊ฐ์˜ ํ‹€์— ๊ฐ‡ํžˆ๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์€ ๊ฐš์ง„ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค..
๋‹ค์Œ๋ถ€ํ„ด ํ’€์ด ๋ฐฉ์‹์ด ๋– ์˜ค๋ฅด๋”๋ผ๋„ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ์‹์ด ์žˆ๋‚˜ ์ฒœ์ฒœํžˆ ์ƒ๊ฐํ•ด ๋ณด๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

์—ฌ๋‹ด์œผ๋กœ ์˜ค๋Š˜์€ ์•ฝ๊ฐ„ PR์Šคํƒ€์ผ์„ ๋ฐ”๊พธ์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
์ˆ˜๋„ ์ฝ”๋“œ์— ์žˆ๋Š” ๋ฌธ์ œ ์„ค๋ช…์˜ ์Šคํƒ€์ผ์„ ํ˜•์‹์ ์ธ ์„ค๋ช…์ด ์•„๋‹Œ ์ตœ๋Œ€ํ•œ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋ฐ”๊พธ๊ณ , ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์ ์ง€ ์•Š๊ณ  ํ’€์ดํ–ˆ๋˜ ๋ฐฉ์‹๋งŒ ์„ค๋ช…ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๋ณด์•˜๋Š”๋ฐ ์–ด๋– ์‹ ์ง€ ์˜๊ฒฌ์„ ๋“ฃ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค!

์ฐธ๊ณ ์ž๋ฃŒ:
erase ํ•จ์ˆ˜ ์‹œ๊ฐ„๋ณต์žก๋„
string ํด๋ž˜์Šค ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ์ฐธ๊ณ 

๊ทธ๋ฆฌ๊ณ  ํ˜น์‹œ๋ผ๋„ ์ดํ•ด๊ฐ€ ์•ˆ๋˜์‹œ๋Š” ๋ถ„๋“ค์„ ์œ„ํ•ด์„œ ์ €๋ณด๋‹ค ์„ค๋ช…์„ ์ž˜ํ•ด์ฃผ์‹  ๊ธ€์„ ๊ฐ€์ ธ์™”์œผ๋‹ˆ ์ฐธ๊ณ ํ•ด์ฃผ์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
BOJ 1406 ์„ค๋ช…

Copy link
Member

@kjs254 kjs254 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒ˜์Œ ๋ดค์„ ๋•Œ ์ €๋„ ์ธ๋ฑ์‹ฑ์œผ๋กœ ๊ตฌํ˜„ ํ•ด์•ผ๊ฒ ๋‹ค ๋ผ๊ณ  ๋‹น์—ฐํžˆ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ปค์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ 2๊ฐœ์˜ ์Šคํƒ ์‚ฌ์šฉ์„ ์ƒ๊ฐํ•˜๋‹ˆ ๋ฌธ์ œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๋‹จํ•ด์กŒ๋„ค์š”.

์†์œผ๋กœ ์ง์ ‘ ํ’€์–ด๋ณด๋‹ˆ ๊ทธ ์ฐจ์ด๊ฐ€ ์‹ค๊ฐ๋‚ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ ๋งˆ์ง€๋ง‰ ์ถœ๋ ฅ ๋ถ€๋ถ„์—์„œ ์ปค์„œ๋ฅผ ์ด๋™ํ•˜์—ฌ popํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค stk1๋’ค์— stk2์„ ๋ฐ˜๋Œ€๋กœ ๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋–จ๊นŒ ์‹ถ๋„ค์š”.

ํŒŒ์ด์ฌ์—์„  ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ตฌํ˜„์ด ๋˜๋Š”๋ฐ cpp์—์„œ ๊ตฌํ˜„์ด ๋  ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ณด์•˜์„ ๋•Œ ํฌ์ธํ„ฐ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปค์„œ ์œ„์น˜๋ฅผ ์˜ฎ๊ฒจ์•ผํ•˜๋‚˜? ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์—ˆ๋Š”๋ฐ ์ด ํ’€์ด๋กœ ํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œจ๊ฒ ์ง€์š”...

stack์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•˜๋Š” ๊ฒƒ์€ ๊ต‰์žฅํžˆ ์‹ ๋ฐ•ํ–ˆ๋˜๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!!
์ƒˆ๋กœ์šด ์ ‘๊ทผ๋ฐฉ์‹์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ข‹์€ ํ’€์ด์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.๐Ÿ˜

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stack์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์‹ ์ ์ด ์ธ์ƒ๊นŠ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. stack์„ ์ž˜ ์‚ฌ์šฉ์„ ์•ˆํ–ˆ๋Š”๋ฐ ์ด๋Ÿฐ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”. ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants