Gistrec icon

Solve the Frequent Words Problem

Gistrec | PRO | 05/10/18 07:29:40 PM UTC | 0 ⭐ | 726 👁️ | Never ⏰ | []
C++ |

1.12 KB

|

None

|

0 👍

/

0 👎

/**
 * Input: A string Text and an integer k.
 * Output: All most frequent k-mers in Text.
 */
 
#include <iostream>  // std::cout, std::endl
#include <string>    // std::string
#include <map>       // std::map
#include <algorithm> // std::find
 
using std::map;
using std::string;
using std::find;
using std::cout;
using std::endl;
 
int main() {
    int length = 4;
    string input = "ACGTTGCATGTCGCATGATGCATGAGAGCT";
 
    map<string, int> array;
 
    for (int pos = 0; pos < input.length() - length; ++pos) {
        string substr = input.substr(pos, length);
 
        auto search = array.find(substr);
 
        // Если строка уже встречалась в тексте
        if (search != array.end()) {
            search->second += 1;
        }else {
            array[substr] = 1;
        }
    }
 
    int max = 0;
    for (auto &substr : array) {
        if (max < substr.second) {
            max = substr.second;
        }
    }
 
    for (auto &substr : array) {
        if (max == substr.second) {
            cout << substr.first << " ";
        }
    }
 
 
    return 0;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎