C language-question about parentheses matching (no stack is used)

Source: Internet
Author: User

I recently posted a question on OJ of Nanyang sci-tech. I found that most of the answers to a question that is interesting on the Internet use stacks. Unfortunately, I have not learned the data structure yet, therefore, it can only be solved in a simple way.

Question link on this http://acm.nyist.net/JudgeOnline/problem.php? Pid = 2

The questions are as follows:

Description:

Now, there is a sequence of parentheses. Please check whether this line of parentheses is paired.

Input:

Enter N (0 <N <= 100) in the first row, indicating that N groups of test data exist. The next N rows Input Multiple groups of input data, each group of input data is one

String S (S length is less than 10000, and S is not an empty string), the number of test data groups is less than five. Data guarantee S only contains "[", "]", "(", ")" 4

.

Output:

The output of each group of input data occupies one row. If the brackets contained in the string are paired, Yes is output. If the string is not paired, No is output.

Example:

3

[(])

(])

([[] ()])

No

No

Yes

The Code is as follows:

#include<stdio.h>#include<string.h>#include<stdlib.h>int main(){    int n;    char str[10000];    void deal( char str[]);        scanf("%d",&n);    while(n--)    {        scanf("%s",str);        deal( str);    }}void deal( char str[]){    int len = strlen( str);    int i,j,n,flag;        if( len%2 != 0 || str[0] == ')' || str[0] == ']' )    {        printf("No\n");    }    else    {        for( i = 0; i < len; i++)        {            if( str[i] == ')' || str[i] == ']')            {                for( j = i-1; j > -1; j--)                {                    if( str[j] == '0' )                        continue;                    if( (int)(str[i] - str[j]) != 1 && (int)(str[i] - str[j]) != 2)                    {                        printf("No\n");                        return;                    }                    else                    {                        str[i] = str[j] = '0';                        break;                    }                }            }        }        if( str[ len-1] == '0' )            printf("Yes\n");    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.