Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string s;
  9.     while(cin>>s){
  10.         bool isOk=true;
  11.         stack<string> stk;
  12.        
  13.         for( int i=s.length()-1 ; i>=0 ; i--){
  14.             string sentence;       
  15.  
  16.             if(s[i]>='p' && s[i]<='z'){
  17.                 sentence = s[i];
  18.                 stk.push(sentence);
  19.             }
  20.             else if(s[i] == 'N'){
  21.                 if( stk.empty() ){
  22.                     isOk = false;
  23.                     break;
  24.                 }
  25.                 sentence = 'N' + stk.top();
  26.                 stk.pop();
  27.                 stk.push(sentence);
  28.             }
  29.             else if( s[i] == 'C' || s[i] == 'D' || s[i] == 'E' || s[i] == 'I' ){
  30.                 if(stk.size()<2 ){
  31.                     isOk = false;
  32.                     break;
  33.                 }
  34.                 sentence = stk.top();
  35.                 stk.pop();
  36.                 //sentence = s[i]+ sentence + stk.top();
  37.                 sentence = s[i]+ stk.top() + sentence;
  38.                 stk.pop();
  39.                 stk.push(sentence);
  40.             }
  41.             else{
  42.                 isOk = false;
  43.                 break;
  44.             }
  45.         }
  46.         if(stk.size()!=1){
  47.             isOk = false;
  48.         }
  49.         cout<<((isOk)? "YES":"NO")<<endl;
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement