搜索
查看: 512|回复: 1
打印 上一主题 下一主题

12014 括号匹配 求解答

[复制链接]
跳转到指定楼层
楼主
发表于 2015-7-23 15:59:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
直接放上代码
[mw_shl_code=cpp,true]#include<iostream>
#include<stack>
using namespace std;


int main(){
   
   
    string match;
   
    cin>>match;
   
    stack<char> matchMachine;
    bool result = true; //匹配结果
    //匹配过程
   
   
   
   
    for( int i = 0; i < match.length(); i++ )
    {
          if( match[ i ] == '(' || match[ i ] == '{' || match[ i ] == '[' )
          matchMachine.push( match[ i ] );
          else if( match[ i ] == ')' || match[ i ] == '}' || match[ i ] == ']' )
          {
               if( !matchMachine.empty() )
               {
               char temp = matchMachine.top();
               
               if( temp == '(' && match[ i ] == ')' )
               matchMachine.pop();
               else if( temp == '[' && match[ i ] == ']' )
               matchMachine.pop();
               else if( temp == '{' && match[ i ] == '}' )
               matchMachine.pop();
               else
               {
                   result = false;
                   break;
                   }//end else
               
               }//end if
               else
               {
                   result = false;
                   break;
                   }//end else
               
               
           }//end else if
           


                  }//end for i

                  
                  if( result )
                  cout<<"YES"<<endl;
                  else
                  cout<<"NO"<<endl;
                  
         
   
    //system( "pause" );
    return 0;
   
    }//end main
[/mw_shl_code]

一直有一个测试出错,第10个测试样本,求AC了的给我看看,代码挂在哪里了,表示弄不到测试的数据
推荐
 楼主| 发表于 2015-7-23 21:13:26 | 只看该作者
本帖最后由 马小李 于 2015-7-25 13:37 编辑

终于过了,原因是没有考虑最后栈不空的状态
[mw_shl_code=cpp,true]#include<iostream>
#include<stack>
using namespace std;


int main(){
   
   
    string match;
   
    cin>>match;
    //cout<<match<<endl;
   
    stack<char> matchMachine;
    bool result = true; //匹配结果
    //匹配过程
   
    for( int i = 0; i < match.length(); i++ )
    {
          if( match[ i ] == '(' || match[ i ] == '{' || match[ i ] == '[' )
          matchMachine.push( match[ i ] );
          else if( match[ i ] == ')' || match[ i ] == '}' || match[ i ] == ']' )
          {
               if( !matchMachine.empty() )
               {
               char temp = matchMachine.top();
               
               if( temp == '(' && match[ i ] == ')' )
               matchMachine.pop();
               else if( temp == '[' && match[ i ] == ']' )
               matchMachine.pop();
               else if( temp == '{' && match[ i ] == '}' )
               matchMachine.pop();
               else
               {
                   result = false;
                   break;
                   }//end else
               
               }//end if
               else
               {
                   result = false;
                   break;
                   }//end else
               
               
           }//end else if
           


                  }//end for i
  
if( !matchMachine.empty() )
                  result = false;
                  
                  if( result )
                  cout<<"YES"<<endl;
                  else
                  cout<<"NO"<<endl;
                  
         
   
    //system( "pause" );
    return 0;
   
    }//end main
[/mw_shl_code]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表