People love A-B
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/others) total submission (s): 43070 accepted submission (s): 12081
The person who participated in the Problem description last month's competition must remember one of the simplest questions: {A} + {B}. The question is the union of the two sets, today, our A-B calculates the difference between the two sets, that is, the subtraction operation of the Set. (Of course, we all know the definition of a set, that is, there will not be two identical elements in the same set. Here, I would like to remind you)
It's easy, right?
Each input group occupies one row. Each row starts with two integers, n (0 <= n <= 100) and M (0 <= m <= 100 ), indicates the number of elements of set a and Set B respectively, followed by N + M elements. The first n elements belong to set a, and the rest belong to Set B. each element is an integer not greater than the int value range. Each element is separated by a space. if n = 0 and m = 0, the input ends without processing.
Output outputs a line of data for each group of data, indicating the results of the A-B, if the results are empty set, the output "null", otherwise the output results from small to large, to simplify the problem, each element is followed by a space (PIT ).
Sample input3 3 1 2 3 1 4 73 7 2 5 8 2 3 4 5 6 7 8 0
Sample output2 3 Null
Authorlcy
1 #include <map> 2 #include <iostream> 3 4 using namespace std; 5 6 7 int main() 8 { 9 10 map<int,bool> setMapA;11 int n,m,num;12 while(cin >> n >> m)13 {14 if(m == 0 && n == 0)15 {16 break;17 }18 setMapA.clear();19 for(int i = 0; i< n;i++)20 {21 cin >> num;22 setMapA.insert(pair<int,bool>(num,true));23 }24 25 for(int i = 0; i< m;i++)26 {27 cin >> num;28 setMapA[num] = false;29 }30 31 for(map<int,bool>::iterator cmp = setMapA.begin(); cmp != setMapA.end();)32 {33 if(!(cmp->second))34 {35 setMapA.erase(cmp++);36 }else37 {38 ++cmp;39 }40 }41 42 if(setMapA.size() == 0)43 {44 cout<<"NULL\n";45 }else46 {47 int i = 0;48 for(map<int,bool>::iterator cmp = setMapA.begin(); cmp != setMapA.end();cmp++)49 {50 if(i != (int)setMapA.size() -1)51 {52 cout << cmp->first << " ";53 }else54 {55 cout << cmp->first << " \n";56 }57 i++;58 }59 60 }61 62 63 }64 }View code
When using the erase function in map, pay attention to setmapa. Erase (CMP ++); in this way, the information of the nodes indicated by CMP is deleted first, and then the CMP node is directed forward;
If setmapa. Erase (++ CMP); deletes the information of the next node, the link is broken, causing a bug