Advertisement
JosepRivaille

X34352: Freqüència de paraules amb diccionaris

Oct 30th, 2015
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5.  
  6.  
  7. int main() {
  8.   map<string, int> M;
  9.   char op;
  10.   string s;
  11.   while (cin >> op >> s) {
  12.     map<string, int>::const_iterator it = M.find(s);
  13.     if (op == 'f') {
  14.       if (it == M.end()) cout << 0 << endl;
  15.       else cout << it->second << endl;
  16.     }
  17.     else if (op == 'a') {
  18.       if (it == M.end()) M.insert(make_pair(s, 1));
  19.       else {
  20.     int freq = it->second;
  21.     ++freq;
  22.     M[s] = freq;
  23.       }
  24.     }
  25.   }
  26. }
  27.  
  28. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement