728x90
728x90
01 λ¬Έμ
02 μκ³ λ¦¬μ¦
- μ λ ₯λ λ¬Έμμ΄μ μ€νμ push, pop νλ©΄μ κ΄νΈμ μ§μ΄ λ§λμ§ νμΈνλ€.
- λ¬Έμμ΄μμ β(β μΌ λ pushν νμμ β)βμΌ λ popν νμκ° κ°μμΌ νλ€.
- is_empty ν¨μλ₯Ό μ΄μ©ν΄ pushν κ² λ€ pop λμλμ§ λΉκ΅νλλ°, popμ λ λ§μ΄ νλ κ²½μ°λ κ³ λ €ν΄μ£Όμ΄μΌ νλ―λ‘ stackμμ popν μμκ° μμ κ²½μ°μ vps = -1λ‘ ν΄μ£Όκ³ , μ΄ κ°μ΄ μ΄κΈ°κ°κ³Ό κ°μμ§ λΉκ΅ν¨.
03 μ½λ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 100
typedef struct array_stack
{
int arr[LEN];
int top;
}array_stack;
void stack_init(array_stack *pstack);
void push(array_stack *pstack, int x);
void pop(array_stack *pstack);
void size(array_stack *pstack);
int is_empty(array_stack *pstack);
void top(array_stack *pstack);
int vps;
int main(void)
{
int i, size, num;
char str[50];
array_stack stack;
scanf("%d", &num);
getchar();
while (num--)
{
scanf("%s", str);
size = strlen(str);
stack_init(&stack);
for (i = 0; i < size; i++)
{
if (str[i] == '(')
push(&stack, i);
else if (str[i] == ')')
pop(&stack);
}
if (is_empty(&stack) == 1 && vps == 0)
printf("YES\n");
else
printf("NO\n");
}
}
void stack_init(array_stack *pstack)
{
vps = 0;
pstack->top = -1;
}
void push(array_stack *pstack, int x)
{
// vps = 0;
++pstack->top;
pstack->arr[pstack->top] = x;
}
void pop(array_stack *pstack)
{
if (pstack->top == -1)
vps = -1;
else
pstack->top--;
}
void size(array_stack *pstack)
{
printf("%d\n", pstack->top + 1);
}
int is_empty(array_stack *pstack)
{
if (pstack->top == -1)
return 1;
else
return 0;
}
void top(array_stack *pstack)
{
if (pstack->top == -1)
printf("-1\n");
else
printf("%d\n", pstack->arr[pstack->top]);
}
728x90
728x90
'π Algorithm > μκ³ λ¦¬μ¦-Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μκ³ λ¦¬μ¦] Day2 - 2750λ² μ μ λ ¬νκΈ° (0) | 2018.08.04 |
---|---|
[μκ³ λ¦¬μ¦] Day2 - 2775λ² λΆλ νμ₯μ΄ λ ν μΌ (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 - μ€ν (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 - νμ (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 - μ ν λλ² (0) | 2018.08.04 |