PKU 1208 The Blocks Problem

http://poj.org/problem?id=1208
ブロックの積み上げをシミュレーションする問題。

サイズが大きいわけでもないので、愚直にやっても問題ない。
デバッグに若干手間取りました。

vector<int> sta[25];
int n;

PI find(int x){
  rep(i,n){
    rep(j,SZ(sta[i])){
      if(sta[i][j]==x)return mp(i,j);
    }
  }
}

main(){
  string s,t;
  cin>>n;
  rep(i,n)sta[i].pb(i);
  
  while(cin>>s){
    if(s[0]=='q')break;
    int a,b;
    cin>>a>>t>>b;

    PI ap=find(a),bp=find(b);
    if(ap.F==bp.F)continue;
    
    if(s[0]=='m' && t[1]=='n'){
      for(;SZ(sta[ap.F])>ap.S+1;)sta[sta[ap.F].back()].pb(sta[ap.F].back()),sta[ap.F].pop_back();
      for(;SZ(sta[bp.F])>bp.S+1;)sta[sta[bp.F].back()].pb(sta[bp.F].back()),sta[bp.F].pop_back();
      sta[ap.F].pop_back();
      sta[bp.F].pb(a);
    }else if(s[0]=='m'){
      for(;SZ(sta[ap.F])>ap.S+1;)sta[sta[ap.F].back()].pb(sta[ap.F].back()),sta[ap.F].pop_back();
      sta[ap.F].pop_back();
      sta[bp.F].pb(a);      
    }else if(t[1]=='n'){
      for(;SZ(sta[bp.F])>bp.S+1;)sta[sta[bp.F].back()].pb(sta[bp.F].back()),sta[bp.F].pop_back();
      for(int i=ap.S;i<SZ(sta[ap.F]);i++)sta[bp.F].pb(sta[ap.F][i]);
      for(;SZ(sta[ap.F])>ap.S;)sta[ap.F].pop_back();
    }else{
      for(int i=ap.S;i<SZ(sta[ap.F]);i++)sta[bp.F].pb(sta[ap.F][i]);
      for(;SZ(sta[ap.F])>ap.S;)sta[ap.F].pop_back();
    }
  }
  rep(i,n){
    cout<<i<<':';
    rep(j,SZ(sta[i]))cout<<' '<<sta[i][j];
    cout<<endl;
  }
}