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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "poszukiwania.h"
#include "message.h"

#include <algorithm>
#include <iostream>
using namespace std;


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef int LL;

vector<LL> tab;
vector<LL> tekst;
vector<LL> pi;

LL kmp(vector<LL>&pos)
{
    LL m,q,k,n,i,j,res=0;
        m=tab.size()-1;
	pi.resize(tab.size(),0);
        pi[1]=0;
        k=0;
        for (q=2;q<=m;q++)
        {
            while (k>0 && tab[k+1]!=tab[q])
            {
                k=pi[k];
            }
            if (tab[k+1] == tab[q])
            {
                k++;
            }
            pi[q]=k;
        }
        
        n=tekst.size()-1;
        i=1;
        j=1;
        while (i<=n-m+1)
        {
            j=pi[j];
            while ((j<m)&&(tab[j+1]==tekst[i+j])) j=j+1;
            if (j==m) {res++;pos.push_back(i-1);}//printf("%d\n",i-1); //wypisanie indeksu, w którym istnieje dopasowanie
            i=i+ (1>j-pi[j]?1:j-pi[j]);
        }
	//printf("%lld: res:%lld\n",MyNodeId(),res);
	return res;

}




LL run(LL S, LL poczS, vector<LL>&pos)
{
  //cerr<<"run S:"<<S<<" poczS:"<<poczS<<"\n";
  long long res = 0;
  tab.clear();
  tab.push_back(-1);
  pi.clear();
  for(LL i=0;i<S;i++)
  {
    tab.push_back(SignalAt(i+1+poczS));
  }
  //cerr << "sizeof tab:" << tab.size() << "\n";
  kmp(pos);
  //cerr << "sizeof pos:" << pos.size() << "\n";
  return pos.size();
}

vector<LL>pos[20100];

int main() {

  const LL MAXS = 10000001;

  long long M = SeqLength();
  long long S = SignalLength();

  LL m = (M-S)/(NumberOfNodes())+1;
  LL SeqStart = m*MyNodeId();
  LL SeqEnd = min(M,m*(MyNodeId()+1)+S-1);
  //cerr<< "M " << M << " S " << S << "\n";
  //cerr<< SeqStart << " " << SeqEnd << "\n";
 
  long long res = 0;
  tekst.push_back(-1);
  if (SeqEnd-SeqStart >=S)
  {
    for(LL i=SeqStart;i<SeqEnd;i++)
    {
      tekst.push_back(SeqAt(i+1));
    }

    LL numsig = (S-1)/MAXS+1;


//  for(int i=0;i<pos.size();i++)cerr << pos[i]<<"\n";
  
    for (LL i = 0; i<numsig; i++)
    {
      run((i+1)*MAXS<S?MAXS:S%MAXS, i*MAXS,pos[i]);
    }
  
    vector<LL>::iterator iteratory[numsig];
    for(LL i = 0;i<numsig;i++)
    {
      iteratory[i]=pos[i].begin();
/*      for(auto it=pos[i].begin();it!=pos[i].end();it++)
      {
        cerr << *it << " ";
      }
      cerr << "\n";
  */  }

    for(auto it = iteratory[0]; it!=pos[0].end();it++)
    {
      LL pocz = *it;
    //  cerr << "pocz: " << pocz << "\n";
      int found=0;
      for(LL i=1;i<numsig;i++)
      {
        LL size = (i)*MAXS<S?MAXS:S%MAXS;
      //  cerr << "size " << size << "\n";
        while( iteratory[i]!=pos[i].end() && pocz + size > *iteratory[i]) iteratory[i]++;
        if(iteratory[i]==pos[i].end()){found=2;break;}
        if(pocz + size  != *iteratory[i]){found=1;/*cerr<<"it "<<i<<" "<<*iteratory[i]<<"\n";*/break;}
        pocz+=size;
      }
 //     cerr << "found " << found << "\n";
      if (found == 2) break;
      if (found == 0) res++;
    }
  }

  if (MyNodeId() != 0) 
  {
	PutLL(0,res);
	Send(0);
  }
  else
  {
	for(int i=0;i<NumberOfNodes()-1;i++)
	{
		LL o=Receive(-1);
		LL r =GetLL(o);
#ifdef DEBUG
		printf("%lld %lld\n",o,r);
#endif
		res+=r;

  //      printf("%lld\n",res);

	}
	printf("%lld\n",res);
  }


  return 0;
}