홍시홍의 프로그래밍

[백준 1158] 조세퍼스 문제 본문

알고리즘 문제풀이/백준

[백준 1158] 조세퍼스 문제

홍시홍 2019. 11. 18. 01:31

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

 

1406번: 에디터

문제 한 줄로 된 간단한 에디터를 구현하려고 한다. 이 편집기는 영어 소문자만을 기록할 수 있는 편집기로, 최대 600,000글자까지 입력할 수 있다. 이 편집기에는 '커서'라는 것이 있는데, 커서는 문장의 맨 앞(첫 번째 문자의 왼쪽), 문장의 맨 뒤(마지막 문자의 오른쪽), 또는 문장 중간 임의의 곳(모든 연속된 두 문자 사이)에 위치할 수 있다. 즉 길이가 L인 문자열이 현재 편집기에 입력되어 있으면, 커서가 위치할 수 있는 곳은 L+1가지 경우가

www.acmicpc.net

 

요구사항

1. 리스트 구현

 - 리스트 사용

2. 조건 구현

 - 조건에 명시된 대로 명령어를 처리해주면 된다

 - iter로 auto cursor로 커서 위치 표시

 

 

#include <iostream>
#include <string>
#include <list>
using namespace std;

struct NODE {
	char val;
	int prev;
	int next;
};
const int NODE_SIZE = 9876543;

struct MY_LIST {
	int HEAD = NODE_SIZE;
	int TAIL = NODE_SIZE + 1;
	int pos = 0;
	NODE node[NODE_SIZE + 2];
	MY_LIST() {
		pos = 0;
		node[HEAD].next = TAIL;
		node[TAIL].prev = HEAD;
	}
	int size = 0;
	void insert(int p, char data)
	{
		int next = node[HEAD].next;
		for (int i = 0; i < p; i++)
		{
			next = node[next].next;
		}
		int prev = node[next].prev;
		node[pos].val = data;

		node[pos].next = next;
		node[next].prev = pos;

		node[pos].prev = prev;
		node[prev].next = pos;
		size++;
		pos++;
	}
	void push_back(char data)
	{
		int next = TAIL;
		int prev = node[TAIL].prev;

		node[pos].val = data;

		node[pos].next = next;
		node[next].prev = pos;

		node[pos].prev = prev;
		node[prev].next = pos;
		pos++;
		size++;
	}
	
	void at(int p)
	{
		int next = node[HEAD].next;
		for (int i = 0; i < p; i++)
		{
			next = node[next].next;
		}
		cout << node[next].val;
	}
	
	void erase(int p)
	{
		int target = node[HEAD].next;
		for (int i = 0; i < p; i++)
		{
			target = node[target].next;
		}
		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
		
		size--;
	}
};

int main()
{
	ios_base::sync_with_stdio(false);
	string str;
	int n;
	cin >> str;
	//MY_LIST list;
	list<char> li;
	for (int i = 0; i < str.size(); i++)
	{
		li.push_back(str[i]);
	}
	auto cursor = li.end();
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		char x, y;
		cin >> x;
		if (x == 'P')
		{
			cin >> y;
			li.insert(cursor, y);
		}
		else if (x == 'L')
		{
			if (cursor != li.begin())
			{
				cursor--;
			}
		}
		else if (x == 'D')
		{
			if (cursor != li.end())
			{
				cursor++;
			}
		}
		else if (x == 'B')
		{
			if (cursor != li.begin())
			{
				cursor--;
				li.erase(cursor++);
				//cursor++;
			}
		}
	
	}
	
	for (auto x : li)
	{
		cout << x;
	}

	
	return 0;
}

 

'알고리즘 문제풀이 > 백준' 카테고리의 다른 글

[백준 1764] 듣보잡(해시 구현)  (0) 2019.11.20
[백준 1764] 듣보잡  (0) 2019.11.18
[백준 1158] 조세퍼스 문제  (0) 2019.11.16
[백준 17837] 새로운 게임2  (0) 2019.11.16
[백준 17822] 원판 돌리기  (0) 2019.11.14
Comments