1022 Digital Library
题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336
解题思路
这题最大的麻烦就是对于数据的读入处理;
总结
https://blog.csdn.net/SYaoJun/article/details/87351227
(可以参考参考)
cin
遇到空格/回车终止,适合读取不含有空格的一串字符;
getchar()
读取一个字符,包括空格/回车;可以用来取出换行符;
getline(cin,s)//只适用于string类
读取一串字符,可以包含空格,遇“回车”终止,且自动舍弃“回车”;
gets(m)//m是 char[]类型
读取一串字符,可以包含空格,遇“回车”终止,且自动舍弃“回车”;
cin.getline(m,6)
读取一串字符,可以包含空格,遇“回车”终止,且自动舍弃“回车”;
但是长度是有限制的,只能读取 "6"位,且最后一位是'\0';
代码展示
#include<bits/stdc++.h>
using namespace std;
/*
It is assumed that each book belongs to one author only
and contains no more than 5 key words;
*/
struct book{
string str[10];
};
book lib[10055];
bool cmp(book a,book b){
return a.str[1]<b.str[1];
}
int main(){
int n;
cin>>n;getchar();
for(int i=0;i<n;i++){
for(int j=1;j<=6;j++){
string s;
getline(cin,s);
lib[i].str[j]=s;
}
}
sort(lib,lib+n,cmp);
int m;
cin>>m;
while(m--){
int f;string s;
scanf("%d: ",&f);
getline(cin,s);
cout<<f<<": "<<s<<endl;
bool flag=true;
for(int i=0;i<n;i++){
if(lib[i].str[f+1]==s||(f==3&&lib[i].str[f+1].find(s)!=string::npos)){
cout<<lib[i].str[1]<<endl;
flag=false;
}
}
if(flag)cout<<"Not Found"<<endl;
}
return 0;
}