Parentheses Balance

 ACM题http://online-judge.uva.es/problemset/v6/673.html

解答如下:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stack>


typedef struct StaKey
{
 int flag;
 char str;
}StaKey;

int main()
{
 using namespace std;

 int count;
 scanf("%d", &count);
 char str[128];
 int found,i;

 stack<StaKey> balace;
 while(count-- >0)
 {
  scanf("%s", &str);
  if(0==strlen(str))
  {
   printf("Yes/n");
   continue;
  }

  found = 0;
  for( i = 0;!found&&i<strlen(str);++i)
  {
   if('(' == str[i])
   {
    StaKey tmp;
    tmp.flag = 0;
    tmp.str = str[i];

    balace.push(tmp);
   }
   else if('[' == str[i] )
   {
    StaKey tep;
    tep.flag = 1;
    tep.str = str[i];

    balace.push(tep);
   }
   else if(')'== str[i] )
   {
    StaKey& temp = balace.top();
    
    if(balace.empty())
    {
    
     found = 1;
    }
    else if(0 == temp.flag)
    {
     balace.pop();
    }
    else
    {
     
     found = 1;
    }
   }
   else
   {
    StaKey& temp1= balace.top();
    if(balace.empty())
    {
     
     found = 1;
    }
    else if(1 == temp1.flag)
    {
     balace.pop();
    }
    else
    {
     
     found = 1;

    }
   }
  }
  if(!found&&balace.empty())
  {
   printf("Yes/n");
  }
  else
  {
   printf("No/n");
  }
  if(!balace.empty())
  {
   balace.pop();
  }
 }
 return 0;
}

在linux下运行是正常的,开始编译过程中,遇到一个问题,即把
typedef struct StaKey
{
 int flag;
 char str;
}StaKey;放在main里面时,编译有错,不知是不是结构定义不能作为局部变量的。不过最终代码测试是通过的。

  1. #include <iostream>
  2.  
  3. #include <stack>
  4.  
  5. #include <string>
  6.  
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. int main ( ) {
  14.  
  15.  
  16.  
  17.         int count;
  18.  
  19.         string in;
  20.  
  21.  
  22.  
  23.         cin >> count;
  24.  
  25.  
  26.  
  27.         stack<char> s;
  28.  
  29.         for ( int i = 0; i < count; ++i ) {
  30.  
  31.                 cin >> in;
  32.  
  33.                 while (!s. empty ( ) ) s. pop ( );
  34.  
  35.  
  36.  
  37.                 bool valid = true;
  38.  
  39.                 for ( int c = 0; valid && c < in. size ( ); ++c ) {
  40.  
  41.                         char _c = in. at (c );
  42.  
  43.                         if ( '(' == _c || '[' == _c ) s. push (_c );
  44.  
  45.                         if ( ')' == _c ) {
  46.  
  47.                                 if (!s. empty ( ) && '(' == s. top ( ) ) s. pop ( );
  48.  
  49.                                 else valid = false;
  50.  
  51.                         }
  52.  
  53.                         if ( ']' == _c ) {
  54.  
  55.                                 if (!s. empty ( ) && '[' == s. top ( ) ) s. pop ( );
  56.  
  57.                                 else valid = false;
  58.  
  59.                         }
  60.  
  61.                 }
  62.  
  63.  
  64.  
  65.                 if (valid && s. empty ( ) ) cout << "Yes" << endl;
  66.  
  67.                 else           cout << "No" << endl;
  68.  
  69.         }
  70.  
  71.  
  72.  
  73.         return 0;
  74.  
  75. }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值