UVA传送门
思路:参照紫书的思路,根据题意模拟即可。步骤:1、输入完所有书目后根据作者标题排序 2、统计借出的书目3、统计当前归还的书目3、输入SHELVE后将归还的书目放回书架,注意:当前书目前面的书都被借走了此时将当前书看做是第一本书,类似的放书的时候要看书目在书架的存在的前一本。
此题思路不难想不过细节上需要处理好和需要理解题意,我的思路利用了STL里面的map,这样在查找每一本书的位置提供了便利
AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
map<string,int>check;
struct temp{
string title;
string name;
}a[100000],cur[100000];
int num[100000];
bool cmp(const struct temp &s1,const struct temp &s2){ //根据作者标题排序函数
if(s1.name!=s2.name)
return s1.name<s2.name;
return s1.title<s2.title;
}
int main()
{
//freopen("text.txt","r",stdin);
int n = 0;
string s;
while(getline(cin,s) && s!="END"){
string x,y;
int i;
for(i = 1;i<s.size();i++){
if(s[i]=='"'){
a[n].title = x;
break;
}
x+=s[i];
}
//cout<<"hhh"<<endl;
for(i = i+5;i<s.size()-1;i++)
y+=s[i];
a[n++].name = y;
}
sort(a,a+n,cmp);
for(int i = 0;i<n;i++) if(check.count(a[i].title)==0) check[a[i].title] = i; //为每一本书的标题映射一个位置
int cnt = 0;
while(getline(cin,s) && s!="END"){
if(s[0]=='R'){
string x;
for(int i = 8;i<s.size()-1;i++)
x+=s[i];
cur[cnt++]= a[check[x]];
num[check[x]] = 0; //表示当前书归还了
}
else if(s[0]=='S'){
int pp;
bool flag = true;
sort(cur,cur+cnt,cmp);
for(int i = 0;i<cnt;i++){
int p = check[cur[i].title];
for(int i = p-1;i>=0;i--) //看在书架上的前一本书是什么,如果没有则放在首位
if(num[i]!=-1){
pp = i;
flag = false;
break;
}
if(flag)
cout<<"Put \"" <<a[p].title<< "\" first" <<endl;
else
cout<<"Put \""<<a[p].title<<"\" after \""<<a[pp].title<<"\""<<endl;
}
cout<<"END"<<endl;
cnt = 0;
}
else if(s[0]=='B'){
string x;
for(int i = 8;i<s.size()-1;i++)
x+=s[i];
num[check[x]] = -1; //书被借走了,每本书只有一本
}
}
return 0;
}
uDebug的两组测试数据(侵删)
第一组:
输入
"The Canterbury Tales" by Chaucer, G.
"The Canterbury Taless" by Chaucer, B.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchiee, D.
"The C Programming Languag" by Kernighan, B. and Ritchiee, D.
"The D Programming Language" by Kernighan, B. and Ritchiee, D.
"A House for Mr. Biswas" by Naipaul, V.S.
"A Congo Diary" by Naipaul, V.S.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
BORROW "The C Programming Languag"
BORROW "The Canterbury Taless"
SHELVE
RETURN "Algorithms"
SHELVE
RETURN "The C Programming Languag"
SHELVE
BORROW "The C Programming Languag"
BORROW "The Canterbury Taless"
BORROW "A House for Mr. Biswas"
RETURN "The Canterbury Taless"
SHELVE
RETURN "The C Programming Language"
RETURN "A House for Mr. Biswas"
SHELVE
END
输出
END
Put "Algorithms" after "A House for Mr. Biswas"
END
Put "The C Programming Languag" after "The Canterbury Tales"
END
Put "The Canterbury Taless" first
END
Put "The C Programming Language" after "The Canterbury Tales"
Put "A House for Mr. Biswas" after "A Congo Diary"
END
第二组
输入
"a" by b.
"b" by c.
END
BORROW "a"
BORROW "b"
RETURN "b"
SHELVE
END
输出
Put "b" first
END
如果这两组数据加样例都过了话基本就没什么问题了O(∩_∩)O~~