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

来源:互联网 发布:程序员软件工程师招聘 编辑:程序博客网 时间:2024/06/03 15:15

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:
31111111The Testing BookYue Chentest code debug sort keywordsZUCS Print20113333333Another Testing BookYue Chentest code sort keywordsZUCS Print220122222222The Testing BookCYLLkeywords debug bookZUCS Print2201161: The Testing Book2: Yue Chen3: keywords4: ZUCS Print5: 20113: blablabla
Sample Output:
1: The Testing Book111111122222222: Yue Chen111111133333333: keywords1111111222222233333334: ZUCS Print11111115: 2011111111122222223: blablablaNot 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;}


0 0
原创粉丝点击