Advertisement
arsovski

Untitled

Dec 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include<iostream>
  2. #include<unordered_map>
  3. #include <string>
  4. #include <map>
  5.  
  6. int main()
  7. {
  8.     std::map<std::string, int> sub_words;
  9.     int line_len, word_len;
  10.     std::cin >> line_len >> word_len;
  11.  
  12.     std::cin.ignore();
  13.     std::string line;
  14.     std::getline(std::cin, line);
  15.  
  16.     for(int i=0;i<=line_len - word_len ;i++)
  17.     {
  18.         std::string key = line.substr(i, word_len);
  19.  
  20.         if(sub_words.find(key) == sub_words.end())
  21.         {
  22.             //not found
  23.             sub_words[key] = 1;
  24.         }
  25.         else
  26.         {
  27.             sub_words[key]++;
  28.         }
  29.     }
  30.  
  31.     //TODO take only keys with value of word_len
  32.     //TODO find value from behin of list
  33.  
  34.     int max_number = 0;
  35.     std::string max_key;
  36.     std::map<std::string, int>::reverse_iterator it = sub_words.rbegin();
  37.  
  38.     max_key = it->first;
  39.     max_number = it->second;
  40.     for(;it != sub_words.rend();++it)
  41.     {
  42.         if(it->second > max_number)
  43.         {
  44.             max_number = it->second;
  45.             max_key = it->first;
  46.         }
  47.  
  48.     }
  49.  
  50.     std::cout << max_key<< std::endl;
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement