Simulate the question and write the program directly according to the content described in the question.
It should be noted that illegal input: For a = B or A and B in the same heap input commands directly ignore.
Returns the volume I index.
Returns the total index.
// 101_the_blocks_problem.cpp: defines the entry point for the console application. <br/> // </P> <p> # include <set> <br/> # include <map> <br/> # include <list> <br/> # include <queue> <br/> # include <stack> <br/> # include <vector> <br/> # include <string> <br/> # include <sstream> </P> <p> # include <cstdio> <br/> # include <cmath> <br/> # include <limits> <br/> # include <utility> </P> <p> # ifndef online_judge <br/> # includ E <fstream> <br/> STD: ifstream CIN ("in.txt"); <br/> STD: ofstream cout ("out.txt "); <br/> // STD: ofstream cout (stdout ); <br/> # else <br/> # include <iostream> <br/> # endif </P> <p> typedef long llong; <br/> typedef unsigned long ullong; <br/> typedef long double ldouble; </P> <p> using namespace STD; </P> <p> vector <int> blocks; <br/> vector <pair <int, int> position; </P> <p> void returnblocksontopo F (INT block) <br/>{< br/> int nstack = position [block]. first; <br/> int noffset = position [block]. second; </P> <p> for (INT I = noffset + 1; I <blocks [nstack]. size (); ++ I) <br/>{< br/> int x = blocks [nstack] [I]; <br/> blocks [X]. push_back (x); <br/> position [x] = make_pair (x, 0); <br/>}</P> <p> blocks [nstack]. resize (noffset + 1); <br/>}</P> <p> void mergestack (INT source, int target) <br/>{< br/> int nsourcestac K = position [Source]. first; <br/> int nsourceoffset = position [Source]. second; </P> <p> int ntargetstack = position [target]. first; <br/> int ntargetoffset = position [target]. second; </P> <p> If (nsourcestack! = Ntargetstack) <br/>{< br/> for (INT I = nsourceoffset; I <blocks [nsourcestack]. size (); ++ I) <br/>{< br/> int x = blocks [nsourcestack] [I]; <br/> position [x] = make_pair (ntargetstack, blocks [ntargetstack]. size (); <br/> blocks [ntargetstack]. push_back (x); <br/>}</P> <p> blocks [nsourcestack]. resize (nsourceoffset); <br/>}</P> <p> int main (INT argc, char * argv []) <br/>{< br/> int nblocks; <br/> CIN> nblocks; </P> <p> blocks. resize (nblocks); <br/> position. resize (nblocks); </P> <p> for (INT I = 0; I <nblocks; ++ I) <br/> {<br/> blocks [I]. push_back (I); <br/> position [I] = make_pair (I, 0); <br/>}</P> <p> (;;) <br/>{< br/> string command, type; <br/> int source, target; </P> <p> CIN> command; <br/> If (command [0] = 'q') <br/> break; </P> <p> CIN> source> type> target; </P> <p> If (Source = target | position [Source]. first = position [target]. first) <br/> continue; </P> <p> switch (type [1]) <br/> {<br/> case 'N ': // onto <br/> returnblocksontopof (target); <br/> break; </P> <p> case 'V': // over <br/> break; <br/>}</P> <p> switch (command [0]) <br/>{< br/> case 'M ': // move <br/> returnblocksontopof (source); <br/> break; </P> <p> case 'p': // pile <br/> break; <br/>}</P> <p> mergestack (source, target); <br/>}</P> <p> for (INT I = 0; I <nblocks; ++ I) <br/>{< br/> cout <I <":"; </P> <p> If (blocks [I]. size ()> 0) <br/> {<br/> for (Int J = 0; j <blocks [I]. size (); ++ J) <br/> cout <"" <blocks [I] [J]; <br/>}</P> <p> cout <"/N"; <br/>}</P> <p> return 0; <br/>}</P> <p>
101-the blocks Problem