반응형
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=2405&sca=99&sfl=wr_hit&stx=3123
#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;
}
반응형
'프로그래밍 알고리즘' 카테고리의 다른 글
[정올 3136] const구간의 합 구하기(2D) (0) | 2023.01.10 |
---|---|
[정올 3135] const구간의 합 구하기(1D) (0) | 2023.01.10 |
[정올 3115] 긴 자리 나눗셈 (0) | 2023.01.10 |
[정올 3106] 진법 변환 (0) | 2023.01.09 |
[정올 3101] 요세푸스 문제1 (0) | 2023.01.09 |