Pagini recente » Cod sursa (job #1568114) | Cod sursa (job #1752673) | Cod sursa (job #820906) | Cod sursa (job #2664189) | Cod sursa (job #629635)
Cod sursa(job #629635)
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int B = 67;
const int P = 666013;
string a,b;
int Bp = 1, BBp = 1;
vector<int> poz;
int x;
int calc[256];
int main()
{
ifstream fin("strmatch.in");
fin>>a>>b;
int k = a.length(), i;
for(int i=1; i<k; ++i)
{
Bp = Bp * B % P;
}
int hashb=0, hasha=0;
for (i=0; i < k; i++)
{
hasha = (hasha * B + a[i]) % P;
}
k--;
for (i=0; i < k; i++)
{
hashb = (hashb * B + b[i]) % P;
}
k++;
int j = b.length();
for (int i = 0; i <256; i++)
calc[i] = (i * Bp) % P;
for(i=k-1; i < j;i++)
{
hashb = ((P + hashb - calc[b[i - k]]) * B + b[i])%P;
if( hasha == hashb && i >= k - 1)
{
/* int ok = 0, s=i-k+1;
for(int q = 0; q<k; ++q )
if(a[q] != b[s+q])
ok = 1;
if( !ok)
poz.push_back(s);*/
}
}
ofstream fout("strmatch.out");
i = (int)poz.size();
fout<<i<<"\n";
j = min(1000, i);
for(i=0;i < j ;i++)
fout<<poz[i]<<" ";
fout<<"\n";
return 0;
}