Advertisement
arsovski

Untitled

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