PKU 3290 WFF 'N PROOF

http://poj.org/problem?id=3290
条件を満たす文の内最長のものを作れというような問題。

くっつけられるものをくっつけていく。

main(){
  string in;
  while(cin>>in){
    if(in=="0")break;
    stack<char> sl,sn,so;
    rep(i,SZ(in)){
      if(islower(in[i]))sl.push(in[i]);
      else if(in[i]=='N')sn.push('N');
      else so.push(in[i]);
    }
    string ans;
    if(!sl.empty()){
      ans+=sl.top();
      sl.pop();
    }else{
      cout<<"no WFF possible"<<endl;
      continue;
    }
    
    while(!sl.empty() && !so.empty()){
      ans=so.top()+ans+sl.top();
      so.pop();sl.pop();
    }
    while(!sn.empty()){
      ans="N"+ans;
      sn.pop();
    }
    cout<<ans<<endl;
  }
}