Programming Question # List
Source: POJ (Coursera statement: The exercises completed on POJ will not count against the final results of Coursera. )
Note: Total time limit: 4000ms memory limit: 65536kB
Describe
Write a program to complete the following commands:
New id--Create a sequence with a specified ID (id<10000)
Add ID num--adds integer num to a sequence numbered ID
Merge Id1 id2--merges the number in the sequence ID1 and Id2 and empties the ID2
Unique id--removes duplicate elements from the sequence ID
Out id--elements in a sequence with a small to large output number ID, separated by a space
Input
The first row is a number n, which indicates how many commands (n<=200000). One command per line after n rows.
Output
Output according to the requirements of the topic.
Sample input
16new 1new 2add 1 1add 1 2add 1 3add 2 1add 2 2add 2 3add 2 4out 1out 2merge 1 2out 1out 2unique 1out 1
Sample output
1 2 3 1 2 3 41 1 2 2 3 3 41 2 3 4
At the beginning how also pass, but own test answer is correct, later refer to https://zmy.im/'s work, in "Add" processing, also sort, passed, poj really difficult to ponder, I thought each output again sort on the line.
1#include <iostream>2#include <iterator>3#include <list>4#include <vector>5 using namespacestd;6list<int> & Findlist (vector<list<int> > & L,intID) {7 intTMP =l.size ();8 if(tmp>0) {9vector<list<int> >:: iterator i;Teni =L.begin (); One return* (i + ID-1); A } - }; - intMain () { the intN; -Cin>>N; -vector<list<int> >A; - for(inti =0; I < n; ++i) { + strings; -CIN >>s; + if(s = ="New") { A intID; atCIN >>ID; -A.push_back (list<int>()); -}Else if(s = ="Add") { - intID, num; -CIN >> ID >>num; -list<int> &temp =Findlist (A, id); in temp.push_back (num); - Temp.sort (); to}Else if(s = ="Merge") { + intid1, Id2; -CIN >> ID1 >>Id2; thelist<int> &TEMP1 =Findlist (A, id1); *list<int> &TEMP2 =Findlist (A, id2); $ Temp1.merge (TEMP2);Panax Notoginseng}Else if(s = ="Unique") { - intID; theCIN >>ID; +list<int> &temp =Findlist (A, id); A Temp.unique (); the}Else if(s = =" out") { + intID; -CIN >>ID; $list<int> &temp =Findlist (A, id); $ Temp.sort (); - if(Temp.size () >0) { -list<int>:: iterator i; the for(I=temp.begin (); I! = Temp.end (); + +i) { -cout << *i <<" ";Wuyi } the } -cout <<Endl; Wu } - } About return 0; $}
POJ C + + programming problem List