模拟 回顾一下这道题的过程,刚开始题意理解错了。后来看别人的题解发现自己做的太复杂了。我感觉最简单最好像的思路就是把图书的信息存在结构体数组里面,然后对每本书按照要求排序。并且用标记数组记录哪本书被借走了。 再用一个结构体数组存还书的信息。最后枚举还书的数组,按要求放入书架即可。 其中最坑的就是,不知道是我没理解题意还是他表述有问题。Put title first 这句我以为的是如果还的书是书架上的第一本书就用这条语句。后来发现根本不是这样,它的意思就是,还的第一本书可能不是书架上的第一本,但是它在书架前面的书一定都被借走了 还没还回来。
AC代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
struct node
{
string book;
string name;
};
struct rule
{
bool operator()(const node &a1,const node &a2)
{
return a1.name<a2.name||(a1.name==a2.name&&a1.book<a2.book);
}
};
void process1(string s,string &book,string &name)
{
int k=s.find("by");
book=s.substr(0,k-1);
name=s.substr(k+3);
}
void process2(string s,string &c,string &d)
{
int k=s.find(" ");
c=s.substr(0,k);
d=s.substr(k+1);
}
vector<node> v1;
vector<node>::iterator p;
bool vis[10010];//判断是否被借走
int main()
{
ios::sync_with_stdio(false);
string s,a,b;
node m;
node m1[10010];//书单
node m2[10010];//还书的列表
while(getline(cin,s)&&s!="END")
{
process1(s,a,b);
m.book=a;
m.name=b;
// cout<<a<<b<<endl;
v1.push_back(m);
}
int len1=v1.size();
sort(v1.begin(),v1.end(),rule());
for(int i=0;i<len1;i++)
{
m1[i]=v1[i];
vis[i]=true;
}
int number=0;
while(getline(cin,s)&&s!="END")
{
process2(s,a,b);
// cout<<a<<b<<endl;
if(a=="BORROW")
{
for(int i=0;i<len1;i++)
{
if(m1[i].book==b)
{vis[i]=false;break;}
}
}
if(a=="RETURN")
{
for(int i=0;i<len1;i++)
{
if(m1[i].book==b)
{
m.book=b;
m.name=m1[i].name;
m2[number]=m;
number++;
break;
}
}
}
if(a=="SHELVE")
{
sort(m2,m2+number,rule());
int a=0;
string pro;
for(int i=0;i<number;i++)
{
string book=m2[i].book;
for(int j=0;j<len1;j++)
{
// if(m2[i].book==m1[j].book&&vis[j]==false)
// {
// vis[j]=true;
// if(j==0)
// cout<<"Put "<<m2[i].book<<" first"<<endl;
// else
// cout<<"Put "<<m2[i].book<<" after "<<m1[j-1].book<<endl;
// break;
// }
if(vis[j])
{
pro=m1[j].book;
a++;
}
if(m1[j].book==book)
{
vis[j]=true;
if(a!=0)
cout<<"Put "<<book<<" after "<<pro<<endl;
else
cout<<"Put "<<book<<" first"<<endl;
break;
}
}
}
cout<<"END"<<endl;
number=0;
}
}
return 0;
}
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
string book;
string name;
};
struct rule
{
bool operator()(const node &a1,const node & a2)
{
return a1.name<a2.name||(a1.name==a2.name&&a1.book<a2.book);
}
};
struct node2
{
string s;
int no;
};
struct rule2
{
bool operator()(const node2 &a1,const node2 &a2)
{
return a1.no<a2.no;
}
};
string s,a,b;
node m;
vector<node> v1;
//vector<string> v2;
vector<node>::iterator p;
vector<string> v3;
map<string,int> mp;
int ll;
bool vis[100010];
void process1(string s,string &book,string &name)
{
int k=s.find("by");
book=s.substr(0,k-1);
name=s.substr(k+3);
}
void process2(string s,string &c,string &d)
{
int k=s.find(" ");
c=s.substr(0,k);
d=s.substr(k+1);
}
void solve()
{
// cout<<"len="<<len<<endl;
// if(len==0) {cout<<"END"<<endl;return;}
// else
// {
int len3=v3.size();
int len1=v1.size();
node2 d[len3];
for(int i=0;i<len3;i++)
{
d[i].s=v3[i];
d[i].no=mp[v3[i]];
}
sort(d,d+len3,rule2());
// int ll=v2.size();
// for(int i=0;i<ll;i++)
// {
// cout<<v2[i]<<endl;
// cout<<"---------"<<endl;
// }
for(int i=0;i<len3;i++)
{
string book=d[i].s;
string pro;
int a=0;
for(int j=0;j<len1;j++)
{
// if(vis[j]==false&&book==v1[j].book)
// {
// vis[j]=true;
// if(j==0)
// cout<<"Put "<<book<<" first"<<endl;
// else
// cout<<"Put "<<book<<" after "<<v1[j-1].book<<endl;
// break;
// }
if(vis[j])
{
pro=v1[j].book;
a++;
}
if(book==v1[j].book)
{
vis[j]=true;
if(a!=0)
cout<<"Put "<<book<<" after "<<pro<<endl;
else
cout<<"Put "<<book<<" first"<<endl;
}
}
// if(v2.empty())
// {
// cout<<"Put "<<d[i].s<<" first "<<endl;
// v2.push_back(d[i].s);
// }
// else
// {
// cout<<"Put "<<d[i].s<<" after "<<*(v2.end()-1)<<endl;
// v2.push_back(d[i].s);
// }
}
cout<<"END"<<endl;
v3.clear();
return;
}
int main()
{
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
while(getline(cin,s)&&s!="END")
{
process1(s,a,b);
m.book=a;
m.name=b;
v1.push_back(m);
// v2.push_back(m.book);
}
sort(v1.begin(),v1.end(),rule());
ll=v1.size();
for(int i=0;i<ll;i++)
vis[i]=true;
for(int i=0;i<ll;i++)
{
mp[v1[i].book]=i;
}
string c,d;
// string t;
// getline(cin,t);
while(getline(cin,s)&&s!="END")
{
process2(s,c,d);
// cout<<"111111"<<c<<endl;
// cout<<"000000"<<d<<endl;
if(c=="BORROW")
{
// for(p=v2.begin();p!=v2.end();p++)
// {
// if(*p==d)
// {v2.erase(p);break;}
// }
for(p=v1.begin();p!=v1.end();p++)
{
if((*p).book==d)
{
vis[p-v1.begin()]=false;
break;
}
}
}
if(c=="RETURN")
v3.push_back(d);
if(c=="SHELVE")
{
///调用函数 函数中要记得清空v3
solve();
}
}
return 0;
}