๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”ฅ PS(Problem Solving) ๐Ÿ”ฅ/BOJ

[BOJ] #5397 ํ‚ค๋กœ๊ฑฐ

by dar0m! 2020. 5. 21.
์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ •๋‹ต ๋น„์œจ
 1 ์ดˆ  256MB 23.480 %

 

 

5397๋ฒˆ: ํ‚ค๋กœ๊ฑฐ

๋ฌธ์ œ ์ฐฝ์˜์ด๋Š” ๊ฐ•์‚ฐ์ด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ›”์น˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ•์‚ฐ์ด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์ปดํ“จํ„ฐ์— ํ‚ค๋กœ๊ฑฐ๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค. ๋ฉฐ์น ์„ ๊ธฐ๋‹ค๋ฆฐ ๋์— ์ฐฝ์˜์ด๋Š” ๊ฐ•์‚ฐ์ด๊ฐ€ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐฝ์— ์ž…๋ ฅํ•˜๋Š” ๊ธ€์ž๋ฅผ ์–ป์–ด๋ƒˆ๋‹ค. ํ‚ค๋กœ๊ฑฐ๏ฟฝ

www.acmicpc.net

์Šคํƒ ๊ตฌํ˜„ํ•ด์„œ ํ’€๊ธ”-โญ

๋ฌธ์ œ

ํ•ด๊ฒฐ

key point, ์ปค์„œ์˜ ์ด๋™์„ ๊ตฌํ˜„ํ•œ๋‹ค. → cur

head → ์Šคํƒ ← tail

  • ์Šคํƒ์„ head์™€ tail์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค๊ณ 
  • ์ปค์„œ ์—ญํ• ์„ ํ•˜๋Š” ํฌ์ธํ„ฐ cur์„ ์ด์šฉํ•˜์—ฌ
  • < ์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด cur๋ฅผ ์™ผ์ชฝ์œผ๋กœ,
  • > ์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด cur๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๊ณ 
  • - ์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ปค์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ด์ „ ๋…ธ๋“œ์™€ ์ดํ›„๋…ธ๋“œ๋ฅผ ์„œ๋กœ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๊ณ ,
                                                ์ปค์„œ๊ฐ€ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ๋˜ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
  • ๊ทธ ์™ธ์˜ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•ด์„œ ์Šคํƒ๊ณผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

์ฝ”๋“œ

๋ฉ”๋ชจ๋ฆฌ ์‹œ๊ฐ„
72656KB 240 ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
 
// ๋”๋ธ” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ
// ์ฒ˜์Œ๊ณผ ๋์„ ์ค€ ๊ฒƒ. 
class Node {
public:
    char data;
    Node* prev;
    Node* next;
    Node() {
        prev = 0;
        next = 0;
    }
};
 
Node head;
Node tail;
char str[1000001];
 
 
int main() {
    int T;
    scanf("%d"&T);
    // ๋ฌธ์ž์—ด์„ ์ €์žฅํ•  ๋•Œ ๋ฒ”์œ„๋Š”  ์•„์Šคํ‚ค์ฝ”๋“œ. 
    //ํฌ์ธํ„ฐ๊ฐ€ ์ด ๊ฐ’์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ๋์ž„์„ ์•Œ๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด์„œ 
    head.data = -1;
    tail.data = -1;
 
    while(T--) {
        head.next = &tail;
        tail.prev = &head;
 
        scanf("%s", str);
        Node* cur; // ์ž…๋ ฅ๊ณผ ์ปค์„œ์˜ ์ด๋™ ์ˆ˜ํ–‰.
        cur = &head;
 
        for (int i = 0; str[i]; ++i) {
            if (str[i] == '<') {
                if (cur != &head) cur = cur->prev;
            }
            else if (str[i] == '>') {
                // ํ—ค๋“œ์—์„œ tail ์ง์ „๊นŒ์ง€๋งŒ ์›€์ง์ผ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ.
                if (cur->next != &tail) cur = cur->next;
            }
            else if (str[i] == '-') {
                // ๋ฉ”๋ชจ์žฅ์˜ ์ปค์„œ๋ฅผ ๋ดค์„ ๋•Œ ์˜ค๋ฅธ์ชฝ์— ์ƒˆ๋กœ์šด ์ž…๋ ฅ์ด ๋“ค์–ด์˜จ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ 
                // ๋ฐฑ์ŠคํŽ˜์ด์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ปค์„œ ์•ž์˜ ๋ฌธ์ž ํ•˜๋‚˜ ์‚ญ์ œ.
                if (cur != &head) {
                    Node* tmp = cur;
                    cur->prev->next = cur->next;
                    cur->next->prev = cur->prev;
                    cur = cur->prev;
                    free(tmp);
                }
            }
            else {
                Node* nNode = new Node;
                nNode->data = str[i];
                nNode->prev = cur;
                nNode->next = cur->next;
                //nNode->next->prev = nNode;
                cur->next->prev = nNode;
                cur->next = nNode;
                cur = nNode;
            }
        }
        Node* t = head.next;
        while (t->data != -1) {
            printf("%c", t->data);
            t = t->next;
        }
        printf("\n");
    }
 
    return 0;
}
cs

'๐Ÿ”ฅ PS(Problem Solving) ๐Ÿ”ฅ > BOJ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] #2164 ์นด๋“œ2  (0) 2020.06.02
[BOJ] #14830 ๊ฒฝ์‚ฌ๋กœ  (0) 2020.05.21
[BOJ] #1912 ์—ฐ์†ํ•ฉ  (0) 2020.05.11
[BOJ] #2512 ์˜ˆ์‚ฐ  (0) 2020.05.11
[BOJ] #13414 ์ˆ˜๊ฐ•์‹ ์ฒญ  (0) 2020.05.02

๋Œ“๊ธ€