uva673 Parentheses Balance

题意:看括号是否匹配

解题思路:一直想用递归来做,发现做不出  后来才想到消去当今可以消去的() 与 【】 的括号  循环找就行了

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char str[1000];
int main()
{
    int t ;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        memset(str,0,sizeof(str));
        gets(str);
        int ok = 0;
        int k = strlen(str);
        int sum = 1;
        while(sum != 0)
        {
           sum = 0; 
           for(int i = 0 ;i < k;i ++)
           {
             if(str[i] == '(')
             {
               for(int j = i+1 ;j < k ; )
               {
                 if(str[j] == ')')
                 {
                   str[i] = '0';
                   str[j] = '0';
                   sum ++;
                   break;
                 }
                 else if(str[j] == '0')
                     j++;
                 else
                    break;
               
               }

             }
             if(str[i] == '[')
             {
               for(int j = i+1 ;j < k ; )
               {
                 if(str[j] == ']')
                 {
                   str[i] = '0';
                   str[j] = '0';
                   sum ++;
                   break;
                 }
                 else if(str[j] == '0')
                     j++;
                 else
                    break;
               
               }

             }

           }
           //puts(str);
        }
        for(int i = 0 ;i < k ;i ++)
            if(str[i] != '0')
              ok = 1;
        if(ok == 1)
           printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}
View Code

解题思路2:用栈的思路! 比较当前和栈顶元素

// File Name: uva6731.c
// Author: darkdream
// Created Time: 2013年05月15日 星期三 20时24分53秒

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
char str[1000];
int main(){

    //freopen("/home/plac/problem/input.txt","r",stdin);
    //freopen("/home/plac/problem/output.txt","w",stdout);
    int a[1000];
    int b[1000];
    int t ;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(str);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i = 0 ;i < strlen(str); i++)
        {
            if(str[i] == '(') 
                b[i] = 1;
            else if(str[i] == ')')
                b[i] = -1;
            else if(str[i] == '[')
                b[i] = 2;
            else if(str[i] == ']')
                b[i] = -2;    
        }
        int j = 0 ;
        for(int i = 0; i < strlen(str);i++)
        {
            if(b[i] != 0)
            {
                if(b[i] + a[j] == 0 && b[i] < 0)
                {      
                    a[j] = 0;
                        j--;

                }
                else
                {
                    j++;
                    a[j] = b[i];

                }
            }
        }
        if(a[1] == 0)
            printf("Yes\n");
        else 
            printf("No\n");
    }
    return 0 ;
}
View Code
没有梦想,何谈远方
原文地址:https://www.cnblogs.com/zyue/p/3080661.html