首页 > 试题广场 >

Digital Library (30)

[编程题]Digital Library (30)
  • 热度指数:2279 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 640M,其他语言1280M
  • 算法知识视频讲解
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.

输入描述:
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


  • 输出描述:
    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.
    示例1

    输入

    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

    输出

    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
    输入完全不符合要求,题目明确说年份是4位数字,而实际测试数据却是一个字符串
    发表于 2015-10-13 14:04:47 回复(2)
    #include <iostream> 
    #include <map>
    #include <set>
    #include <string>
    using namespace std;
    map<string,set<int>> book[6];    //分别代表5种映射关系(用set可自动排序)
    int main(){
        int n,id,type;
        cin>>n;
        string s;
        for(int i=0;i<n;i++){
            cin>>id;
            char c=getchar();       //接收掉cin后面的换行
            for(int j=1;j<=5;j++){
                if(j!=3){           //除了第三行关键词
                    getline(cin,s);
                    book[j][s].insert(id);
                }
                else{
                    while(cin>>s){  //第三行每次读入一个关键次
                        book[j][s].insert(id);
                        c=getchar();//接收关键词后的字符
                        if(c=='\n') break; //若为换行,则结束关键词输入
                    }
                }
            }
        }
        cin>>n;
        for(int i=1;i<=n;i++){
            scanf("%d: ",&type);    //输入查询类型
            getline(cin,s);
            cout<<type<<": "<<s<<endl;
            map<string,set<int>>::iterator mp=book[type].find(s); //返回查询到s的迭代器
            if(mp==book[type].end()) cout<<"Not Found"<<endl;     //若map中没有s
            else{
                for(set<int>::iterator it=book[type][s].begin();it!=book[type][s].end();it++)
                    printf("%07d\n",*it);                         //输入s对应的所有id
            }
        }            
        return 0;
    }
    

    发表于 2018-03-08 23:04:13 回复(0)
    易错点:输出是没有固定长度(7位),PAT上最后两个测试点没法通过;
    
    #include <iostream>
    #include <cstdio>
    #include <set>
    #include <map>
    #include <string>
    using namespace std;
    const int maxn = 10010;
    int N, M;
    map<string, set<int>>s_title, s_author, s_key, s_pub, s_year;
    void query(int num,string inf) {     set<int>::iterator it;     switch (num) {     case 1:         if (s_title.find(inf) == s_title.end()) {             printf("Not Found\n");             return;         }         for (it = s_title[inf].begin(); it != s_title[inf].end(); it++) {             printf("%07d\n", *it);         }         break;     case 2:         if (s_author.find(inf) == s_author.end()) {             printf("Not Found\n");             return;         }         for (it = s_author[inf].begin(); it != s_author[inf].end(); ++it) {             printf("%07d\n", *it);         }         break;     case 3:         if (s_key.find(inf) == s_key.end()) {             printf("Not Found\n");             return;         }         for (it = s_key[inf].begin(); it != s_key[inf].end(); ++it) {             printf("%07d\n", *it);         }         break;     case 4:         if (s_pub.find(inf) == s_pub.end()) {             printf("Not Found\n");             return;         }         for (it = s_pub[inf].begin(); it != s_pub[inf].end(); ++it) {             printf("%07d\n", *it);         }         break;     case 5:         if (s_year.find(inf) == s_year.end()) {             printf("Not Found\n");             return;         }         for (it = s_year[inf].begin(); it != s_year[inf].end(); ++it) {             printf("%07d\n", *it);         }         break;     }
    }
    int main() {     int ID;     string title, author, key, pub, year;     cin >> N;     for (int i = 0; i < N; ++i) {         scanf("%d", &ID);         getline(cin, title);         getline(cin, title);         s_title[title].insert(ID);         getline(cin, author);         s_author[author].insert(ID);         while (cin >> key) {             s_key[key].insert(ID);             char c = getchar();             if (c == '\n')break;         }         getline(cin, key);         s_pub[key].insert(ID);         cin >> year;         s_year[year].insert(ID);     }     //开始查询     cin >> M;     int num;     string cate,inf;     for (int i = 0; i < M; ++i) {         scanf("%d: ", &num);         //num = cate[0] - '0';         getline(cin,inf);         cout <<num<<": " << inf<<endl;         query(num, inf);     }     return 0;
    }
    
    
    
    

    发表于 2019-07-31 22:16:02 回复(0)
    d = {}
    for i in range(int(input())):
        b = [input() for j in range(6)]
        for j in b[1:3] + b[3].split() + b[4:]:
            try:
                d[j].append(b[0])
            except:
                d[j] = [b[0]]
    
    for i in range(int(input())):
        b = input()
        try:
            print('\n'.join([b] + sorted(d[b[3:]])))
        except:
            print(b + "\nNot Found")
    

    发表于 2020-02-26 10:16:47 回复(0)
    #include<bits/stdc++.h>
    using namespace std;
    
    map<string,set<int> > mpt,mpa,mpk,mpp,mpy;
    
    void query(map<string,set<int> > & mp,string & q) {
    	if(mp.find(q)==mp.end()) cout<<"Not Found"<<endl;
    	else {
    		for(set<int>::iterator it=mp[q].begin(); it!=mp[q].end(); it++) {
    			printf("%07d\n",*it);
    		}
    	}
    }
    
    int main() {
    	int n,m,id;
    	string title,author,key,publisher,year;
    	cin>>n;
    	for(int i=0; i<n; i++) {
    		cin>>id;
    		getchar();
    		getline(cin,title);
    		mpt[title].insert(id);
    		getline(cin,author);
    		mpa[author].insert(id);
    		while(cin>>key) {
    			mpk[key].insert(id);
    			char c=getchar();
    			if(c=='\n') break;
    		}
    		getline(cin,publisher);
    		mpp[publisher].insert(id);
    		getline(cin,year);
    		mpy[year].insert(id);
    	}
    	cin>>m;
    	int type;
    	string q;
    	for(int i=0; i<m; i++) {
    		scanf("%d: ",&type);
    		getline(cin,q);
    		cout<<type<<": "<<q<<endl;
    		if(type==1) query(mpt,q);
    		else if(type==2) query(mpa,q);
    		else if(type==3) query(mpk,q);
    		else if(type==4) query(mpp,q);
    		else query(mpy,q);
    	}
    	return 0;
    }

    发表于 2022-11-20 12:10:01 回复(0)
    我说第五个测试点为什么一直过不去,原来是数组开小了[热词系列_爱了爱了]
    #include<iostream>
    #include<string>
    #include<map>
    #include<vector>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<bits/stdc++.h>
    
    #define INF 2147483647
    #define MIN INF+1
    #define ll long long
    
    using namespace std;
    
    struct book {
    	string info[6];
    	set<string> keywords;
    };
    
    bool cmp(book b1, book b2) {
    	return b1.info[0] < b2.info[0];
    }
    
    book b[10005];
    
    bool check(int i, int lb, int ub) {
    	if(i >= lb && i <= ub)
    		return true;
    	cout << "|" << i << "|" << endl;
    	return false;
    }
    
    int main() {    
    	// std::ios::sync_with_stdio(false);
        // std::cin.tie(0);
        
    	string Ns, Ms;
    	getline(cin, Ns);
    	int N = atoi(Ns.c_str());
    
    	for(int i = 0; i < N; i++) {
    		check(i, 0, N);
    		getline(cin, b[i].info[0]);
    		getline(cin, b[i].info[1]);
    		getline(cin, b[i].info[2]);
    		getline(cin, b[i].info[3]);
    		getline(cin, b[i].info[4]);
    		getline(cin, b[i].info[5]);
    		
    		std::string s = b[i].info[3];
    		
    		check(i, 0, N);
    		while(s.find(" ") != string::npos) {
    			string key = s.substr(0, s.find(" "));
    			s = s.substr(s.find(" ") + 1);
    			b[i].keywords.insert(key);
    		}
    		b[i].keywords.insert(s);
    		
    	}
    	
    	// 按照ID升序排序 
    	
    	sort(b, b + N, cmp);
    	
    	getline(cin, Ms);
    	int M = atoi(Ms.c_str());
    	while(M--) {
    		string query;
    		getline(cin, query);
    		cout << query << endl;
    		int qn = query[0] - '0';
    		string qs = query.substr(3, -1);
    		
    		bool has = false;
    		
    		for(int i = 0; i < N; i++) {
    			if(qn == 3) {
    				check(i, 0, N);
    				check(qn, 0, 5);
    				if(b[i].keywords.find(qs) != b[i].keywords.end()) {
    					has = true;
    					cout << b[i].info[0] << endl;
    				}
    			} else {
    				check(i, 0, N);
    				check(qn, 0, 5);
    				if(b[i].info[qn] == qs) {
    					cout << b[i].info[0] << endl;
    					has = true;
    				}
    			}
    		}
    		
    		if(!has) {
    			cout << "Not Found" << endl; 
    		}
    	}
    	return 0;
    } 


    发表于 2020-09-18 21:22:51 回复(0)
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <sstream>
    using namespace std;


    //1022 Digital Library

    class book {

    public:
        string id;//7位
        string title;//<=80 characters
        string author;//<=80 characters,保证作者只有一个
        vector<string> keywords;//每个关键词不超过10个字符,关键词不超过5个
        string publisher;//出版商 <=80 characters
        string pbyear; // 出版年份 [10000,3000]
        bool operator ==(const book &b)const
        {
            return id == b.id;
        }
    };
    //全服关键词、出版商不会超过1000个

    bool mycomp(const book &a, const book &b) {
        return a.id < b.id;
    }


    void bookQuery(const string &s, int t, book *books, int n) {
        int flag = 0;

        //查书名
        if (t == 1) {
            for (int i = 0; i < n; ++i) {
                if (s == books[i].title) {
                    cout << books[i].id << endl;
                    flag = 1;
                }
            }
        }

        //查作者
        if (t == 2) {
            for (int i = 0; i < n; ++i) {
                if (s == books[i].author) {
                    cout << books[i].id << endl;
                    flag = 1;
                }
            }
        }

        //查关键词
        if (t == 3) {
            for (int i = 0; i < n; ++i) {
                for (unsigned int j =0; j < books[i].keywords.size(); ++j) //有风险是size=0时
                    if (s == books[i].keywords[j]) {
                        cout << books[i].id << endl;
                        flag = 1;
                    }
            }
        }

        //查出版商
        if (t == 4) {
            for (int i = 0; i < n; ++i) {
                if (s == books[i].publisher) {
                    cout << books[i].id << endl;
                    flag = 1;
                }
            }
        }

        //查出版年份 
        if (t == 5) {
            for (int i = 0; i < n; ++i) {
                if (s == books[i].pbyear) {
                    cout << books[i].id << endl;
                    flag = 1;
                }
            }
        }

        if (flag == 0) cout << "Not Found" << endl;
        return;
    }


    std::vector<std::string> split(const std::string& s, char delimiter)
    {
        std::vector<std::string> tokens;
        std::string token;
        std::istringstream tokenStream(s);
        while (std::getline(tokenStream, token, delimiter))
        {
            tokens.push_back(token);
        }
        return tokens;
    }

    int main() {
        int n, m;
        string str;
        char c;
        cin >> n;//<=10^4

        /*输入样例
        1 //书本总数n
        1111111  //id
        The Testing Book //tile
        Yue Chen //author
        test code debug sort keywords
        ZUCS Print
        2011
        */
        //注意,读入n之后,没有换行!还有一个'\n'字符在第一行
        //这时候如果不管,那么getline()读id的时候,会读到空白,然后跳到下一行
        //再次getline读title的时候,才会读到本来属于id的内容
        cin.get();//用这条语句把最后的\n处理掉

        book *books = new book[n + 1];
        for (int i = 0; i < n; ++i) {
            //输入n个item,每个占6行
            getline(cin, books[i].id);
            getline(cin, books[i].title);
            getline(cin, books[i].author);
            //第4行有多个关键词
            getline(cin, str);
            books[i].keywords = split(str, ' ');
            // while(getline(cin,str,' ')){
                // books[i].keywords.push_back(str);
                // c=getchar(); //读取字符串之后的空格或换行符
                // if(c=='\n') break; //遇见换行符说明关键词输入完毕
            // }
            getline(cin, books[i].publisher);
            getline(cin, books[i].pbyear);
        }

        sort(books, books + n, mycomp);

        cin >> m;//<=1000,查询信息
        //同上面的说理,此处需要处理换行符
        cin.get();
        for (int i = 0; i < m; ++i) {
            getline(cin, str);
            cout << str << endl;
            bookQuery(str.substr(3), str[0] - '0', books, n);
        }


        return 0;
    }
    发表于 2019-01-25 23:45:26 回复(0)
    n = input()
    n = int(n)
    tem = [[] for i in range(n)]
    for i in range(n):
        for j in range(0,6):
            tem[i].append(input())
    tem.sort(key=lambda x:(x[0]))
    m = input()
    m = int(m)
    for i in range(m):
        boo = True
        temp= input()
        print(temp)
        index = int(temp[0])
        temp = temp[3:]
        for j in tem:
            if temp == j[index]:
                print(j[0])
                boo = False
            else:
                klst = list(j[index].split())
                for kt in klst:
                    if temp == kt:
                        print(j[0])
                        boo = False
                        break
        if boo:
            print("Not Found")
    
    需要知道的是,开头的数字 代表要寻找的内容范围book title,author,keywords,publisher,year...
    发表于 2018-12-10 23:19:22 回复(0)
    这道题就是坑,一直报错numberFormatException错误,明明自己的测试数据都弄成字符串啦,题上明明year和id是int类型
    发表于 2018-10-10 23:31:53 回复(0)
    思路:构建结构体,然后求解,mb,老子的代码怎么这么长??
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <sstream>
    #include <algorithm>
    using namespace std;
    
    struct book
    {
        string ID;
        string title;
        string author;
        vector<string> keywords;
        string publisher;
        string year;
    };
    
    vector<string> SearchSystem(const vector<struct book> & v, string query)
    {
        vector<string> rlt;
        if (query.size() == 0)
        {
            return rlt;
        }
        string year;
        string queryAvil = query.substr(3);
        switch (query[0])
        {
            case '1'://title
                for (int i = 0; i < v.size(); i++)
                {
                    if (queryAvil == v[i].title)
                        rlt.push_back(v[i].ID);
                }
                break;
            case '2'://author
                for (int i = 0; i < v.size(); i++)
                {
                    if (queryAvil == v[i].author)
                        rlt.push_back(v[i].ID);
                }
                break;
            case '3'://keywords
                for (int i = 0; i < v.size(); i++)
                {
                    for (int j = 0; j < v[i].keywords.size(); j++)
                    {
                        if (queryAvil == v[i].keywords[j])
                        {
                            rlt.push_back(v[i].ID);
                        }
                    }
                }
                break;
            case '4':// publisher
                for (int i = 0; i < v.size(); i++)
                {
                    if (queryAvil == v[i].publisher)
                        rlt.push_back(v[i].ID);
                }
                break;
            case '5':// year
                for (int i = 0; i < v.size(); i++)
                {
                    if (queryAvil == v[i].year)
                        rlt.push_back(v[i].ID);
                }
                break;
            default:
                break;
        }
        if (rlt.size() == 0)
        {
            rlt.push_back("Not Found");
        }
        return rlt;
    }
    
    bool Cmp(struct book & a, struct book & b)
    {
        return a.ID < b.ID;
    }
    
    int main()
    {
        int N,M;
        //ifstream cin("test.txt");
        while (cin >> N)
        {
            vector<struct book> bookDatabase(N);
            for (int i = 0; i < N; i++)
            {
                cin >> bookDatabase[i].ID;
                char c = getchar();
                //cin.get();
                getline(cin, bookDatabase[i].title);  
                getline(cin, bookDatabase[i].author);
                string tmp;
                string temp;
                getline(cin, tmp);
                for (int j = 0; tmp[j] != '\0'; j++)
                {
                    if(tmp[j]!=' ')
                        temp += tmp[j];
                    if (j < tmp.size() && (tmp[j+1] == ' ' || tmp[j+1] == '\0' ))
                    {
                        bookDatabase[i].keywords.push_back(temp);
                        temp = "";
                    }
                }
                getline(cin, bookDatabase[i].publisher);
                cin >> bookDatabase[i].year;
            }
            sort(bookDatabase.begin(), bookDatabase.end(), Cmp);
            cin >> M;
            char c = getchar();
            //cin.get();
    
            string query;
            for (int i = 0; i < M; i++)
            {
                getline(cin, query);
                vector<string> rlt = SearchSystem(bookDatabase, query);
                cout << query << endl;
                for (int j = 0; j < rlt.size(); j++)
                {
                    cout << rlt[j] << endl;
                }
            }
        }
        system("pause");
    }
    

    编辑于 2018-08-22 10:15:12 回复(0)
    #include <iostream>
    #include <cstdio>
    #include <map>
    #include <set>
    using namespace std;
    map<string, set<int> > title, author, key, pub, year;
    void query(map< string, set<int> > &m, string &str) {
        if(m.find(str) != m.end()) {
            for(set<int>::iterator it = m[str].begin(); it != m[str].end(); it++)
                printf("%07d\n", *it);
        } else
            cout << "Not Found\n";
    }
    int main() {
        int n, m, id, num;
        scanf("%d", &n);
        string ttitle, tauthor, tkey, tpub, tyear;
        for(int i = 0; i < n; i++) {
            scanf("%d", &id);
            getchar();//
            getline(cin, ttitle);
            title[ttitle].insert(id);
            getline(cin, tauthor);
            author[tauthor].insert(id);
            while(cin >> tkey) {
                key[tkey].insert(id);
                char c;
                c = getchar();
                if(c == '\n') break;
            }
            getline(cin, tpub);
            pub[tpub].insert(id);
            getline(cin, tyear);
            year[tyear].insert(id);
        }
        scanf("%d", &m);
        for(int i = 0; i < m; i++) {
            scanf("%d: ", &num);
            string temp;
            getline(cin, temp);
            cout << num << ": " << temp << "\n";
            if(num == 1) query(title, temp);
            else if(num == 2) query(author, temp);
            else if(num == 3) query(key, temp);
            else if(num == 4) query(pub,temp);
            else if(num ==5) query(year, temp);
        }
        return 0;
    }

    发表于 2017-09-05 19:51:43 回复(0)
    #include <algorithm>
    #include <string>
    #include <unordered_map>
    #include <unordered_set>
    #include <cstdio>
    #include <vector>
    #include <sstream>
    
    using namespace std;
    
    const int maxn = 500, item = 6, interval = 3;
    
    unordered_map <string, unordered_set <string> > titleList;
    unordered_map <string, unordered_set <string> > authorList;
    unordered_map <string, unordered_set <string> > keyWordList;
    unordered_map <string, unordered_set <string> > publisherList;
    unordered_map <int, unordered_set <string> > publishYearList;
    vector <string> tempList;
    
    int main() {
    
        int m, n, i;
        string str, bookId;
        scanf("%d", &m);
        getchar();
    
        while (m--) {
    
            for (i = 0; i < item; i++) {
    
                char s[maxn];
                scanf("%[^\n]", s);
                getchar();
                str = s;
    
                if (i == 0)
                    bookId = str;
                else if (i == 1)
                    titleList[str].insert(bookId);
                else if (i == 2)
                    authorList[str].insert(bookId);
                else if (i == 3) {
                    vector <string> keyWordSave;
                    istringstream ss (str);
                    do {
                        string keyWordTemp;
                        ss >> keyWordTemp;
                        keyWordSave.push_back(keyWordTemp);
                    } while (ss);
                    keyWordSave.pop_back();
                    for (auto &j : keyWordSave) {
                        keyWordList[j].insert(bookId);
                    }
                }
                else if (i == 4)
                    publisherList[str].insert(bookId);
                else
                    publishYearList[stoi(str, nullptr, 16)].insert(bookId);
            }
        }
    
        scanf("%d", &n);
        getchar();
    
        while (n--) {
    
            char s[maxn];
            scanf("%[^\n]", s);
            getchar();
            str = s;
            string query (str, interval);
            vector <string> ().swap(tempList);
    
            if (str[0] == '1')
                copy(titleList[query].begin(), titleList[query].end(), back_inserter(tempList));
            else if (str[0] == '2')
                copy(authorList[query].begin(), authorList[query].end(), back_inserter(tempList));
            else if (str[0] == '3')
                copy(keyWordList[query].begin(), keyWordList[query].end(), back_inserter(tempList));
            else if (str[0] == '4')
                copy(publisherList[query].begin(), publisherList[query].end(), back_inserter(tempList));
            else {
                int queryInt = stoi(query, nullptr, 16);
                copy(publishYearList[queryInt].begin(), publishYearList[queryInt].end(), back_inserter(tempList));
            }
            
            printf("%s\n", str.c_str());
            if (tempList.size() == 0)
                printf("Not Found\n");
            else {
                sort(tempList.begin(), tempList.end());
                for (auto &j : tempList)
                    printf("%s\n", j.c_str());
            }
        }
        return 0;
    }
    

    发表于 2017-05-03 20:30:47 回复(0)
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<map>
    using namespace std;
    struct Node{
        string ID;
    };
    
    vector<string> StrSplit(string str,char ch){
        vector<string> result;
        int pos = 0;
        for (int i=0; i<str.size(); ++i) {
            pos = (int)str.find(ch,i);
            if(pos<str.size()){//找到字符串.
                string  te = str.substr(i,pos-i);
                result.push_back(te);
                i = pos;
            }else{
                string  te = str.substr(i,str.size()-1);
                result.push_back(te);
                break;
            }
        }
        return result;
    }
    vector<Node>allBooks;
    bool cmp(int a,int b)
    {
        return allBooks[a].ID<allBooks[b].ID;
    }
    int main(){
        int N,M,kind;
        scanf("%d%*c",&N);
        map<string, vector<int>> authorMap,keyWordMap,titleMap,publisherMap,yearMap;
        string title,author,keystr,publisher,year,query;
        vector<string>curKeys;
        allBooks.resize(N);
        for(int i=0;i<N;++i)
        {
            getline(cin,allBooks[i].ID);
            getline(cin, title);
            titleMap[title].push_back(i);
            getline(cin, author);
            authorMap[author].push_back(i);
            getline(cin, keystr);
            vector<string> keys = StrSplit(keystr,' ');
            for (int j=0; j<keys.size();++j)
                keyWordMap[keys[j]].push_back(i);
            getline(cin, publisher);
            publisherMap[publisher].push_back(i);
            getline(cin, year);
            yearMap[year].push_back(i);
        }
        scanf("%d",&M);
        for (int i=0; i<M;++i) {
            scanf("%d: ",&kind);
            getline(cin, query);
            printf("%d: %s\n",kind,query.c_str());
            vector<int>res;
            switch (kind) {
                case 1:
                    res.assign(titleMap[query].begin(),titleMap[query].end());
                    break;
                case 2:
                    res.assign(authorMap[query].begin(),authorMap[query].end());
                    break;
                case 3:
                    res.assign(keyWordMap[query].begin(),keyWordMap[query].end());
                    break;
                case 4:
                    res.assign(publisherMap[query].begin(),publisherMap[query].end());
                    break;
                case 5:
                    res.assign(yearMap[query].begin(),yearMap[query].end());
                    break;
            }
            if (res.size()==0)
                printf("Not Found\n");
            else{
                sort(res.begin(),res.end(),cmp);
                for (int j=0; j<res.size(); ++j)
                    printf("%s\n",allBooks[res[j]].ID.c_str());
            }
        }
        return 0;
    }
    


    发表于 2016-03-06 12:18:17 回复(0)
    啥头像
    硬来
    class Book:
        def __init__(self, ID, title, author, keyWords, publisher, year):
            self.ID = ID; self.title = title
            self.author = author; self.keyWords = keyWords
            self.publisher = publisher; self.year = year
        def __cmp__(self, other):
            return cmp(self.ID, other.ID)
    
    N = input()
    books = [0]*N
    for i in range(N):
        ID = raw_input(); title = raw_input()
        author = raw_input(); keyWords = raw_input().strip().split()
        publisher = raw_input(); year = raw_input()
        books[i] = Book(ID, title, author, keyWords, publisher, year)
    
    books.sort()
    
    K = input()
    while K:
        K -= 1
        string = raw_input().strip()
        print string
        idx, text = string.split(': ')
        idx = int(idx); rlt = []
        if idx == 3:
            for i in range(N):
                if text in books[i].keyWords:
                    rlt.append(books[i].ID)
        elif idx == 1:
            for i in range(N):
                if text == books[i].title:
                    rlt.append(books[i].ID)
        elif idx == 2:
            for i in range(N):
                if text == books[i].author:
                    rlt.append(books[i].ID)
        elif idx == 4:
            for i in range(N):
                if text == books[i].publisher:
                    rlt.append(books[i].ID)
        else:
            for i in range(N):
                if text == books[i].year:
                    rlt.append(books[i].ID)
        if rlt:
            for i in rlt:
                print i
        else:
            print 'Not Found' 


    发表于 2016-03-05 10:39:13 回复(1)