Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<unordered_map>
- #include <string>
- #include <map>
- #include <iterator>
- int main()
- {
- std::map<std::string, int> sub_words;
- int line_len, word_len;
- std::cin >> line_len >> word_len;
- std::cin.ignore();
- std::string line;
- std::getline(std::cin, line);
- for(int i=0;i<=line_len;i++)
- {
- for(int j=0;j<=line_len-i;j++)
- {
- std::string sub_str = line.substr(i, j);
- if(sub_words.find(sub_str) == sub_words.end())
- {
- //not found
- sub_words[sub_str] = 1;
- }
- else
- {
- sub_words[sub_str]++;
- }
- }
- }
- int max_number = 0;
- std::string max_key;
- std::map<std::string, int>::iterator it = sub_words.begin();
- for (; it != sub_words.end(); ++it)
- {
- if (it->first.length() == word_len) {
- if (it->second >= max_number)
- {
- max_number = it->second;
- max_key = it->first;
- }
- }
- }
- std::cout << max_key << std::endl;
- /*for(auto et=sub_words.begin();et!=sub_words.end();++et)
- {
- std::cout << et->first << "=" << et->second << std::endl;
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement