UVA101-STL Simulation

Source: Internet
Author: User

A somewhat complex STL simulation problem, the STL iterator is not familiar with the change for a long time, finally a out.

Feel more convenient with arrays ... But to practice the STL.

Compared to the code on the White book, I still write a silly point. At first did not understand the meaning of four operations, simple simulation.

#include <algorithm>#include<cstring>#include<ctype.h>#include<cstdlib>#include<cstdio>#include<vector>#include<string>#include<queue>#include<stack>#include<cmath>#include<Set>#include<map>using namespacestd;intN,m,t;vector<int> v[ -];intGetPosintx) {     for(intI=0; i<n;i++) {vector<int>::iterator it =V[i].begin (); It=Find (V[i].begin (), V[i].end (), x); if(It! =V[i].end ()) {            returni; }    }    return-1;}voidRepose (intXinta) {     for(vector<int>::iterator it = V[x].begin (); It! = V[x].end (); + +it) {        if(*it = =a) {if(++it = = V[x].end ())return;  for(vector<int>::iterator it2 = it;it2! = V[x].end (); + +it2) {                intK = *it2; Vector<int>::iterator Beg =V[k].begin (); //printf ("%d%d\n", K, (int) v[k].size ()); //-while (1);V[k].insert (beg,k);            } v[x].erase (It,v[x].end ());  Break; }    }    return;}voidMove_onto (intAintb) {    intx = GetPos (a), y =GetPos (b);    Repose (X,a); repose (Y,B);  for(vector<int>::iterator it = V[x].begin (); It! = V[x].end (); + +it) {        if(*it = =a) {v[x].erase (IT);  Break; }    }     for(vector<int>::iterator it = V[y].begin (); It! = V[y].end (); + +it) {        if(*it = =b) {v[y].push_back (a);  Break; }    }    return;}voidMove_over (intAintb) {    intx = GetPos (a), y =GetPos (b);    Repose (X,a);  for(vector<int>::iterator it = V[x].begin (); It! = V[x].end (); + +it) {        if(*it = =a) {v[x].erase (IT);  Break;    }} v[y].push_back (a); return;}voidPile_over (intAintb) {    intx = GetPos (a), y =GetPos (b);  for(vector<int>::iterator it = V[x].begin (); It! = V[x].end (); + +it) {        if(*it = =a) { for(vector<int>::iterator it2 = it;it2! = V[x].end (); ++it2) V[y].push_back (*it2);            V[x].erase (It,v[x].end ());  Break; }    }    return;}voidPile_onto (intAintb) {    intx = GetPos (a), y =GetPos (b);    Repose (Y,B);  for(vector<int>::iterator it = V[x].begin (); It! = V[x].end (); + +it) {        if(*it = =a) { for(vector<int>::iterator it2 = it;it2! = V[x].end (); ++it2) V[y].push_back (*it2);            V[x].erase (It,v[x].end ());  Break; }    }    return;}intMain () { while(~SCANF ("%d",&N)) {intb; Charop[Ten],s[Ten];  for(intI=0; i<n;i++) {v[i].clear (); V[i].push_back (i);}  while(SCANF ("%s", op) && op[0] !='Q') {scanf ("%d%s%d",&a,s,&b); if(GetPos (a) = = GetPos (b))Continue; if(!STRCMP (OP,"Move") &&!strcmp (s),"onto") ) Move_onto (A, b); Else if(!STRCMP (OP,"Move") &&!strcmp (s)," Over") ) Move_over (A, b); Else if(!STRCMP (OP,"Pile") &&!strcmp (s),"onto") ) Pile_onto (A, b); Else if(!STRCMP (OP,"Pile") &&!strcmp (s)," Over") ) Pile_over (A, b); }         for(intI=0; i<n;i++) {printf ("%d:", i);  for(intj =0; J < (int) V[i].size (); j + +) printf ("%d", V[i][j]); printf ("\ n"); }    }}

UVA101-STL Simulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.