题意:
输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE。
BORROW 和 RETURN 都会带有一个书名在后面,如:
BORROW "The Canterbury Tales"
RETURN "The Canterbury Tales"
当遇到SHELVE指令,输出已还但没上架的书排序后(规则同上)依次插入书架的序列。
用例:
输入:
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "The Canterbury Tales"
RETURN "The Canterbury Tales"
BORROW "The C Programming Language"
RETURN "The C Programming Language"
SHELVE
BORROW "Algorithms"
RETURN "Algorithms"
SHELVE
END
输出:
Put "The Canterbury Tales" first
Put "The C Programming Language" after "The Canterbury Tales"
END
Put "Algorithms" after "The C Programming Language"
END
分析:
这题的读入有点麻烦,我的处理是使用getline, 然后将 “ 号变成空格 将空格变成“#”(ASCII差距不大), 再用stringsteam输入到string中,最后输出再将“#”转换为“ ”。
网上的方法是使用string.substr()截取,用string.find()去寻找位置, 我截取这种方法更好(stringsteam很慢)。
用map<string,int>将string映射到书架位置上面,用一个bool去标记这个位置有没被拿走,注意return后不能直接把书放回去,要标记,等收到SHELVE指令再排序放书。
1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <map> 5 #include <string> 6 #include <iostream> 7 #include <set> 8 #include <sstream> 9 #include <algorithm> 10 using namespace std; 11 struct B 12 { 13 string name; 14 string author; 15 B(){} 16 B(const string& _name,const string& _author): 17 name(_name),author(_author){} 18 friend bool operator < (const B a,const B b) 19 { 20 if(a.author < b.author) return true; 21 else 22 { 23 if(a.author == b.author) 24 { 25 if(a.name < b.name) 26 return true; 27 else return false; 28 } 29 else return false; 30 } 31 } 32 }; 33 struct A 34 { 35 int a; 36 int b; 37 A(){} 38 A(int _a, int _b): 39 a(_a), b(_b){} 40 // a after b of a is the first(b=-1); 41 }; 42 typedef set<B> :: iterator sIT; 43 typedef vector<B> :: iterator vIT; 44 typedef vector<A> :: iterator aIT; 45 typedef vector<int> :: iterator IT; 46 string trans(const string& a) 47 { 48 string t = a; 49 for(int i = 0; i < t.size(); i++) 50 { 51 if(t[i] == '#') t[i] = ' '; 52 } 53 return t; 54 } 55 int main() 56 { 57 ios::sync_with_stdio(false); 58 #if LOCAL 59 freopen("1.txt","r",stdin); 60 freopen("2.txt","w",stdout); 61 #endif // LOCAL 62 string s; 63 vector<B> book; 64 bool lend[1005]; 65 vector<A> ans; 66 fill(lend, lend+1005,1); 67 map<string,int> id; 68 vector<int> ret; 69 while(getline(cin,s)) 70 { 71 for(int i = 0; i < s.size(); i++) 72 { 73 if(s[i] == '"') s[i] = ' '; 74 else if(s[i] == ' ') s[i] = '#'; 75 } 76 stringstream ss(s); 77 string tname, tauthor; 78 if(!(ss>>tname>>tauthor)) break; 79 book.push_back(B(tname,tauthor)); 80 } 81 sort(book.begin(), book.end()); 82 int cnt = 0; 83 for(vIT it = book.begin(); it != book.end(); it++) 84 { 85 id[(*it).name] = cnt++; 86 } 87 string op; 88 while(cin>>op) 89 { 90 if(op=="END") break; 91 if(op == "SHELVE") 92 { 93 sort(ret.begin(), ret.end()); 94 for(IT it = ret.begin(); it != ret.end(); it++) 95 { 96 lend[*it] = 1; 97 int c = (*it) -1; 98 for(; c >= 0 && lend[c] == 0; c--); 99 ans.push_back(A(*it,c)); 100 } 101 ret.clear(); 102 for(aIT it = ans.begin(); it != ans.end(); it++) 103 { 104 if((*it).b != -1) 105 { 106 cout<<"Put ""<< trans(book[(*it).a].name) << "" after "" << trans(book[(*it).b].name) << "" "; 107 108 } 109 else 110 { 111 cout<<"Put ""<<trans(book[(*it).a].name)<< "" first"<<" "; 112 113 } 114 } 115 ans.clear(); 116 cout<<"END "; 117 continue; 118 } 119 cin.ignore(); 120 string b; 121 getline(cin,b); 122 for(int i = 0; i < b.size(); i++) 123 { 124 if(b[i] == '"') b[i] = ' '; 125 else if(b[i] == ' ') b[i] = '#'; 126 } 127 stringstream ss(b); 128 ss>>b; 129 if(op== "BORROW") 130 { 131 lend[id[b]] = 0; 132 } 133 else if(op == "RETURN") 134 { 135 136 ret.push_back(id[b]); 137 } 138 } 139 140 141 return 0; 142 }