Advertisement
Gistrec

Solve the Frequent Words Problem

May 10th, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. /**
  2.  * Input: A string Text and an integer k.
  3.  * Output: All most frequent k-mers in Text.
  4.  */
  5.  
  6. #include <iostream>  // std::cout, std::endl
  7. #include <string>    // std::string
  8. #include <map>       // std::map
  9. #include <algorithm> // std::find
  10.  
  11. using std::map;
  12. using std::string;
  13. using std::find;
  14. using std::cout;
  15. using std::endl;
  16.  
  17. int main() {
  18.     int length = 4;
  19.     string input = "ACGTTGCATGTCGCATGATGCATGAGAGCT";
  20.  
  21.     map<string, int> array;
  22.  
  23.     for (int pos = 0; pos < input.length() - length; ++pos) {
  24.         string substr = input.substr(pos, length);
  25.  
  26.         auto search = array.find(substr);
  27.  
  28.         // Если строка уже встречалась в тексте
  29.         if (search != array.end()) {
  30.             search->second += 1;
  31.         }else {
  32.             array[substr] = 1;
  33.         }
  34.     }
  35.  
  36.     int max = 0;
  37.     for (auto &substr : array) {
  38.         if (max < substr.second) {
  39.             max = substr.second;
  40.         }
  41.     }
  42.  
  43.     for (auto &substr : array) {
  44.         if (max == substr.second) {
  45.             cout << substr.first << " ";
  46.         }
  47.     }
  48.  
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement