题意:先输入若干图书的标题和作者,然后按照先按作者名字的字典序排,然后按照书的字典序来排。然后会有三种操作borrow 表示借书 return表示还书,shelve表示让你输出怎么放书(还是按照原来的顺序)
Sample Input
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END
Sample Output
Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END
然后学到了
string s;
s.find(“by”);//表示返回b下标;;如果没有找到的话,返回的是string::npos
然后是set里的删除操作
set t;
set :: iterator it;
it= t.find(s);
t.erase(it);
然后一定不要忘了 放在first的情况
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
using namespace std;
struct node
{
string book;
string au;
node (string a,string b)
{
book=a;
au=b;
}
};
vector <node> v;//放作者和书名
struct lala
{
int num;
string book;
};
lala la[100005];//放还书的书在原来的序号和书名
bool cmp(node a,node b)
{
if(a.au!=b.au)
return a.au<b.au;
else return a.book<b.book;
}
bool cmp1(lala a2,lala b2)
{
return a2.num<b2.num;
}
int main()
{
// freopen("E:\\input.txt","r",stdin);
string str;
while(getline(cin,str))
{
if(str=="END")//不能用strcmp
break;
int k=str.find("by");
string book=str.substr(0,k-1);
string au=str.substr(k+3);
v.push_back((node){book,au});
}
sort(v.begin(),v.end(),cmp);
int h=0;
set< string > br1;
set<string> :: iterator it;
string str1;
while(getline(cin,str1))
{
if(str1=="END")
break;
string book;
if(str1.length()>6)
book=str1.substr(7);
if(str1[0]=='B')
{
br1.insert(book);
}
else if(str1[0]=='R')
{
lala temp;
for(int i=0;i<v.size();i++)
{
node t=v[i];
if(t.book==book)
{
temp.book=book;
temp.num=i;
break;
}
}
la[h++]=temp;
// cout<<h<<endl;
}
else if(str1[0]=='S')
{
sort(la,la+h,cmp1);
for(int i=0;i<h;i++)
{
int j;
if(la[i].num==0) cout<<"Put "<<la[i].book<<" first\n";
else{
for(j=la[i].num;j>=0;j--)
{
if(j<0) break;
if(br1.count(v[j].book)==0)
{
cout<<"Put "<<la[i].book<<" after ";
cout<<v[j].book<<endl;
break;
}
}
if(j<0) cout<<"Put "<<la[i].book<<" first\n";//这个要注意
}
it=br1.find(la[i].book);
br1.erase(it);
}
cout<<"END\n";
// memset(la,0,sizeof(la));
h=0;
}
}
return 0;
}