The two methods are implemented with two vectors, one for the adopted animal (output), one for the adopted animal (input), one for the adoption of the two queue (both input), an adopted cat, an adopted dog, vetor (output).
/*\file catdogasylum.cpp* \date 2016/05/09 15:19* \ Description of the problem: the title describes a family animal shelter that only houses cats and dogs, but there are special adoption rules, the adoption of two types of adoption, the first for direct adoption of all animals in the first asylum, the second is to choose the type of animal (cat or dog), and the first to adopt the animal into the shelter. Given an action sequence int[][2] Ope (vector<vector<int>> in C + +) represents all events. If the first element is 1, it means that there is an animal into the shelter, the second element is the animal's number, the positive numbers represent the dog, the negative numbers represent the cat, and if the first element is 2, then the second element, if 0, adopts the first adoption method, if 1, the adoption dog is designated as the adoption cat. Please return the adopted sequence in order. In the event of an illegal operation, that is, there are no animals that can meet the adoption requirements, the adoption operation is ignored. Test sample: [[[1,1],[1,-1],[2,0],[2,-1]] return: [1,-1]* \ Problem Analysis: Idea: Define two Vector<int> objects A and B, For the animals in the shelter and the adopted animals are operated as follows: for the ope array from i = 0 to Ope.size ()-1 traversal 1, instruction 1 (ope[i][0] = 1), when an animal comes in, the animal serial number is pressed into the a--execution A.push_back (ope[ I][1]); 2, the instruction is 2 (ope[i][0] = 2), when an animal is adopted, the first to determine whether a is empty, that is, whether there is an animal 1) if a is empty, then continue2) if a is not empty 1. If the operation is ope[i][1] = 0, the first animal to come in, A[0] is pressed into B, execute B.push_back (a[0]), and then delete the corresponding element in A, that is, the execution of A.erase (A.begin ()); 2. If the operation is ope[i][1] = 1, that is, the adoption of the first dog came in, at this time to traverse A to find the first dog, Then the found element is pressed into B and the corresponding element is removed in A; 3. If the operation is ope[i][1] =-1, which is the first cat to be adopted, walk A to find the first cat, then press the found element into B and then delete the corresponding element in a. After the traversal is complete, return B. *****************************************************/#include<iostream>using namespacestd; #include<vector>classCatdogasylum { Public: Vector<int> Asylum (vector<vector<int> >ope) { //Write code here intLen =ope.size (); Vector<int> ans,inch; if(len==0) { returnans; } for(inti =0; I < len;i++) { if(ope[i][0]==1)//There are animals coming to the shelter { inch. push_back (ope[i][1]); } Else if(ope[i][0]==2)//someone's adopted . { if(inch. Empty ()) { Continue; } if(ope[i][1]==0)//The first form of adoption, take the first one{ans.push_back (inch[0]); inch. Erase (inch. Begin ()); } Else if(ope[i][1]==1)//designated dog-keeping { for(inti =0; I <inch. Size (); i++) { if(inch[i]>0) {Ans.push_back (inch[i]); inch. Erase (inch. Begin () +i); Break; } } }Else if(ope[i][1]==-1)//designation of a pet cat { for(inti =0; I <inch. Size (); i++) { if(inch[i]<0) {Ans.push_back (inch[i]); inch. Erase (inch. Begin () +i); Break; } } } } } returnans; }};////////////////////////////////////////////////////////////////////////////Topic Analysis://according to the principle of FIFO, naturally think of the implementation of the queue, if only one queue, then the first method of implementation is relatively simple, only need to remove the team head of the animals can be,//But the second approach is more complex and requires access to the entire queue to find the first cat or dog to be visited. //so we can choose to maintain two queues to implement, a queue to store put into the dog, a queue to put into the cat, for the second method to achieve quite easy,//We just need to choose the cat or dog from the corresponding queue to remove the can, but the first method needs to determine the two queue head is the cat first into the shelter, or the dog first enter,//this time need a flag, so every time we put the animal into the queue, at the same time an increment of the sequence number in the queue, the sequence number is a time series, according to this sequence number can be easily implemented the first method. //"Programmer Interview Gold"--detailed topic://http://blog.csdn.net/zdplife/article/category/5799903//////////////////////////////////////////////////////////////////////#include <queue>classCatdogasylum { Public: Vector<int> Asylum (vector<vector<int> >ope) { //Write code herequeue<int>Cat; Queue<int>Dog; Vector<int>VEC; intindex =0; intSize1 =ope.size (); for(inti =0; i < size1; i++) { intKind = ope[i][0]; if(Kind = =1)//There are animals coming to the shelter { if(ope[i][1] >=0)//Dog Queue{Dog.push (index++);//Mark who is entering from oneDog.push (ope[i][1]); } Else{Cat.push (index++);//Cat QueueCat.push (ope[i][1]); } } Else //someone's adopted . { if(ope[i][1] ==0)//the first animal to come in. { intMin =0; if(Cat.empty () &&!dog.empty ())//Dog is not emptyMin =1; if(!cat.empty () &&dog.empty ()) min= -1; if(!cat.empty () &&!dog.empty ()) min= Dog.front () > Cat.front ()? -1:1; if(min = =-1)//Adopt a Cat{cat.pop (); Vec.push_back (Cat.front ()); Cat.pop (); } if(min = =1)//Adopt a dog{dog.pop (); Vec.push_back (Dog.front ()); Dog.pop (); } } Else { if(ope[i][1] ==1&&!dog.empty ())//Adopt a dog{dog.pop (); Vec.push_back (Dog.front ()); Dog.pop (); } if(ope[i][1] == -1&&!cat.empty ())//Adopt a Cat{cat.pop (); Vec.push_back (Cat.front ()); Cat.pop (); } } } } returnVEC; }};
Cat and Dog Adoption Institute