直接放上代码
[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了的给我看看,代码挂在哪里了,表示弄不到测试的数据
|