PAT A1022 Digital Library [O the STL]

Title Description

Links
analog to digital library search function. Will give information, n and m order book to be queried, reference numbers corresponding to the respective commands, digital serial string behind the search query word, line id required output command and output satisfies the condition of the book, If you have not found a output not found

analysis

  • 5 establish map, representing 5 to xx string mappings. What this xx is it? Because the query result is not unique, the array is at least, and then to automatically sorted id, the SET use, it is a structure map <string, set >
  • O problems (which are summarized in the other blog too)! ! ! Very pit! ! !
  • Establishment query, you can map a different name unification in the form of parameter passing, first determine map.find () and m.end () are equal, if unequal to go through the entire map, all id output to meet the conditions, If they are equal it means that the search term corresponding id does not exist, then it would have output not Found ~
  • Be sure to pass parameters passed by reference! ! ! ! ! Finally otherwise make life difficult
  • C ++ 11 inside, auto type is not the type of store, but automatically infer the type, so do not writemap<string, set<int> >::iterator it
#include<bits/stdc++.h>
using namespace std;

map<string, set<int> > title, author, keyword, pub, year;
int n,id,q;
string ttitle, tauthor, tkey, tpub, tyear;

void query(map<string, set<int> > &m, string &str){  //坑点1,把查询抽象成一个函数
    if(m.find(str) != m.end()){
        for(auto it = m[str].begin(); it != m[str].end(); it++){ //坑点2,用auto
            printf("%07d\n", (*it)); //坑点3:记住前导0!!!
        }
    }else{
        printf("Not Found\n");
    }
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&id);
        getchar(); //坑点4:记得getline前吸收
        getline(cin, ttitle);
        title[ttitle].insert(id);  //坑点5:使用这个结构map<string, set<int> >
        getline(cin, tauthor);
        author[tauthor].insert(id);
        while(cin>>tkey){  //坑点6:没有告诉关键词个数,同时后面还有输入的情况
            keyword[tkey].insert(id);
            char ch = getchar();
            if(ch == '\n') break; //坑点6:没有告诉关键词个数,同时后面还有输入的情况
        }
        getline(cin, tpub);
        pub[tpub].insert(id);
        getline(cin, tyear);
        year[tyear].insert(id);
    }
    scanf("%d",&q);
    while(q--){
        scanf("%d: ",&id);  //坑点7:记得保持这个输入格式
        string qstr;
        getline(cin, qstr);  //坑点8:输入的字符串有空格,是整行
        cout<<id<<": "<<qstr<<endl;  //坑点9:坑点太多,忘记还要输出这个
        if(id == 1) query(title, qstr);
        else if(id == 2) query(author, qstr);
        else if(id == 3) query(keyword, qstr);
        else if(id == 4) query(pub, qstr);
        else if(id == 5) query(year, qstr);
    }


}

Guess you like

Origin www.cnblogs.com/doragd/p/11316446.html