프로그래밍 알고리즘

[정올 3123] 키로거(Keylogger)

꾸준한사람 2023. 1. 10. 01:21
반응형

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=2405&sca=99&sfl=wr_hit&stx=3123 

 

JUNGOL

 

www.jungol.co.kr

#include <stdio.h>

int T;
char Sent[1000010];
typedef struct Node
{
	Node() : pPrev(nullptr), pNext(nullptr), c(0) {}
	Node* pPrev;
	Node* pNext;
	char c;
};

Node Head, Tail, *pCursor;
void Add(char c)
{
	Node* pNew = new Node;
	Node* pOldPrev = pCursor->pPrev;

	pOldPrev->pNext = pNew;

	pNew->pPrev = pOldPrev;
	pNew->pNext = pCursor;
	pNew->c = c;

	pCursor->pPrev = pNew;
}
void Right()
{
	if (pCursor->pNext != &Tail)
	{
		Node* pOldNext = pCursor->pNext;
		Node* pOldDbNext = pOldNext->pNext;
		Node* pOldPrev = pCursor->pPrev; //아무것도 없어도 H가 있음
		pOldPrev->pNext = pOldNext;
		pOldNext->pPrev = pOldPrev;

		pOldNext->pNext = pCursor;
		pCursor->pPrev = pOldNext;

		pCursor->pNext = pOldDbNext;
		pOldDbNext->pPrev = pCursor;
	}
}
void Left()
{
	if (pCursor->pPrev != &Head)
	{
		Node* pOldNext = pCursor->pNext; //아무것도 없어도 T가 있음
		Node* pOldPrev = pCursor->pPrev;
		Node* pOldDbPrev = pOldPrev->pPrev;
		pOldDbPrev->pNext = pCursor;
		pCursor->pPrev = pOldDbPrev;

		pCursor->pNext = pOldPrev;
		pOldPrev->pPrev = pCursor;

		pOldPrev->pNext = pOldNext;
		pOldNext->pPrev = pOldPrev;		
	}
}
void Delete()
{
	if (pCursor->pPrev != &Head)
	{
		Node* pOldPrev = pCursor->pPrev;
		Node* pOldDbPrev = pOldPrev->pPrev;
		pOldDbPrev->pNext = pCursor;
		pCursor->pPrev = pOldDbPrev;
		delete pOldPrev;
	}
}
void Init()
{
	Head.pNext = pCursor;
	Tail.pPrev = pCursor;
	pCursor->pPrev = &Head;
	pCursor->pNext = &Tail;
}

int main(void)
{
	pCursor = new Node;
	Init();
	pCursor->c = 1;

	scanf("%d", &T);
	for (int i = 0; i < T; i++)
	{
		scanf("%s", Sent);
		char c;
		for (int j = 0; Sent[j] != 0; j++)
		{
			c = Sent[j];
			switch (c)
			{
			case '<':
				Left();
				break;
			case '>':
				Right();
				break;
			case '-':
				Delete();
				break;
			default:
				Add(c);
				break;
			}
		}
		//출력
		for (Node* p = Head.pNext; p != &Tail;)
		{
			Node* pDel = p;
			p = p->pNext;
			if (pDel == pCursor)
			{
				continue;
			}
			printf("%c", pDel->c);
			delete pDel;
		}
		printf("\n");
		//초기화
		Init();
	}

	return 0;
}
반응형