我在刷uva230遇到的问题,代码在本地测试都没问题,但是一提交就是WrongAnswer,不知道问题具体出在哪,求大神指点迷津。不要网上的代码,希望大神能帮忙找出我代码的错误。
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<iterator>
using namespace std;
struct bookList {
string bookName;
string author;
};
bool cmp(bookList l, bookList r) {
if (l.author == r.author)return l.bookName < r.bookName;
else return l.author < r.author;
}
string bookName;
string author;
string temp;
bookList L1[10000];
map<string, int> bookNum;
vector<int> bookShelves;
int n = 0;
void T5_8() {
while (cin >> temp) {
bookName.clear();
author.clear();
if (temp == "END")break;
bookName += temp;
while (cin >> temp&&temp != "by") {
bookName += " ";
bookName += temp;
}
getline(cin, author);
L1[n].bookName = bookName;
L1[n].author = author;
n++;
}
//排序
sort(L1, L1 + n, cmp);
//赋予编号,并把图书上架
for (int i = 0; i < n; i++) {
bookNum[L1[i].bookName] = i;
bookShelves.push_back(i);
}
vector<int> books;
//处理输入;
while (cin >> temp) {
if (temp[0] == 'B') {
getchar();//吃掉空格
getline(cin, bookName);
vector<int>::iterator it = find(bookShelves.begin(), bookShelves.end(), bookNum[bookName]);
bookShelves.erase(it);
}
else if (temp[0] == 'R') {
getchar();//吃掉空格
getline(cin, bookName);
books.push_back(bookNum[bookName]);
}
else if (temp[0] == 'S') {
sort(books.begin(), books.end());//先排序
while (!books.empty()) {
int bookNumber = books.front();
vector<int>::iterator it = books.begin();
books.erase(it);
if (bookShelves.empty()) {
bookShelves.push_back(bookNumber);
cout << "Put ";
cout << L1[bookNumber].bookName;
cout << " first\n";
}
else {
int toFind = bookNumber;//把这个插入到num比他小的最大的数的后面
int insertMax = -1;
for (int i = 0; i < bookShelves.size(); i++) {
if (bookShelves[i] < toFind) {
if (insertMax<bookShelves[i])
insertMax = bookShelves[i];
}
else break;
}
if (insertMax == -1) {//放在最前面
cout << "Put ";
cout << L1[bookNumber].bookName;
cout << " first\n";
}
else {
cout << "Put ";
cout << L1[toFind].bookName;
cout << " after ";
cout << L1[insertMax].bookName;
cout << "\n";
//cout << "Put " << L1[toFind].bookName << " after " << L1[insertWho].bookName << endl;
}
bookShelves.insert(find(bookShelves.begin(), bookShelves.end(), insertMax), toFind);
}
}
cout << "END" << endl;
}
else { break; }
}
}
int main() {
T5_8();
}