All pastes #2092541 Raw Edit

Miscellany

public text v1 · immutable
#2092541 ·published 2011-10-22 14:12 UTC
rendered paste body
    #include <iostream>
    #include <map>
    #include <string>
    #include <stdint.h>
    #include <vector>
    #include <set>
    #include <cstring>
     
    using namespace std;
     
    void LoadNotes(uint8_t notes[10000], int& PattLen, int& PattNum, int& N)
    {
            cin >> PattLen;
            cin >> PattNum;
            cin >> N;
     
            for (int i = 0; i < N; i++)
            {
                    cin >> notes[i];
            }
    }
     
    struct pattern {
            pattern(int StartIndex)
            :startIndex(StartIndex), numberOfOccurence(1)
            {}
     
            int startIndex;
            int numberOfOccurence;
    };
     
    // Returns the number of valid pattern of a certain length
    int GenerateAllPatternsOfLen(uint8_t notes[10000], int len, int pattNum, int N)
    {
            // Map patterns to their number of occurence
            vector<pattern> patterns;
            // We stop at len - 1 since there is no point in checking for an
            // overflowing pattern
            for(int i = 0; i < N - (len - 1); i++)
            {
                    bool existing = false;
                    // Check if it's an existing pattern. If not, add it
                    for (unsigned int j = 0; j < patterns.size(); j++)
                    {
                            // Compare the pattern
                            int result = strncmp((char*)&(notes[patterns[j].startIndex]), (char*)&notes[i], len);
                            if (result == 0)
                            {
                                    existing = true;
                                    patterns[j].numberOfOccurence++;
                                    break; // We can only match one pattern of a given length
                            }
                    }
     
                    if (!existing)
                    {
                            patterns.push_back(pattern(i));
                    }
            }
     
            int sum = 0;
            for (unsigned int i = 0; i < patterns.size(); i++)
            {
                    if (patterns[i].numberOfOccurence >= pattNum)
                    {
                            sum += patterns[i].numberOfOccurence;
                    }
            }
     
            return sum;
    }
     
    int GenerateAllPatterns(uint8_t notes[10000], int pattLen, int pattNum, int N)
    {
            int sum = 0;
            // We stop at N - pattNum since there is no way we can have pattNum occurence
            // of a pattern of size more than N - pattNum + 1. i.e. N = 3, pattnum = 3
            // it is impossible to have 3 pattern of size 2
            for (int i = pattLen; i < (N - (pattNum - 1)); i++)
            {
                    sum += GenerateAllPatternsOfLen(notes, i, pattNum, N);
            }
     
            return sum;
    }
     
    int main ()
    {
            uint8_t notes[10000];
            int pattLen, pattNum, N;
     
            LoadNotes(notes, pattLen, pattNum, N);
     
            int result = GenerateAllPatterns(notes, pattLen, pattNum, N);
     
            cout << result;
     
            return 0;
    }