ํฌ์ŠคํŠธ

๐ŸŒ“ Baekjoon 1406 - '์—๋””ํ„ฐ'

๐Ÿ’ซ ๋ฌธ์ œ


๐Ÿซง Example Input/Output

1
2
3
4
5
6
7
8
9
10
11
12
13
// IN
abcd // ์ดˆ๊ธฐ ๋ฌธ์ž์—ด (์˜์–ด ์†Œ๋ฌธ์ž, ์ตœ๋Œ€ 100,000 ๊ธ€์ž)
3 // m : ๋ช…๋ น์–ด ์ˆ˜ (1 <= m <= 500,000)
B // ์ปค์„œ ์™ผ์ชฝ ์‚ญ์ œ (์ปค์„œ๋Š” ์ฒ˜์Œ์— ๋ฌธ์ž์—ด ๋งจ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•ด ์žˆ์Œ)
P d // d ๋ฅผ ์ปค์„œ ์™ผ์ชฝ์— ์ถ”๊ฐ€
P x // x ๋ฅผ ์ปค์„œ ์™ผ์ชฝ์— ์ถ”๊ฐ€
L // ์ปค์„œ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
D // ์ปค์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
L
P y

// OUT
abcdyx


๐Ÿ’ซ C++ ํ’€์ด


๐Ÿซง 1. ์•ผ๋งค ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <algorithm>

using namespace std;

// ๋ฐ”ํ‚น๋… ์„ผ์„ธ์˜ ์•ผ๋งค ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ
const int MAX = 1000005;
char dat[MAX];
int nxt[MAX], pre[MAX];
int unused = 1;

void insert(int addr, char newData)
{
	dat[unused] = newData;
	nxt[unused] = nxt[addr];
	pre[unused] = addr;

	nxt[addr] = unused;
	if (nxt[unused] != -1)
		pre[nxt[unused]] = unused;

	unused++;
}

void erase(int addr)
{
	nxt[pre[addr]] = nxt[addr];
	if (nxt[addr] != -1) pre[nxt[addr]] = pre[addr];
}

void traverse()
{
	int cur = nxt[0];
	while(cur != -1)
	{
		cout << dat[cur];
		cur = nxt[cur];
	}
	cout << '\n';
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	fill(pre, pre + MAX, -1);
	fill(nxt, nxt + MAX, -1);

	string s;
	cin >> s;

	int cursor = 0;
	for (auto c : s)
	{
		insert(cursor, c);
		cursor = nxt[cursor];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m; i++)
	{
		char command;
		cin >> command;

		if (command == 'L')
		{
			if (pre[cursor] != -1)
				cursor = pre[cursor];
		}
		else if (command == 'D')
		{
			if (nxt[cursor] != -1)
				cursor = nxt[cursor];
		}
		else if (command == 'B')
		{
			if (pre[cursor] == -1)
				continue;
			erase(cursor);
			cursor = pre[cursor];
		}
		else if (command == 'P')
		{
			cin >> command;
			insert(cursor, command);
			cursor = nxt[cursor];
		}
	}
	traverse();
}


๐Ÿซง 2. STL list

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
#include <iostream>
#include <algorithm>
#include <list>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	list<char> l;

	string s;
	cin >> s;

	for (auto c : s)
		l.push_back(c);

	list<char>::iterator cursor = l.end();
	
	int m;
	cin >> m;

	for (int i = 0; i < m; i++)
	{
		char command;
		cin >> command;

		if (command == 'L')
		{
			if (cursor == l.begin())
				continue;
			cursor--;
		}
		else if (command == 'D')
		{
			if (cursor != l.end())
				cursor++;
		}
		else if (command == 'B')
		{
			if (cursor == l.begin())
				continue;
			cursor = l.erase(--cursor);
		}
		else if (command == 'P')
		{
			cin >> command;
			l.insert(cursor, command);
		}
	}

	for (auto c : l)
		cout << c;
}


๐Ÿ’ซ Memo


ํžŒํŠธ0
ํžŒํŠธ1

  • ๋ฐ”ํ‚น๋… ์„ผ์„ธ์˜ ๊ฐ•์˜ ์˜์ƒ์„ ํ•œ ๋ฒˆ ๋ณธ ์ดํ›„ ํ’€๊ธฐ ์‹œ์ž‘
    • ์•ผ๋งค ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋Š” Dummy Node๊ฐ€ ๊ฐ€์žฅ ์•ž์— ์žˆ๊ณ , STL list๋Š” Dummy Node๊ฐ€ ๊ฐ€์žฅ ๋’ค์— ์žˆ๋‹ค๋Š” ๋Œ“๊ธ€์„ ๋ดค์—ˆ๋Š”๋ฐ, ๊ทธ๊ฒŒ ํžŒํŠธ๊ฐ€ ๋๋‹ค.
    • ์œ„ ๊ทธ๋ฆผ์€ ๊ฐ ํ’€์ด ๋ฐฉ๋ฒ• ๋ณ„ ์ปค์„œ ์œ„์น˜ (์ƒ๊ฐํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ปค์„œ๊ฐ€ ๋ฐ์ดํ„ฐ ์œ„์— ์œ„์น˜ํ•˜๋Š”๊ฑธ๋กœ)
  • ์•ผ๋งค ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ํ’€ ๋•Œ ๋ฒ„๊ทธ๋กœ ์žก๋Š” ๊ฒŒ ๋„ˆ๋ฌด ํž˜๋“ค์—ˆ๋‹ค.
  • STL list๋กœ ๋จผ์ € ํ‘ผ ๋‹ค์Œ, ์•ผ๋งค ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํ’€๊ธฐ ์‹œ์ž‘ํ•˜๋‹ˆ ์กฐ๊ธˆ ์ˆ˜์›”ํ–ˆ๋‹ค.

  • ๋‹ค ํ’€๊ณ  ๋ฐ”ํ‚น๋… ์„ผ์„ธ์˜ ๋‹ต์•ˆ๊ณผ ์ฒดํฌํ•ด๋ณด๋‹ˆ ๋˜‘๊ฐ™์ด ํ’€์–ด์„œ ์•ˆ์‹ฌํ–ˆ๋‹ค.

  • @ TIME : ์Šคํƒ์„ ์ด์šฉํ•œ ํ’€์ด


์ด ๊ธฐ์‚ฌ๋Š” ์ €์ž‘๊ถŒ์ž์˜ CC BY 4.0 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.