백준 5430: AC(C언어)
5430번: AC
각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.
www.acmicpc.net
문제
선영이는 주말에 할 일이 없어서 새로운 언어 AC를 만들었다. AC는 정수 배열에 연산을 하기 위해 만든 언어이다.
이 언어에는 두 가지 함수 R(뒤집기)과 D(버리기)가 있다.
함수 R은 배열에 있는 수의 순서를 뒤집는 함수이고, D는 첫 번째 수를 버리는 함수이다.
배열이 비어있는데 D를 사용한 경우에는 에러가 발생한다. 함수는 조합해서 한 번에 사용할 수 있다.
예를 들어, "AB"는 A를 수행한 다음에 바로 이어서 B를 수행하는 함수이다.
예를 들어, "RDD"는 배열을 뒤집은 다음 처음 두 수를 버리는 함수이다.
배열의 초기값과 수행할 함수가 주어졌을 때, 최종 결과를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 테스트 케이스의 개수 T가 주어진다. T는 최대 100이다.
각 테스트 케이스의 첫째 줄에는 수행할 함수 p가 주어진다. p의 길이는 1보다 크거나 같고, 100,000보다 작거나 같다.
다음 줄에는 배열에 들어있는 수의 개수 n이 주어진다. (0 ≤ n ≤ 100,000)
다음 줄에는 [x1,...,xn]과 같은 형태로 배열에 들어있는 정수가 주어진다. (1 ≤ xi ≤ 100)
전체 테스트 케이스에 주어지는 p의 길이의 합과 n의 합은 70만을 넘지 않는다.
출력
각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.
문제풀이
위의 내용을 정리해보면
1. 테스트 케이스의 개수 T를 입력받는다.
2. 수행할 명령 p를 입력받는다 (1 <= p <= 100,000)
3. 배열에 들어갈 수의 개수 n을 입력받는다 (0 <= n <= 100,000)
4. 배열에 들어갈 정수를 입력받는다 (1 <= x <= 100)
5. 명령을 p번 반복한다.
정리한 내용을 바탕으로 포인트 변수를 통해서 연결리스트를 만들었다.
입력을 받을 때 문자와 숫자 둘 다 받기 때문에 입력을 받는 과정에서 신경써서 받아줘야 한다.
숫자 배열에 들어갈 숫자의 개수가 0개일 때의 경우와 0개가 아닐 때의 경우 2가지로 나눠서 받아줬다.
또한 error 가 이미 발생했을 경우와 아닌 경우를 나눠서 출력을 해줬다.
세부설명
// 입력받은 정수를 넣을 때 node를 생성해주는 함수
struct node* createNode(int key);
// 정수를 연결리스트에 저장하는 함수
void insert(int key);
// 명령 D를 받았을 때 연결리스트에서 노드를 지우는 함수
void remover(int flag);
// error로 끝나지 않았을 때 현재 연결리스트의 배열 상태 출력하는 함수
void display(int flag);
// 메모리 사용량을 줄이기 위해 사용한 함수 (사용 안해도 정답으로 출력됨)
void node_free();
// 메인 함수의 변수 용도 설명
int T = 테스트 케이스 개수
int n = 입력받을 정수의 개수
int x = 입력받은 정수를 임시로 저장
int str_index = 명령을 받은 문자열의 인덱스 이동을 위한 변수
int flag = 배열의 순서를 정방향인지 역방향인지 정하는 변수(1:정방향, 0:역방향)
int error = error를 출력해줘야 하는지 체크를 위한 변수
char str[100001] = 명령을 저장하는 변수
char str_temp = [ , ] 를 입력받는 변수 (사실상 쓰레기통)
최종코드
#include<stdio.h>
#include<stdlib.h>
struct bucket* hashTable = NULL;
struct node {
int num;
struct node* next;
struct node* front;
};
struct bucket {
int count;
struct node* head;
struct node* tail;
};
struct node* createNode(int key) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->num = key;
newNode->next = NULL;
newNode->front = NULL;
return newNode;
}
void insert(int key) {
struct node* newNode = createNode(key);
if (hashTable->count == 0) {
hashTable->head = newNode;
hashTable->tail = newNode;
hashTable->count = 1;
}
else {
newNode->front = hashTable->tail;
hashTable->tail->next = newNode;
hashTable->tail = newNode;
hashTable->count++;
}
}
void remover(int flag) {
struct node* node = NULL;
// 정방향
if (flag) {
node = hashTable->head;
hashTable->head = node->next;
if(hashTable->head != NULL) hashTable->head->front = NULL;
}
// 역방향
else {
node = hashTable->tail;
hashTable->tail = node->front;
if (hashTable->tail != NULL) hashTable->tail->next = NULL;
}
hashTable->count--;
free(node);
}
void display(int flag) {
struct node* horse = NULL;
if (!hashTable->count) {
printf("[]\n");
return;
}
// 정방향
if (flag) {
horse = hashTable->head;
printf("[%d", horse->num);
horse = horse->next;
while (horse != NULL) {
printf(",%d", horse->num);
horse = horse->next;
}
printf("]\n");
}
// 역방향
else {
horse = hashTable->tail;
printf("[%d", horse->num);
horse = horse->front;
while (horse != NULL) {
printf(",%d", horse->num);
horse = horse->front;
}
printf("]\n");
}
}
void node_free() {
struct node* horse = hashTable->head;
while (hashTable->count) {
struct node* node = horse->next;
free(horse);
horse = node;
hashTable->count--;
}
}
int main() {
int T, n, x, str_index, flag, error;
char str[100001], str_temp;
hashTable = (struct bucket*)malloc(sizeof(struct bucket));
scanf("%d", &T);
for (int i = 0; i < T; i++) {
hashTable->count = 0;
hashTable->head = NULL;
hashTable->tail = NULL;
str_index = 0, flag = 1, error = 0;
scanf("%s %d", str, &n);
scanf(" %c", &str_temp);
if (n) {
for (int j = 0; j < n; j++) {
scanf("%d %c", &x, &str_temp);
insert(x);
}
}
else scanf(" %c", &str_temp);
for (int j = 0; str[j] != '\0'; j++) {
if (str[j] == 'R') flag = (flag + 1) % 2;
else {
if (!hashTable->count) {
error++;
break;
}
remover(flag);
}
}
if (error) printf("error\n");
else display(flag);
node_free();
}
free(hashTable);
}