포스트

7785번

백준 문제풀이

7785번

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

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 1000000

typedef struct {
	char name[6];
	char action[6];
}LogEntry;

char stack[MAX][6];
int ii = 0;
void push(char* str) {
	strcpy(stack[ii++], str);
}
void pop() {
	ii--;
}

int reverse_strcmp(const void* a, const void* b) {
	return strcmp(((LogEntry*)b)->name, ((LogEntry*)a)->name); // 인수를 반대로 전달
}

int main() {
	int n;
	scanf("%d", &n);


	LogEntry* log = (LogEntry*)malloc(sizeof(LogEntry) * n);
	for (int i = 0;i < n;i++) {
		scanf("%s %s", log[i].name, log[i].action);
	}

	qsort(log, n, sizeof(LogEntry), reverse_strcmp);
	//qsort(b, n, sizeof(char) * 6, reverse_strcmp);

	/*
	for (int i = 0;i < n;i++) {
		printf("%s %s\n", log[i].name, log[i].action);
	}
	*/

	for (int i = 0;i < n;i++) {
		if (strcmp(log[i].action, "enter") == 0) {
			push(log[i].name);
		}
		else pop();

	}

	for (int i = 0;i < ii;i++) {
		printf("%s\n", stack[i]);
	}
	/*
	a
	a
	b
	b
	c
	
	*/


	free(log);

}