我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

HLOJ_1013

Posted on 2009-07-13 09:25 Hero 阅读(153) 评论(0)  编辑 收藏 引用 所属分类: 代码如诗--ACM
 1 //HLOJ 1013  Accepted  15 196 916 C++  
 2 
 3 //当调用stack<char>.pop()的时候要注意当前栈是否为空
 4 
 5 #include <iostream>
 6 #include <string>
 7 #include <stack>
 8 using namespace std ;
 9 
10 string instr ;
11 
12 stack<char> mystack ;
13 
14 int main()
15 {
16     while( cin >> instr )
17     {
18         while!mystack.empty() ) mystack.pop() ;
19 
20         bool OK = true ;
21 
22         string::iterator posi = instr.begin() ;
23         for( ; posi!=instr.end(); posi++ )
24         {
25             if!OK ) break ;
26 
27             if'('==(*posi) || '['==(*posi) )
28             {
29                 mystack.push( *posi ) ;
30             }
31             else
32             {
33                 if( mystack.empty() ) 
34                 {
35                     OK = false ; break ;
36                 }
37                 if*posi == ')' )
38                 {
39                     if( mystack.top() != '(' )        
40                         OK = false ;
41                     else
42                         mystack.pop() ;
43                 }
44                 else if*posi == ']' )
45                 {
46                     if( mystack.top() != '[' )
47                         OK = false ;
48                     else
49                         mystack.pop() ;
50                 }                
51             }
52         }
53 
54         if!mystack.empty() ) OK = false ;
55 
56         if( OK ) cout << "yes" << endl ;
57         else     cout << "no"  << endl ;
58     }
59 
60     return 0 ;
61 }

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理