PTA bracket question

Parenthesis problem

Author: Li Tingyuan
Setting: China Civil Aviation Flight Institute
Time limit: 400 ms
Memory limit: 64 MB
Code length limit: 16 KB

Problem description

Given a string of characters, no more than 100 characters, which may include brackets, numbers, letters, punctuation marks, and spaces, programming checks whether (), [], {} in the string of characters > match.

Input format:

Input a line of string in one line, no more than 100 characters, including brackets, numbers, letters, punctuation marks, and spaces.

Output format:

If the brackets are paired, output yes, otherwise output no.

Input example 1:

sin(10+20)

Output sample 1:

yes

Input example 2:

{[}]

Output sample 2:

no

Code

#include<stdio.h>
#include<stdlib.h>
#define Maxsize 100

typedef struct SNode* Stack;
struct SNode {
    char data[Maxsize];
    int Top;
};

Stack create ();
int Push (Stack S, char x);
char Pop (Stack S);

int main ()
{
    char str[101];
    Stack S = create ();
    int i = 0, flag = 1;
    gets(str);
    while(str[i] != '\0')
    {
        if(str[i] == '(' || str[i] == '[' || str[i] == '{')
            Push(S, str[i]);
        else if(str[i] == ')' || str[i] == ']' || str[i] == '}')
        {
            if(S->Top == -1)
            {
                flag = 0;
                printf("no\n");
                break;
            }
            if(str[i] == ')')
            {
                if(S->Top == -1 || S->data[S->Top] != '(')
                {
                    flag = 0;
                    printf("no\n");
                    break;
                }
                else Pop(S);
            }
            if(str[i] == ']')
            {
                if(S->Top == -1 || S->data[S->Top] != '[')
                {
                    flag = 0;
                    printf("no\n");
                    break;
                }
                else Pop(S);
            }
            if(str[i] == '}')
            {
                if(S->Top == -1 || S->data[S->Top] != '{')
                {
                    flag = 0;
                    printf("no\n");
                    break;
                }
                else Pop(S);
            }
        }
        i++;
    }
    
    if(flag != 0)
    {
        if(S->Top == -1)
            printf("yes\n");
        else
            printf("no\n");
    }
    
    return 0;
}
Stack create ()
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Top = -1;
    
    return S;
}
int Push (Stack S, char x)
{
    if(S->Top == 99)
    {
        printf("Stack Full\n");
        return -1;
    }
    else
    {
        S->data[++(S->Top)] = x;
        return 1;
    }
}
char Pop (Stack S)
{
    if(S->Top == -1)
    {
        printf("Stack Empty\n");
        return -1;
    }
    else
        return (S->data[(S->Top)--]);
}

At first, I used the while ((CH = getchar())! = '\ n') to read characters, and the result has been timeout. I think it may be due to too many calls to getchar function.

Keywords: C Programming

Added by nkyoung1 on Sun, 24 Nov 2019 23:07:07 +0200