/*这道题主要练习了map的使用。 *使用map来操作,使得该题很简单。 */ #include <iostream> #include <map> using namespace std; int main() { map<string, int> dic; //用来保存数据字典 map<string, int> desc; //用来保存描述 typedef map<string, int>::value_type valType; int m, n; cin >> m >> n; for(int i = 0; i < m; i++) { string word; int dollar; cin >> word >> dollar; dic.insert(valType(word, dollar)); //不断插入新的字典单词 } /* map<string, int>::iterator map_it = dic.begin(); while(map_it != dic.end()) { cout << map_it->first << " " << map_it->second << endl; map_it++; } */ int sumSalary = 0; string desc_word; int descCount = 0; while(cin >> desc_word) { /*遇到'.'时的处理*/ if(desc_word == ".") { cout << sumSalary << endl; //输出当前总的salary sumSalary = 0; //置0,以处理下个描述的salary descCount++; if(descCount == n) break; else continue; } map<string, int>::iterator it = dic.find(desc_word); if(it != dic.end()) //若it不等于end,则表示在字典中找到该单词 sumSalary += it->second; } return 0; }