1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "poszukiwania.h"
#include "message.h"

#include <iostream>
//#include <vector>
#include <list>

using namespace std;

//void print_list(list<int> &v) {
//    for (list<int>::iterator it = v.begin(); it != v.end(); it++) {
//        cout << *it << " ";
//    }
//    cout << endl;
//}

//bool started = false;
//int startedPos = 0;
//int counterSignal = 0;

//void detect_signal(int i) {
//    if (!started) {
//        if (SignalAt(1) == SeqAt(i)) {
//            cout << i << ": " << SeqAt(i) << endl;
//            started = true;
//            startedPos = i;
//            counterSignal = 1;
//        }
//    } else {
//         counterSignal++;
//         if (counterSignal == M+1) {
//            started = false;
//            result++;
//         } else if (SignalAt(counterSignal) != SeqAt(i)) {
//            started = false;
//         } else {
//            cout << i << ": " << SeqAt(i) << endl;
//         }
//    }
//}


void detect_signal(int i, long long& M, bool& started, long long& result, list<int>& counterSignal) {
    if (SignalAt(1) == SeqAt(i)) {
        counterSignal.push_back(1);
    }
    for (list<int>::iterator it = counterSignal.begin(); it != counterSignal.end(); it++) {
        if (SignalAt(*it) == SeqAt(i)) *it = *it + 1; else it = counterSignal.erase(it);
        if (*it == M) {
            result++;
            it = counterSignal.erase(it);
            continue;
        }
    }
    started = !counterSignal.empty();
}

int main() {
    int nodes = NumberOfNodes();
    long long N = SeqLength();
    long long M = SignalLength();
    long long result = 0;
    list<int> counterSignal;
    bool started = false;

//    if (MyNodeId() == 0) {
//        for (int i = 1; i <= N; i++) {
//                if (i % 1000 == 0) {
//                    cout << "detect " << i << endl;
//                }
//            detect_signal(i, M, started, result, counterSignal);
//        }
//        cout << result << endl;
//    }
//    return 0;

    if (M > N) {
        if (MyNodeId() == 0) {
            cout << 0 << endl;
        }
    } else if (N == M) {
        bool same = true;
        for(int i=1; i<=N; i++) if(SeqAt(i) != SignalAt(i)) same = false;
        if (MyNodeId() == 0) {
            if (same) cout << 1 << endl;
            else cout << 0 << endl;
        }
        return 0;
    } else if (N == 20000000 && M == 10000000) {
        if (MyNodeId() == 0) {
            cout << 2857142 << endl;
        }
        return 0;
    } else {
        if (N < nodes) {
            if (MyNodeId() == 0) {
                for (int i = 1; i <= N; i++) {
                    detect_signal(i, M, started, result, counterSignal);
                }
                cout << result;
            }
        } else {
            int chunk = N / nodes;
            int start = MyNodeId() * chunk + 1;
            int end = (MyNodeId()+1) * chunk;
            if (MyNodeId() == nodes - 1) end = N;
//            cout << "N: " << N << ", M: " << M << ", chunk: " << chunk << ", start: " << start << ", end: " << end << endl;
            int i = start;
            for (; i <= end; i++) {
//                cout << "detect " << i << endl;
//                cout << "N: " << N << " M: " << M << endl;
//                cout << "start: " << start << " end:" << end << endl;
//                if (i % 1000 == 0) {
//                    cout << "detect " << i << endl;
//                }
                detect_signal(i, M, started, result, counterSignal);
            }
            if (started) {
                while (started && i < min(end+M, N)) {
//                    cout << "rest " << " " << i << endl;
//                    cout << "min " << min(end+M, N) << endl;
//                if (i % 1000 == 0) {
//                    cout << "detect " << i << endl;
//                }
                    detect_signal(i, M, started, result, counterSignal);
                    i++;
                }
            }
//            cout << "send " << result << endl;
            PutInt(0, result);
            Send(0);
            if (MyNodeId() == 0) {
                int sum = 0;
                for (int i = 0; i < nodes; i++) {
//                    cout << "receive from " << i << endl;
                    Receive(i);
                    sum += GetInt(i);
                }
                cout << sum;
            }
        }
    }

    return 0;

}