PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:
- Line #1: the 7-digit ID number;
- Line #2: the book title -- a string of no more than 80 characters;
- Line #3: the author -- a string of no more than 80 characters;
- Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
- Line #5: the publisher -- a string of no more than 80 characters;
- Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].
It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.
After the book information, there is a line containing a positive integer M (≤) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:
- 1: a book title
- 2: name of an author
- 3: a key word
- 4: name of a publisher
- 5: a 4-digit number representing the year
Output Specification:
For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print Not Found
instead.
Sample Input:
3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla
Sample Output:
1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found
- 题解:
- 题意是一个图书馆查询系统,除了第三个查询是查找子串,其他的问题都是字符串比较,所以就用了循环。
- 1.keywords的读入用到了一个字符串流
- istringstream tmp(str);//<sstream>以空格分开字符串
- a[i].kn=;
- while(tmp>>word)
- {
- a[i].kn++;
- a[i].key[a[i].kn]=word;
- }
- 也可以
- while(cin>>string){
- char c = getchar()
- if(c == ‘\n’) break;
- }
- 2.题目中所说的year在[1000, 3000]之间,但是所给的测试并不一定在这之间,否则会测试点2不能通过。
- AC代码:
- #include<bits/stdc++.h>
- using namespace std;
- struct node{
- int id;
- string ti;
- string au;
- int kn;
- string key[];
- string pu;
- string year;//测试点2坑点,用int反而错了,year不一定全在[1000, 3000]之间
- }a[];
- bool cmp(node x,node y){
- return x.id<y.id;
- }
- int main(){
- int n;
- cin>>n;
- string str,word;
- for(int i=;i<=n;i++){
- cin>>a[i].id;
- getchar();//吸收回车
- getline(cin,a[i].ti);
- getline(cin,a[i].au);
- getline(cin,str);
- istringstream tmp(str);//<sstream>以空格分开字符串
- a[i].kn=;
- while(tmp>>word)
- {
- a[i].kn++;
- a[i].key[a[i].kn]=word;
- }
- getline(cin,a[i].pu);
- getline(cin,a[i].year);
- }
- /*cout<<endl;
- for(int i=1;i<=n;i++){
- cout<<"ID "<<a[i].id<<endl;
- cout<<"TI "<<a[i].ti<<endl;
- cout<<"AU "<<a[i].au<<endl;
- for(int j=1;j<=a[i].kn;j++){
- cout<<a[i].key[j]<<"-";
- }
- cout<<endl;
- cout<<"PU "<<a[i].pu<<endl;
- cout<<"YE "<<a[i].year<<endl;
- }*/
- sort(a+,a++n,cmp);
- int m;
- cin>>m;
- int k;
- char kk;
- string find;
- for(int i=;i<=m;i++){
- cin>>k;
- cin>>kk;//冒号
- getchar();//空格
- getline(cin,find);
- cout<<k<<": "<<find<<endl;
- int f=;
- if(k==){//ti
- for(int j=;j<=n;j++){
- if(a[j].ti==find){
- f=;
- printf("%07d\n", a[j].id);
- }
- }
- }else if(k==){//au
- for(int j=;j<=n;j++){
- if(a[j].au==find){
- f=;
- printf("%07d\n", a[j].id);
- }
- }
- }else if(k==){//kw
- for(int j=;j<=n;j++){
- for(int p=;p<=a[j].kn;p++){
- if(a[j].key[p]==find){
- f=;
- printf("%07d\n", a[j].id);
- break;
- }
- }
- }
- }else if(k==){//pu
- for(int j=;j<=n;j++){
- if(a[j].pu==find){
- f=;
- printf("%07d\n", a[j].id);
- }
- }
- }else if(k==){//year
- for(int j=;j<=n;j++){
- if(a[j].year==find){
- f=;
- printf("%07d\n", a[j].id);
- }
- }
- }
- if(!f){
- cout<<"Not Found"<<endl;
- }
- }
- return ;
- }
PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)的更多相关文章
- pat 甲级 1022. Digital Library (30)
1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...
- PAT Advanced 1022 Digital Library (30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- 1022 Digital Library (30 分)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
- 1022. Digital Library (30) -map -字符串处理
题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key w ...
- PAT-1022 Digital Library (30 分) 字符串处理
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PAT 甲级 1022 Digital Library
https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 A Digital Library cont ...
- 【PAT甲级】1022 Digital Library (30 分)(模拟)
题意: 输入一个正整数N(<=10000),接下来输入N组数据,ID,书名,作者,关键词,出版社,出版年份. 然后输入一个正整数M(<=1000),接下来输入查询的数据,递增输出ID,若没 ...
- PAT A 1022. Digital Library (30)【结构体排序检索】
https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #incl ...
- PAT甲级1022 Digital Library
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 题意: 每一本书有一个id, 书名,作 ...
随机推荐
- 用js刷剑指offer(变态跳台阶)
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 牛客网链接 思路 假设青蛙跳上一个n级的台阶总共有f(n)种跳法. 现在青蛙从第n个台阶 ...
- ]Kinect for Windows SDK开发入门(六):骨骼追踪基础 上
原文来自:http://www.cnblogs.com/yangecnu/archive/2012/04/06/KinectSDK_Skeleton_Tracking_Part1.html Kinec ...
- 【javascript】h5页面禁止返回上一页
window.history.pushState("","","#"); window.addEventListener("pop ...
- python3 基础三
一.if语句 if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3 ...
- 通过字节码分析Java异常处理机制
在上一次[https://www.cnblogs.com/webor2006/p/9691523.html]初步对异常表相关的概念进行了了解,先来回顾一下: 其源代码也贴一下: 下面来看一下jclas ...
- [HNOI2007]分裂游戏 SG打表博弈
结论:其实每一个巧克力都是一堆石子 它的石子数就是它到队尾的距离 打一个SG表即可 #include<bits/stdc++.h> using namespace std; typedef ...
- 基于Flask和百度AI实现与机器人对话
实现对话机器人主要有个步骤 : 一.前端收集语音传入后端 二.后端基于百度AI接口进行语音识别,转换成文字 三.对文字进行自定义验证或通过图灵端口进行处理,生成回复内容 四.将文字通过百度AI接口合成 ...
- Windows服务启动时候报错1053
用.net 开发了一个C#语言的windows服务,在本地和测试环境,安装启动都正常,在新的线上环境报错,不能启动-报出-错误1053:服务没有及时响应启动或控制请求. 后来发现时线上.NET FRA ...
- Vue项目中的文件/文件夹命名规范
Vue项目中的文件/文件夹命名规范 0.2262018.09.21 16:01:09字数 820阅读 6979 文件或文件夹的命名遵循以下原则: index.js 或者 index.vue,统一使用小 ...
- DbVisualizer用JDBC连接SQL Server
1.安装驱动,解压后得到jar文件 Drivers 4.1 and 4.0 for SQL Serve http://www.microsoft.com/en-us/download/confirma ...