PAT 1022. Digital Library (30)【經典字串處理,我的漏洞百出,可算AC。。】

NO IMAGE

1022. Digital Library (30)

時間限制
1000 ms
記憶體限制
32000 kB
程式碼長度限制
16000 B
判題程式
Standard
作者
CHEN, Yue

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 (<=10000) 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 (<=1000) 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

提交程式碼

/*  
要使用系統庫中的 子串比較函式strstr(str1,str2):判斷2是否1的子串,若是,返回第一個相等處的指標,若否,返回0。 
自己寫子串比較函式老超時,該測試點5分,用自己的會扣掉5分。。。    
若題目指明某個欄位具體是多少位,而不是最大多少位,那麼輸出一定要注意格式----【測試點至少5分】 
*/	
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=10001; //卡超時。。。。  
int ans[1000],n;  //全部變數可在函式中修改。看清ans[]的範圍,卡超時---------------------【測試點3分】  
char cut[90];
struct E{
int id;
char title[81];
char author[81];
char keywd[55];
char pub[81];
char year[5]; //4位數的數字,為了統一,不妨用字串存,省事!!!!!!!!!!!! 
}book[MAX];
char* subStr(char str1[],int i,int j)//對一個字串進行擷取:從i開始擷取,到j-1截止。
{
int k=0,cnt1=0;
for(k=0;k<90;k  )  //初始化全域性變數
cut[k]=0;
for(k=i;k<j;k  )
cut[cnt1  ]=str1[k];
return cut;
}
/*
bool strLook(char str3[],char str4[]){ //判斷 後一個串 是 前一個串 的子串。可以直接用庫函式 strstr(str3,str4); 
bool isSubStr=0;
int len3=strlen(str3),len4=strlen(str4);
int i=0,j=0;
for(i=0;i<len3;i  ){
for(j=0;j<len4;j  ){
if(str3[i j]!=str4[j])
break;
if(j==len4-1)//迴圈體裡面怎麼可能會有len4呢??!!只可能最大len4-1 !!!!!!!!!!!!
isSubStr=1;
}
if(isSubStr==1)
break;
}
return isSubStr;
}
*/
void queryLib(char str[])
{
int i,size=0,len=strlen(str);
bool isFound=0;
if(str[0]=='1'){
for(i=0;i<n;i  ){
if(strcmp(subStr(str,3,len),book[i].title)==0){
ans[size  ]=book[i].id;
isFound=1;
}
}
}
if(str[0]=='2'){  //此處從第四個字元開始,必須去掉1. 點後的空格,--------------【不然0分】
for(i=0;i<n;i  ){
if(strcmp(subStr(str,3,len),book[i].author)==0){
ans[size  ]=book[i].id;
isFound=1;
}
}
}
if(str[0]=='3'){  //此處從第四個字元開始,必須去掉1. 點後的空格,--------------【不然0分】
char str2[90];
strcpy(str2,subStr(str,3,len));
for(i=0;i<n;i  ){      //下面一行直接用庫函式 strstr(s1,s2);----------------【測試點5分】  
if(strstr(book[i].keywd,str2)){
ans[size  ]=book[i].id;
isFound=1;
}
}
}
if(str[0]=='4'){ 
for(i=0;i<n;i  ){
if(strcmp(subStr(str,3,len),book[i].pub)==0){
ans[size  ]=book[i].id;
isFound=1;
}
}
}
if(str[0]=='5'){
for(i=0;i<n;i  ){
if(strcmp(subStr(str,3,len),book[i].year)==0){
ans[size  ]=book[i].id;
isFound=1;
}
}
}
printf("%s\n",str);
if(isFound==0)
printf("Not Found\n");
else{
sort(ans,ans size);	
for(i=0;i<size;i  )
printf("%07d\n",ans[i]);// 輸出格式控制若用%d,扣掉8分---------------【測試點8分】 
}
}
int main()
{
int i,m;
char tmp[81];
//freopen("G:\\in.txt","r",stdin);
//freopen("G:\\our.txt","w",stdout);
scanf("%d",&n);
for(i=0;i<n;i  ){  //該迴圈存入所有書的資訊
scanf("%d",&book[i].id);
getchar();//輸入數字後,要用gets(a)輸入字串,必須用getchar() ------【該點意味著0分與30分】
gets(book[i].title);//輸入數字後,用scanf("%s",a)輸入字串,不需要用getchar()。
gets(book[i].author);
gets(book[i].keywd);
gets(book[i].pub);
gets(book[i].year);
}
scanf("%d",&m);
getchar(); //輸入一個整數後,必加個getchar()才能用輸入串函式gets(a);用scanf("%s",a)不需要用getchar()..
while(m--){
gets(tmp);//連續用gets(a)輸入串,不需之間加getchar()只有輸入數字,再用gets(a)輸入串才需之間加getchar()
queryLib(tmp);
}
return 0;
}