UVA 230 - Borrowers

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

typedef struct book{
	string name;
	string author;
	bool flag;
}book;

bool compare(book a,book b){
	if (a.author != b.author) return a.author < b.author;
	return a.name < b.name;
}

int main(){
	string s;
	vector shv;
	while (getline(cin, s)){
		if (s == "END") break;
		book temp;
		int index1 = s.find_last_of("\"");
		temp.name = s.substr(0,index1+1);
		temp.author = s.substr(index1+5);
		temp.flag = true;
		shv.push_back(temp);
	}
	sort(shv.begin(),shv.end(),compare);
	map str2int;
	for (int i = 0; i < shv.size(); i++){
		string name = shv[i].name;
		str2int[name] = i;
	}
	vector result;
	vector re_book;
	while (getline(cin, s)){
		if (s == "END") break;
		else if (s == "SHELVE"){
			sort(re_book.begin(),re_book.end(),compare);
			for (int i = 0; i < re_book.size(); i++){
				string name = re_book[i].name;
				int pos = str2int[name];
				shv[pos].flag = true;
				int cur_pos;
				for (cur_pos = pos - 1; cur_pos >= 0; cur_pos--){
					if (shv[cur_pos].flag) break;
				}
				string res;
				if (cur_pos >= 0){
					res = "Put " + name + " after " + shv[cur_pos].name;
				}
				else{
					res = "Put " + name + " first";
				}
				result.push_back(res);
			}
			for (int i = 0; i < result.size(); i++) cout << result[i] << endl;
			cout << "END" << endl;
			re_book.clear();
			result.clear();
		}
		else{
			int index1;
			index1 = s.find_first_of("\"");
			string op = s.substr(0,index1-1);
			string name = s.substr(index1);
			int pos = str2int[name];
			if (op == "RETURN"){
				book temp;
				temp.name = name;
				temp.author = shv[pos].author;
				re_book.push_back(temp);
			}
			else if (op=="BORROW"){
				shv[pos].flag = false;
			}
		}
	}
	//system("pause");
	return 0;
}

你可能感兴趣的:(Uva,oj)