基本思想:
注意一个新的点,对于map如果有排序去重的问题,完全可以map<string,set<int>>来解决,没必要用vector进行push;
关键点:
新的TLE问题点,对于大型复杂性数据结构,可以采用引用传入而不是值拷贝的形式,这个尤其要注意下;
#include<iostream> #include<stdlib.h> #include<stdio.h> #include<vector> #include<string> #include<math.h> #include<algorithm> #include<cstring> #include<map> #include<queue> #include<set> using namespace std; int n; map<string, set<int>>tmap, amap, kmap, pmap, ymap; void query(string s,map<string,set<int>>& m) { if (m.find(s) == m.end()) { printf("Not Found\n"); return; } for (set<int>::iterator it = m[s].begin(); it != m[s].end(); it++) { printf("%07d\n", *it); } } int main() { int id; string title, author, keyword, publisher, year; cin >> n; getchar(); for (int i = 0; i < n; i++) { scanf("%d", &id); getchar(); getline(cin, title); tmap[title].insert(id); getline(cin, author); amap[author].insert(id); while (cin >> keyword) { kmap[keyword].insert(id); char c = getchar(); if (c == '\n') break; } getline(cin, publisher); pmap[publisher].insert(id); getline(cin, year); ymap[year].insert(id); } int m; string temp; int type; cin >> m; for (int i = 0; i < m; i++) { scanf("%d: ", &type); getline(cin, temp); printf("%d: %s\n", type, temp.c_str()); if (type == 1) { query(temp, tmap); } else if (type == 2) { query(temp, amap); } else if (type == 3) { query(temp, kmap); } else if (type == 4) { query(temp, pmap); } else { query(temp, ymap); } } return 0; }