3339:list
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
Enter the first row, a number n, to indicate how many commands (n<=200000). One command per line after n rows. The output is output according to the topic requirement. 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
Learn more about STL's invincibility. You're not hitting tle or re in your own hands.
Full out Code
#include <bits/stdc++.h>using namespacestd;intn,m,k,l,s,t,r;list<int>a[200005];stringp;intMain () {//freopen ("1.txt", "R", stdin); inti,j; Ios::sync_with_stdio (false); CIN>>N; for(j=1; j<=n;j++) {cin>>p; if(p=="New") cin>>l; Else if(p=="Add") {cin>>l>>R; A[l].push_back (R); } Else if(p==" out") {cin>>l; if(A[l].empty ()) cout<<Endl; Else{a[l].sort (); for(list<int>:: Iterator I=a[l].begin (); I!=a[l].end (); i++) cout<<*i<<" "; cout<<Endl; } } Else if(p=="Merge") {cin>>l>>R; A[l].merge (A[r]); } Else if(p=="Unique") {cin>>l; A[l].sort (); A[l].unique (); } } return 0;}
Click to expand
Handwritten code (0 points for reference only)
#include <vector>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;structnode{inta[20000]; intW;} e[10000];stringA;vector<int>Q;BOOLs[200001],d[200001];intf,b[200001],n,i,id,j,r,z,y;intMain () {CIN>>N; while(n--) {cin>>A; if(a=="New") {cin>>R; D[R]=1; } if(a=="Add") {cin>>z>>y; E[Z].A[E[Z].W++]=y; } if(a==" out") {cin>>R; if(d[r]==1) { for(i=0; i<e[r].w;++i)if(e[r].a[i]!=0) b[i]=E[r].a[i]; Sort (b,b+E[R].W); for(i=0; i<e[r].w;++i) cout<<b[i]<<" "; } cout<<Endl; } if(a=="Merge") {cin>>z>>y; for(i=0; i<e[y].w;++i) E[Z].A[E[Z].W++]=E[y].a[i]; D[y]=0; } if(a=="Unique") {memset (s),0,sizeof(s)); CIN>>R; for(i=0; i<e[r].w;++i)if(s[e[r].a[i]]==0) {q.push_back (e[r].a[i]); S[e[r].a[i]]=1; } E[R].W=0; while(!Q.empty ()) {E[R].A[E[R].W++]=Q.back (); Q.pop_back (); } } }}
Click to expand
Popularize knowledge.
Basic operation and use of C + + list
Lists elements are stored sequentially in the linked list. It allows for fast insertions and deletions compared to vectors (vectors), but random access is slower.
Assign () assigns a value to the list
Back () returns the last element
Begin () returns the iterator that points to the first element
Clear () Delete all elements
Empty () Returns True if list is empty
End () returns the end of the iterator
Erase () Delete an element
Front () returns the first element
Get_allocator () returns the Configurator for list
Insert () Inserts an element into the list
Max_size () returns the maximum number of elements the list can hold
Merge () merges two lists
Pop_back () Delete last element
Pop_front () Delete the first element
Push_back () Adds an element at the end of the list
Push_front () Adds an element to the head of the list
Rbegin () returns a reverse iterator pointing to the first element
Remove () removes an element from the list
remove_if () Delete elements by specified criteria
Rend () reverse iterator to the end of list
Resize () Change the size of the list
Reverse () Reverses the elements of the list
Size () returns the number of elements in the list
Sort () Sorts the list
Splice () Merge two lists
Swap () swap two list
Unique () Delete duplicate elements in list
This problem is a template problem can be done. View
The road of spiritual practice ~ The 3.9-3339 of the European spraying profession